Files
dotnet-runtime/src/libraries/System.IO.Compression.ZipFile/tests/ZipFileReadOpenUpdateTests.cs
T
stephentoub 8cf85d2d52 Modify ZipFile tests to remove temporary garbage
The System.IO.Compression.ZipFile tests were leaving behind lots of temporary files and directories.  I've modified the tests to delete these when the tests completes.

As long as I was modifying the tests, I also boosted the code coverage of ZipFile and ZipFileExtensions to 100% (the library itself isn't at 100% because of the SR file and some unused code in a shared common file).

And I fixed up a few "String"=>"string"s I saw along the way.


Commit migrated from https://github.com/dotnet/corefx/commit/28d96af2ccd6d1f2e3f0ce4788fc8ec21fd15a76
2015-06-06 18:32:55 -04:00

107 lines
3.6 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Threading.Tasks;
using Xunit;
namespace System.IO.Compression.Test
{
public partial class ZipTest
{
[Fact]
public void ReadStreamOps()
{
using (ZipArchive archive = ZipFile.OpenRead(zfile("normal.zip")))
{
foreach (ZipArchiveEntry e in archive.Entries)
{
using (Stream s = e.Open())
{
Assert.True(s.CanRead, "Can read to read archive");
Assert.False(s.CanWrite, "Can't write to read archive");
Assert.False(s.CanSeek, "Can't seek on archive");
Assert.Equal(LengthOfUnseekableStream(s), e.Length);
}
}
}
}
[Fact]
public void UpdateReadTwice()
{
using (ZipArchive archive = ZipFile.Open(zfile("small.zip"), ZipArchiveMode.Update))
{
ZipArchiveEntry entry = archive.Entries[0];
string contents1, contents2;
using (StreamReader s = new StreamReader(entry.Open()))
{
contents1 = s.ReadToEnd();
}
using (StreamReader s = new StreamReader(entry.Open()))
{
contents2 = s.ReadToEnd();
}
Assert.Equal(contents1, contents2);
}
}
[Fact]
public async Task UpdateAddFile()
{
//add file
string testArchive = CreateTempCopyFile(zfile("normal.zip"));
using (ZipArchive archive = ZipFile.Open(testArchive, ZipArchiveMode.Update))
{
await UpdateArchive(archive, zmodified(Path.Combine("addFile", "added.txt")), "added.txt");
}
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))
{
var x = archive.Entries;
await UpdateArchive(archive, zmodified(Path.Combine("addFile", "added.txt")), "added.txt");
}
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))
{
await UpdateArchive(archive, zmodified(Path.Combine("addFile", "added.txt")), "added.txt");
var x = archive.Entries;
}
await IsZipSameAsDirAsync(testArchive, zmodified("addFile"), ZipArchiveMode.Read);
}
private static async Task UpdateArchive(ZipArchive archive, string installFile, string entryName)
{
string fileName = installFile;
ZipArchiveEntry e = archive.CreateEntry(entryName);
var file = FileData.GetFile(fileName);
e.LastWriteTime = file.LastModifiedDate;
using (var stream = await StreamHelpers.CreateTempCopyStream(fileName))
{
using (Stream es = e.Open())
{
es.SetLength(0);
stream.CopyTo(es);
}
}
}
}
}