From 1003e3ca2c766fd6bec3a95e485fa14e0072a08c Mon Sep 17 00:00:00 2001 From: Ian Hays Date: Wed, 29 Jun 2016 14:51:30 -0700 Subject: [PATCH] Cleanup ZipFile tests using temp files Some ZipFile tests aren't cleaning up after themselves like good little tests are supposed to. This commit makes them use the FileCleanupTestBase/TempFile/TempDirectory common files to resolve that Commit migrated from https://github.com/dotnet/corefx/commit/d2927a88b8fd313bcd1587a26c1e94fb8762ce1f --- .../tests/System/IO/Compression/ZipTestHelper.cs | 9 ++- .../System.IO.Compression.ZipFile.Tests.csproj | 14 +++- .../tests/ZipFileConvenienceMethods.cs | 93 ++++++++++++---------- .../tests/ZipFileInvalidFileTests.cs | 85 ++++++++++---------- .../tests/ZipFileReadOpenUpdateTests.cs | 43 +++++----- .../tests/ZipFileTests.cs | 48 ----------- 6 files changed, 131 insertions(+), 161 deletions(-) delete mode 100644 src/libraries/System.IO.Compression.ZipFile/tests/ZipFileTests.cs diff --git a/src/libraries/Common/tests/System/IO/Compression/ZipTestHelper.cs b/src/libraries/Common/tests/System/IO/Compression/ZipTestHelper.cs index 896062c..3fa59a1 100644 --- a/src/libraries/Common/tests/System/IO/Compression/ZipTestHelper.cs +++ b/src/libraries/Common/tests/System/IO/Compression/ZipTestHelper.cs @@ -10,7 +10,7 @@ using Xunit; namespace System.IO.Compression.Tests { - public partial class ZipTest + public partial class ZipFileTestBase : FileCleanupTestBase { #region filename helpers @@ -25,6 +25,13 @@ namespace System.IO.Compression.Tests #region helpers + protected TempFile CreateTempCopyFile(string path, string newPath) + { + TempFile newfile = new TempFile(newPath); + File.Copy(path, newPath, overwrite: true); + return newfile; + } + public static Int64 LengthOfUnseekableStream(Stream s) { Int64 totalBytes = 0; diff --git a/src/libraries/System.IO.Compression.ZipFile/tests/System.IO.Compression.ZipFile.Tests.csproj b/src/libraries/System.IO.Compression.ZipFile/tests/System.IO.Compression.ZipFile.Tests.csproj index 904cfce..48f1462 100644 --- a/src/libraries/System.IO.Compression.ZipFile/tests/System.IO.Compression.ZipFile.Tests.csproj +++ b/src/libraries/System.IO.Compression.ZipFile/tests/System.IO.Compression.ZipFile.Tests.csproj @@ -1,4 +1,4 @@ - + @@ -20,9 +20,17 @@ - + + Common\System\IO\FileCleanupTestBase.cs + + + Common\System\IO\TempFile.cs + + + Common\System\IO\TempDirectory.cs + Common\System\IO\Compression\CRC.cs @@ -46,4 +54,4 @@ - + \ No newline at end of file diff --git a/src/libraries/System.IO.Compression.ZipFile/tests/ZipFileConvenienceMethods.cs b/src/libraries/System.IO.Compression.ZipFile/tests/ZipFileConvenienceMethods.cs index da0bfb9..7b0bed4 100644 --- a/src/libraries/System.IO.Compression.ZipFile/tests/ZipFileConvenienceMethods.cs +++ b/src/libraries/System.IO.Compression.ZipFile/tests/ZipFileConvenienceMethods.cs @@ -10,7 +10,7 @@ using Xunit.NetCore.Extensions; namespace System.IO.Compression.Tests { - public partial class ZipTest + public class ZipFileTest_ConvenienceMethods : ZipFileTestBase { [Fact] public async Task CreateFromDirectoryNormal() @@ -27,14 +27,14 @@ namespace System.IO.Compression.Tests private async Task TestCreateDirectory(string folderName, Boolean testWithBaseDir) { - string noBaseDir = GetTmpFilePath(); + string noBaseDir = GetTestFilePath(); ZipFile.CreateFromDirectory(folderName, noBaseDir); await IsZipSameAsDirAsync(noBaseDir, folderName, ZipArchiveMode.Read, true, true); if (testWithBaseDir) { - string withBaseDir = GetTmpFilePath(); + string withBaseDir = GetTestFilePath(); ZipFile.CreateFromDirectory(folderName, withBaseDir, CompressionLevel.Optimal, true); SameExceptForBaseDir(noBaseDir, withBaseDir, folderName); } @@ -88,11 +88,13 @@ namespace System.IO.Compression.Tests private void TestExtract(string zipFileName, string folderName) { - string tempFolder = GetTmpDirPath(true); - ZipFile.ExtractToDirectory(zipFileName, tempFolder); - DirsEqual(tempFolder, folderName); + using (var tempFolder = new TempDirectory(GetTestFilePath())) + { + ZipFile.ExtractToDirectory(zipFileName, tempFolder.Path); + DirsEqual(tempFolder.Path, folderName); - Assert.Throws(() => ZipFile.ExtractToDirectory(null, tempFolder)); + Assert.Throws(() => ZipFile.ExtractToDirectory(null, tempFolder.Path)); + } } #region "Extension Methods" @@ -103,24 +105,24 @@ namespace System.IO.Compression.Tests public async Task CreateEntryFromFileTest(bool withCompressionLevel) { //add file - string testArchive = CreateTempCopyFile(zfile("normal.zip")); - - using (ZipArchive archive = ZipFile.Open(testArchive, ZipArchiveMode.Update)) + using (TempFile testArchive = CreateTempCopyFile(zfile("normal.zip"), GetTestFilePath())) { - string entryName = "added.txt"; - string sourceFilePath = zmodified(Path.Combine("addFile", entryName)); + using (ZipArchive archive = ZipFile.Open(testArchive.Path, ZipArchiveMode.Update)) + { + string entryName = "added.txt"; + string sourceFilePath = zmodified(Path.Combine("addFile", entryName)); - Assert.Throws(() => ((ZipArchive)null).CreateEntryFromFile(sourceFilePath, entryName)); - Assert.Throws(() => archive.CreateEntryFromFile(null, entryName)); - Assert.Throws(() => archive.CreateEntryFromFile(sourceFilePath, null)); + Assert.Throws(() => ((ZipArchive)null).CreateEntryFromFile(sourceFilePath, entryName)); + Assert.Throws(() => archive.CreateEntryFromFile(null, entryName)); + Assert.Throws(() => archive.CreateEntryFromFile(sourceFilePath, null)); - ZipArchiveEntry e = withCompressionLevel ? - archive.CreateEntryFromFile(sourceFilePath, entryName) : - archive.CreateEntryFromFile(sourceFilePath, entryName, CompressionLevel.Fastest); - Assert.NotNull(e); + ZipArchiveEntry e = withCompressionLevel ? + archive.CreateEntryFromFile(sourceFilePath, entryName) : + archive.CreateEntryFromFile(sourceFilePath, entryName, CompressionLevel.Fastest); + Assert.NotNull(e); + } + await IsZipSameAsDirAsync(testArchive.Path, zmodified("addFile"), ZipArchiveMode.Read, true, true); } - - await IsZipSameAsDirAsync(testArchive, zmodified("addFile"), ZipArchiveMode.Read, true, true); } [Fact] @@ -128,7 +130,7 @@ namespace System.IO.Compression.Tests { using (ZipArchive archive = ZipFile.Open(zfile("normal.zip"), ZipArchiveMode.Read)) { - string file = GetTmpFilePath(); + string file = GetTestFilePath(); ZipArchiveEntry e = archive.GetEntry("first.txt"); Assert.Throws(() => ((ZipArchiveEntry)null).ExtractToFile(file)); @@ -162,7 +164,7 @@ namespace System.IO.Compression.Tests { using (ZipArchive archive = ZipFile.Open(zfile("normal.zip"), ZipArchiveMode.Read)) { - string tempFolder = GetTmpDirPath(false); + string tempFolder = GetTestFilePath(); Assert.Throws(() => ((ZipArchive)null).ExtractToDirectory(tempFolder)); Assert.Throws(() => archive.ExtractToDirectory(null)); archive.ExtractToDirectory(tempFolder); @@ -177,7 +179,7 @@ namespace System.IO.Compression.Tests { using (ZipArchive archive = ZipFile.OpenRead(zfile("unicode.zip"))) { - string tempFolder = GetTmpDirPath(false); + string tempFolder = GetTestFilePath(); archive.ExtractToDirectory(tempFolder); DirsEqual(tempFolder, zfolder("unicode")); @@ -187,34 +189,39 @@ namespace System.IO.Compression.Tests [Fact] public void CreatedEmptyDirectoriesRoundtrip() { - DirectoryInfo rootDir = new DirectoryInfo(GetTmpDirPath(create: true)); - rootDir.CreateSubdirectory("empty1"); + using (var tempFolder = new TempDirectory(GetTestFilePath())) + { + DirectoryInfo rootDir = new DirectoryInfo(tempFolder.Path); + rootDir.CreateSubdirectory("empty1"); - string archivePath = GetTmpFilePath(); - ZipFile.CreateFromDirectory( - rootDir.FullName, archivePath, - CompressionLevel.Optimal, false, Encoding.UTF8); + string archivePath = GetTestFilePath(); + ZipFile.CreateFromDirectory( + rootDir.FullName, archivePath, + CompressionLevel.Optimal, false, Encoding.UTF8); - using (ZipArchive archive = ZipFile.OpenRead(archivePath)) - { - Assert.Equal(1, archive.Entries.Count); - Assert.True(archive.Entries[0].FullName.StartsWith("empty1")); + using (ZipArchive archive = ZipFile.OpenRead(archivePath)) + { + Assert.Equal(1, archive.Entries.Count); + Assert.True(archive.Entries[0].FullName.StartsWith("empty1")); + } } } [Fact] public void CreatedEmptyRootDirectoryRoundtrips() { - DirectoryInfo emptyRoot = new DirectoryInfo(GetTmpDirPath(create: true)); - - string archivePath = GetTmpFilePath(); - ZipFile.CreateFromDirectory( - emptyRoot.FullName, archivePath, - CompressionLevel.Optimal, true); - - using (ZipArchive archive = ZipFile.OpenRead(archivePath)) + using (var tempFolder = new TempDirectory(GetTestFilePath())) { - Assert.Equal(1, archive.Entries.Count); + DirectoryInfo emptyRoot = new DirectoryInfo(tempFolder.Path); + string archivePath = GetTestFilePath(); + ZipFile.CreateFromDirectory( + emptyRoot.FullName, archivePath, + CompressionLevel.Optimal, true); + + using (ZipArchive archive = ZipFile.OpenRead(archivePath)) + { + Assert.Equal(1, archive.Entries.Count); + } } } diff --git a/src/libraries/System.IO.Compression.ZipFile/tests/ZipFileInvalidFileTests.cs b/src/libraries/System.IO.Compression.ZipFile/tests/ZipFileInvalidFileTests.cs index 10c61ae..67ae6ad 100644 --- a/src/libraries/System.IO.Compression.ZipFile/tests/ZipFileInvalidFileTests.cs +++ b/src/libraries/System.IO.Compression.ZipFile/tests/ZipFileInvalidFileTests.cs @@ -6,7 +6,7 @@ using Xunit; namespace System.IO.Compression.Tests { - public partial class ZipTest + public partial class ZipFileTest_Invalid : ZipFileTestBase { private static void ConstructorThrows(Func constructor, string Message = "") where TException : Exception { @@ -19,8 +19,8 @@ namespace System.IO.Compression.Tests [Fact] public void InvalidInstanceMethods() { - string zipFileName = CreateTempCopyFile(zfile("normal.zip")); - using (ZipArchive archive = ZipFile.Open(zipFileName, ZipArchiveMode.Update)) + using (TempFile testArchive = CreateTempCopyFile(zfile("normal.zip"), GetTestFilePath())) + using (ZipArchive archive = ZipFile.Open(testArchive.Path, ZipArchiveMode.Update)) { //non-existent entry Assert.True(null == archive.GetEntry("nonExistentEntry")); @@ -97,17 +97,12 @@ namespace System.IO.Compression.Tests ZipFile.Open(bad("localFileHeaderSignatureWrong.zip"), ZipArchiveMode.Update)); } - [Fact] - public void UnsupportedCompression() - { - //lzma compression method - UnsupportedCompressionRoutine(bad("LZMA.zip"), true); - - UnsupportedCompressionRoutine(bad("invalidDeflate.zip"), false); - } - - private void UnsupportedCompressionRoutine(string filename, Boolean throwsOnOpen) + [Theory] + [InlineData("LZMA.zip", true)] + [InlineData("invalidDeflate.zip", false)] + public void UnsupportedCompressionRoutine(string zipName, Boolean throwsOnOpen) { + string filename = bad(zipName); using (ZipArchive archive = ZipFile.OpenRead(filename)) { ZipArchiveEntry e = archive.Entries[0]; @@ -124,29 +119,31 @@ namespace System.IO.Compression.Tests } } - string updatedCopyName = CreateTempCopyFile(filename); - string name; - Int64 length, compressedLength; - DateTimeOffset lastWriteTime; - using (ZipArchive archive = ZipFile.Open(updatedCopyName, ZipArchiveMode.Update)) + using (TempFile updatedCopy = CreateTempCopyFile(filename, GetTestFilePath())) { - ZipArchiveEntry e = archive.Entries[0]; - name = e.FullName; - lastWriteTime = e.LastWriteTime; - length = e.Length; - compressedLength = e.CompressedLength; - Assert.Throws(() => e.Open()); - } + string name; + Int64 length, compressedLength; + DateTimeOffset lastWriteTime; + using (ZipArchive archive = ZipFile.Open(updatedCopy.Path, ZipArchiveMode.Update)) + { + ZipArchiveEntry e = archive.Entries[0]; + name = e.FullName; + lastWriteTime = e.LastWriteTime; + length = e.Length; + compressedLength = e.CompressedLength; + Assert.Throws(() => e.Open()); + } - //make sure that update mode preserves that unreadable file - using (ZipArchive archive = ZipFile.Open(updatedCopyName, ZipArchiveMode.Update)) - { - ZipArchiveEntry e = archive.Entries[0]; - Assert.Equal(name, e.FullName); - Assert.Equal(lastWriteTime, e.LastWriteTime); - Assert.Equal(length, e.Length); - Assert.Equal(compressedLength, e.CompressedLength); - Assert.Throws(() => e.Open()); + //make sure that update mode preserves that unreadable file + using (ZipArchive archive = ZipFile.Open(updatedCopy.Path, ZipArchiveMode.Update)) + { + ZipArchiveEntry e = archive.Entries[0]; + Assert.Equal(name, e.FullName); + Assert.Equal(lastWriteTime, e.LastWriteTime); + Assert.Equal(length, e.Length); + Assert.Equal(compressedLength, e.CompressedLength); + Assert.Throws(() => e.Open()); + } } } @@ -158,11 +155,11 @@ namespace System.IO.Compression.Tests Assert.Equal(new DateTime(1980, 1, 1, 0, 0, 0), archive.Entries[0].LastWriteTime.DateTime); } - FileInfo fileWithBadDate = new FileInfo(GetTmpFilePath()); + FileInfo fileWithBadDate = new FileInfo(GetTestFilePath()); fileWithBadDate.Create().Dispose(); fileWithBadDate.LastWriteTimeUtc = new DateTime(1970, 1, 1, 1, 1, 1); - string archivePath = GetTmpFilePath(); + string archivePath = GetTestFilePath(); using (FileStream output = File.Open(archivePath, FileMode.Create)) using (ZipArchive archive = new ZipArchive(output, ZipArchiveMode.Create)) { @@ -177,25 +174,25 @@ namespace System.IO.Compression.Tests [Fact] public void FilesOutsideDirectory() { - string archivePath = GetTmpFilePath(); + string archivePath = GetTestFilePath(); using (ZipArchive archive = ZipFile.Open(archivePath, ZipArchiveMode.Create)) using (StreamWriter writer = new StreamWriter(archive.CreateEntry(Path.Combine("..", "entry1"), CompressionLevel.Optimal).Open())) { writer.Write("This is a test."); } - Assert.Throws(() => ZipFile.ExtractToDirectory(archivePath, GetTmpDirPath())); + Assert.Throws(() => ZipFile.ExtractToDirectory(archivePath, GetTestFilePath())); } [Fact] public void DirectoryEntryWithData() { - string archivePath = GetTmpFilePath(); + string archivePath = GetTestFilePath(); using (ZipArchive archive = ZipFile.Open(archivePath, ZipArchiveMode.Create)) using (StreamWriter writer = new StreamWriter(archive.CreateEntry("testdir" + Path.DirectorySeparatorChar, CompressionLevel.Optimal).Open())) { writer.Write("This is a test."); } - Assert.Throws(() => ZipFile.ExtractToDirectory(archivePath, GetTmpDirPath())); + Assert.Throws(() => ZipFile.ExtractToDirectory(archivePath, GetTestFilePath())); } /// @@ -208,7 +205,7 @@ namespace System.IO.Compression.Tests [PlatformSpecific(PlatformID.AnyUnix)] public void Unix_ZipWithInvalidFileNames_ThrowsArgumentException(string zipName) { - Assert.Throws(() => ZipFile.ExtractToDirectory(compat(zipName) + ".zip", GetTmpDirPath())); + Assert.Throws(() => ZipFile.ExtractToDirectory(compat(zipName) + ".zip", GetTestFilePath())); } [Theory] @@ -219,7 +216,7 @@ namespace System.IO.Compression.Tests [PlatformSpecific(PlatformID.AnyUnix)] public void Unix_ZipWithOSSpecificFileNames(string zipName, string fileName) { - string tempDir = GetTmpDirPath(); + string tempDir = GetTestFilePath(); ZipFile.ExtractToDirectory(compat(zipName) + ".zip", tempDir); string[] results = Directory.GetFiles(tempDir, "*", SearchOption.AllDirectories); Assert.Equal(1, results.Length); @@ -238,7 +235,7 @@ namespace System.IO.Compression.Tests [PlatformSpecific(PlatformID.Windows)] public void Windows_ZipWithInvalidFileNames_ThrowsArgumentException(string zipName) { - Assert.Throws(() => ZipFile.ExtractToDirectory(compat(zipName) + ".zip", GetTmpDirPath())); + Assert.Throws(() => ZipFile.ExtractToDirectory(compat(zipName) + ".zip", GetTestFilePath())); } [Theory] @@ -247,7 +244,7 @@ namespace System.IO.Compression.Tests [PlatformSpecific(PlatformID.Windows)] public void Windows_ZipWithOSSpecificFileNames(string zipName, string fileName) { - string tempDir = GetTmpDirPath(); + string tempDir = GetTestFilePath(); ZipFile.ExtractToDirectory(compat(zipName) + ".zip", tempDir); string[] results = Directory.GetFiles(tempDir, "*", SearchOption.AllDirectories); Assert.Equal(1, results.Length); diff --git a/src/libraries/System.IO.Compression.ZipFile/tests/ZipFileReadOpenUpdateTests.cs b/src/libraries/System.IO.Compression.ZipFile/tests/ZipFileReadOpenUpdateTests.cs index 13bf353..bd5b970 100644 --- a/src/libraries/System.IO.Compression.ZipFile/tests/ZipFileReadOpenUpdateTests.cs +++ b/src/libraries/System.IO.Compression.ZipFile/tests/ZipFileReadOpenUpdateTests.cs @@ -7,7 +7,7 @@ using Xunit; namespace System.IO.Compression.Tests { - public partial class ZipTest + public partial class ZipFileTest_ReadOpenUpdate : ZipFileTestBase { [Fact] public void ReadStreamOps() @@ -50,39 +50,38 @@ namespace System.IO.Compression.Tests public async Task UpdateAddFile() { //add file - string testArchive = CreateTempCopyFile(zfile("normal.zip")); - - using (ZipArchive archive = ZipFile.Open(testArchive, ZipArchiveMode.Update)) + using (TempFile testArchive = CreateTempCopyFile(zfile("normal.zip"), GetTestFilePath())) { - await UpdateArchive(archive, zmodified(Path.Combine("addFile", "added.txt")), "added.txt"); + using (ZipArchive archive = ZipFile.Open(testArchive.Path, ZipArchiveMode.Update)) + { + await UpdateArchive(archive, zmodified(Path.Combine("addFile", "added.txt")), "added.txt"); + } + await IsZipSameAsDirAsync(testArchive.Path, zmodified("addFile"), ZipArchiveMode.Read); } - await IsZipSameAsDirAsync(testArchive, zmodified("addFile"), ZipArchiveMode.Read); - //add file and read entries before - testArchive = CreateTempCopyFile(zfile("normal.zip")); - - using (ZipArchive archive = ZipFile.Open(testArchive, ZipArchiveMode.Update)) + using (TempFile testArchive = CreateTempCopyFile(zfile("normal.zip"), GetTestFilePath())) { - var x = archive.Entries; + using (ZipArchive archive = ZipFile.Open(testArchive.Path, ZipArchiveMode.Update)) + { + var x = archive.Entries; - await UpdateArchive(archive, zmodified(Path.Combine("addFile", "added.txt")), "added.txt"); + await UpdateArchive(archive, zmodified(Path.Combine("addFile", "added.txt")), "added.txt"); + } + await IsZipSameAsDirAsync(testArchive.Path, zmodified("addFile"), ZipArchiveMode.Read); } - await IsZipSameAsDirAsync(testArchive, zmodified("addFile"), ZipArchiveMode.Read); - - //add file and read entries after - testArchive = CreateTempCopyFile(zfile("normal.zip")); - - using (ZipArchive archive = ZipFile.Open(testArchive, ZipArchiveMode.Update)) + using (TempFile testArchive = CreateTempCopyFile(zfile("normal.zip"), GetTestFilePath())) { - await UpdateArchive(archive, zmodified(Path.Combine("addFile", "added.txt")), "added.txt"); + using (ZipArchive archive = ZipFile.Open(testArchive.Path, ZipArchiveMode.Update)) + { + await UpdateArchive(archive, zmodified(Path.Combine("addFile", "added.txt")), "added.txt"); - var x = archive.Entries; + var x = archive.Entries; + } + await IsZipSameAsDirAsync(testArchive.Path, zmodified("addFile"), ZipArchiveMode.Read); } - - await IsZipSameAsDirAsync(testArchive, zmodified("addFile"), ZipArchiveMode.Read); } private static async Task UpdateArchive(ZipArchive archive, string installFile, string entryName) diff --git a/src/libraries/System.IO.Compression.ZipFile/tests/ZipFileTests.cs b/src/libraries/System.IO.Compression.ZipFile/tests/ZipFileTests.cs deleted file mode 100644 index 9e6f64e..0000000 --- a/src/libraries/System.IO.Compression.ZipFile/tests/ZipFileTests.cs +++ /dev/null @@ -1,48 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -namespace System.IO.Compression.Tests -{ - public partial class ZipTest : IDisposable - { - private const string DirPrefix = "ZipTests"; - - private readonly DirectoryInfo _rootTestDirectory; - private int _dirCount = 1; - - public ZipTest() - { - _rootTestDirectory = new DirectoryInfo(Path.Combine(Path.GetTempPath(), - DirPrefix + "Root_" + Guid.NewGuid().ToString("N"))); - _rootTestDirectory.Create(); - } - - public void Dispose() - { - _rootTestDirectory.Delete(recursive: true); - } - - private string GetTmpFilePath() - { - return Path.Combine(_rootTestDirectory.FullName, "tmp" + Guid.NewGuid().ToString("N")); - } - - private string GetTmpDirPath(bool create = false) - { - string tempPath = Path.Combine(_rootTestDirectory.FullName, DirPrefix + _dirCount++); - if (create) - { - Directory.CreateDirectory(tempPath); - } - return tempPath; - } - - private string CreateTempCopyFile(string path) - { - string newPath = GetTmpFilePath(); - File.Copy(path, newPath, overwrite: false); - return newPath; - } - } -} -- 2.7.4