258 lines
9.9 KiB
C#
258 lines
9.9 KiB
C#
|
using Alkami.Ops.Common.FileSystem;
|
|||
|
using NUnit.Framework;
|
|||
|
using System;
|
|||
|
using System.IO;
|
|||
|
using System.IO.Compression;
|
|||
|
|
|||
|
namespace Alkami.Ops.Common.Integration.Testing
|
|||
|
{
|
|||
|
[TestFixture]
|
|||
|
public class ZipHelperIntegrationTests
|
|||
|
{
|
|||
|
private string tempPath;
|
|||
|
|
|||
|
[SetUp]
|
|||
|
public void Setup()
|
|||
|
{
|
|||
|
this.tempPath = Path.Combine(Path.GetTempPath() + "ZipTests");
|
|||
|
}
|
|||
|
|
|||
|
[Test]
|
|||
|
public void WhenCallingUnzip_WithDestinationDirectory_FilesAreUnzippedAtDestinationLocation()
|
|||
|
{
|
|||
|
// Arrange
|
|||
|
var zipPath = this.CreateRandomizedZip();
|
|||
|
var destinationDirectory = Path.Combine(Path.GetDirectoryName(zipPath), "output");
|
|||
|
|
|||
|
using (var zipArchive = ZipFile.OpenRead(zipPath))
|
|||
|
{
|
|||
|
Directory.CreateDirectory(destinationDirectory);
|
|||
|
|
|||
|
// Act
|
|||
|
ZipHelpers.UnZip(zipPath, destinationDirectory);
|
|||
|
|
|||
|
// Assert
|
|||
|
foreach (var entry in zipArchive.Entries)
|
|||
|
{
|
|||
|
var attributes = File.GetAttributes(Path.Combine(destinationDirectory, entry.FullName));
|
|||
|
|
|||
|
if (attributes.HasFlag(FileAttributes.Directory))
|
|||
|
{
|
|||
|
Assert.That(Directory.Exists(Path.Combine(destinationDirectory, entry.FullName)));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Assert.That(File.Exists(Path.Combine(destinationDirectory, entry.FullName)));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
// Test fails if we can't re-run it
|
|||
|
Assert.That(this.CleanupFiles(destinationDirectory));
|
|||
|
}
|
|||
|
|
|||
|
[Test]
|
|||
|
public void WhenCallingUnzip_WithNoDestinationDirectory_FilesAreUnzippedLocally()
|
|||
|
{
|
|||
|
// Arrange
|
|||
|
var zipPath = this.CreateRandomizedZip();
|
|||
|
var destinationDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
|
|||
|
|
|||
|
using (var zipArchive = ZipFile.OpenRead(zipPath))
|
|||
|
{
|
|||
|
// Act
|
|||
|
ZipHelpers.UnZip(zipPath);
|
|||
|
|
|||
|
destinationDirectory = Path.Combine(destinationDirectory, Path.GetFileNameWithoutExtension(zipPath));
|
|||
|
|
|||
|
// Assert
|
|||
|
foreach (var entry in zipArchive.Entries)
|
|||
|
{
|
|||
|
var attributes = File.GetAttributes(Path.Combine(destinationDirectory, entry.FullName));
|
|||
|
|
|||
|
if (attributes.HasFlag(FileAttributes.Directory))
|
|||
|
{
|
|||
|
Assert.That(Directory.Exists(Path.Combine(destinationDirectory, entry.FullName)));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Assert.That(File.Exists(Path.Combine(destinationDirectory, entry.FullName)));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// Test fails if we can't re-run it
|
|||
|
Assert.That(this.CleanupFiles(destinationDirectory));
|
|||
|
}
|
|||
|
|
|||
|
[Test]
|
|||
|
public void WhenCallingUnZipSubDirectory_WithDestinationDirectory_OnlyFilesFromSubDirectoryAreUnzippedAtDestinationLocation()
|
|||
|
{
|
|||
|
// Arrange
|
|||
|
var zipPath = this.CreateRandomizedZip();
|
|||
|
var destinationDirectory = Path.Combine(Path.GetDirectoryName(zipPath), "output");
|
|||
|
var internalDirectory = "subOne";
|
|||
|
|
|||
|
using (var zipArchive = ZipFile.OpenRead(zipPath))
|
|||
|
{
|
|||
|
Directory.CreateDirectory(destinationDirectory);
|
|||
|
|
|||
|
// Act
|
|||
|
ZipHelpers.UnZipSubDirectory(internalDirectory, zipPath, destinationDirectory);
|
|||
|
|
|||
|
// Assert
|
|||
|
foreach (var entry in zipArchive.Entries)
|
|||
|
{
|
|||
|
if (Path.GetDirectoryName(entry.FullName).Contains(internalDirectory))
|
|||
|
{
|
|||
|
var attributes = File.GetAttributes(Path.Combine(destinationDirectory, entry.FullName));
|
|||
|
|
|||
|
if (attributes.HasFlag(FileAttributes.Directory))
|
|||
|
{
|
|||
|
Assert.That(Directory.Exists(Path.Combine(destinationDirectory, entry.FullName)));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Assert.That(File.Exists(Path.Combine(destinationDirectory, entry.FullName)));
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Assert.That(File.Exists(Path.Combine(destinationDirectory, entry.Name)), Is.False);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
// Test fails if we can't re-run it
|
|||
|
Assert.That(this.CleanupFiles(destinationDirectory));
|
|||
|
}
|
|||
|
|
|||
|
[Test]
|
|||
|
public void WhenCallingUnZipSubDirectory_WithNoDestinationDirectory_OnlyFilesFromSubDirectoryAreUnzippedLocally()
|
|||
|
{
|
|||
|
// Arrange
|
|||
|
var zipPath = this.CreateRandomizedZip();
|
|||
|
var destinationDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
|
|||
|
var internalDirectory = "subOne";
|
|||
|
|
|||
|
using (var zipArchive = ZipFile.OpenRead(zipPath))
|
|||
|
{
|
|||
|
Directory.CreateDirectory(destinationDirectory);
|
|||
|
|
|||
|
// Act
|
|||
|
ZipHelpers.UnZipSubDirectory(internalDirectory, zipPath);
|
|||
|
|
|||
|
destinationDirectory = Path.Combine(destinationDirectory, Path.GetFileNameWithoutExtension(zipPath));
|
|||
|
|
|||
|
// Assert
|
|||
|
foreach (var entry in zipArchive.Entries)
|
|||
|
{
|
|||
|
if (Path.GetDirectoryName(entry.FullName).Contains(internalDirectory))
|
|||
|
{
|
|||
|
var attributes = File.GetAttributes(Path.Combine(destinationDirectory, entry.FullName));
|
|||
|
|
|||
|
if (attributes.HasFlag(FileAttributes.Directory))
|
|||
|
{
|
|||
|
Assert.That(Directory.Exists(Path.Combine(destinationDirectory, entry.FullName)));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Assert.That(File.Exists(Path.Combine(destinationDirectory, entry.FullName)));
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Assert.That(File.Exists(Path.Combine(destinationDirectory, entry.Name)), Is.False);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
// Test fails if we can't re-run it
|
|||
|
Assert.That(this.CleanupFiles(destinationDirectory));
|
|||
|
}
|
|||
|
|
|||
|
private bool CleanupFiles(string destinationDirectory)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if (Directory.Exists(this.tempPath))
|
|||
|
Directory.Delete(this.tempPath, true);
|
|||
|
if (Directory.Exists(destinationDirectory))
|
|||
|
Directory.Delete(destinationDirectory, true);
|
|||
|
if (File.Exists(Path.Combine(Path.GetDirectoryName(this.tempPath), "tempFile.zip")))
|
|||
|
File.Delete(Path.Combine(Path.GetDirectoryName(this.tempPath), "tempFile.zip"));
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Console.WriteLine("Problem cleaning up. " + ex.Message);
|
|||
|
}
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
private string CreateRandomizedZip()
|
|||
|
{
|
|||
|
// Create Directories. Top level + 2 sub directories
|
|||
|
var topLevelDirectory = Directory.CreateDirectory(this.tempPath);
|
|||
|
var subDirectoryOne = Directory.CreateDirectory(Path.Combine(this.tempPath, "subOne"));
|
|||
|
var subDirectoryTwo = Directory.CreateDirectory(Path.Combine(this.tempPath, "subTwo"));
|
|||
|
var subSubDirectory = Directory.CreateDirectory(Path.Combine(subDirectoryOne.FullName, "subSub"));
|
|||
|
|
|||
|
// Create 2 files in each directory
|
|||
|
|
|||
|
using (var handle = File.CreateText(Path.Combine(topLevelDirectory.FullName, Path.GetRandomFileName())))
|
|||
|
{
|
|||
|
handle.WriteLine("I am the first test file");
|
|||
|
handle.Flush();
|
|||
|
}
|
|||
|
|
|||
|
using (var handle = File.CreateText(Path.Combine(topLevelDirectory.FullName, Path.GetRandomFileName())))
|
|||
|
{
|
|||
|
handle.WriteLine("I am the second test file");
|
|||
|
handle.Flush();
|
|||
|
}
|
|||
|
|
|||
|
foreach (var directory in topLevelDirectory.EnumerateDirectories())
|
|||
|
{
|
|||
|
using (var handle = File.CreateText(Path.Combine(directory.FullName, Path.GetRandomFileName())))
|
|||
|
{
|
|||
|
handle.WriteLine("I am the first test file");
|
|||
|
handle.Flush();
|
|||
|
}
|
|||
|
|
|||
|
using (var handle = File.CreateText(Path.Combine(directory.FullName, Path.GetRandomFileName())))
|
|||
|
{
|
|||
|
handle.WriteLine("I am the second test file");
|
|||
|
handle.Flush();
|
|||
|
}
|
|||
|
|
|||
|
using (var handle = File.CreateText(Path.Combine(directory.FullName, "subOne.txt")))
|
|||
|
{
|
|||
|
handle.WriteLine("I am a file which matches the target directory name.");
|
|||
|
handle.Flush();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// Create a file in a secondary sub folder.
|
|||
|
using (var handle = File.CreateText(Path.Combine(subSubDirectory.FullName, Path.GetRandomFileName())))
|
|||
|
{
|
|||
|
handle.WriteLine("I am the Sub Sub test file");
|
|||
|
handle.Flush();
|
|||
|
}
|
|||
|
|
|||
|
// Create an empty sub directory.
|
|||
|
var empty = Directory.CreateDirectory(Path.Combine(subDirectoryOne.FullName, "EmptyDirectory"));
|
|||
|
|
|||
|
// Zip up the top level directory
|
|||
|
var zipPath = Path.Combine(Path.GetDirectoryName(this.tempPath), "tempFile.zip");
|
|||
|
|
|||
|
if (File.Exists(zipPath))
|
|||
|
{
|
|||
|
File.Delete(zipPath);
|
|||
|
}
|
|||
|
|
|||
|
ZipFile.CreateFromDirectory(this.tempPath, zipPath);
|
|||
|
|
|||
|
return zipPath;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|