NetCoreToolService/test/NetCoreToolService.Test/Packagers/ZipPackagerTests.cs

135 строки
4.5 KiB
C#
Исходник Обычный вид История

2021-06-10 19:52:11 +03:00
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.IO.Compression;
using FluentAssertions;
2021-06-15 19:32:24 +03:00
using Steeltoe.Common.Utils.IO;
using Steeltoe.NetCoreToolService.Packagers;
2021-06-10 19:52:11 +03:00
using Xunit;
2021-06-15 19:32:24 +03:00
namespace Steeltoe.NetCoreToolService.Test.Packagers
2021-06-10 19:52:11 +03:00
{
2021-06-15 19:32:24 +03:00
public class ZipPackagerTests
2021-06-10 19:52:11 +03:00
{
/* ----------------------------------------------------------------- *
* positive tests *
* ----------------------------------------------------------------- */
[Fact]
public void ToStream_Should_Create_Zip_Archive()
{
// Arrange
2021-06-15 19:32:24 +03:00
var archiver = new ZipPackager();
2021-06-10 19:52:11 +03:00
var tempDir = new TempDirectory();
// Act
2021-06-15 19:32:24 +03:00
var buf = archiver.ToBytes(tempDir.FullPath);
2021-06-10 19:52:11 +03:00
// Assert
new ZipArchive(new MemoryStream(buf)).Should().BeOfType<ZipArchive>();
}
[Fact]
public void ToStream_Should_Archive_File_Contents()
{
// Arrange
2021-06-15 19:32:24 +03:00
var archiver = new ZipPackager();
2021-06-10 19:52:11 +03:00
using var tempDir = new TempDirectory();
2021-06-15 19:32:24 +03:00
var d1 = Path.Join(tempDir.FullPath, "d1");
2021-06-10 19:52:11 +03:00
Directory.CreateDirectory(d1);
var f1 = Path.Join(d1, "f1");
File.WriteAllText(f1, "f1 stuff");
// Act
2021-06-15 19:32:24 +03:00
var buf = archiver.ToBytes(tempDir.FullPath);
2021-06-10 19:52:11 +03:00
// Assert
var zip = new ZipArchive(new MemoryStream(buf));
using var entries = zip.Entries.GetEnumerator();
entries.MoveNext().Should().BeTrue();
Assert.NotNull(entries.Current);
entries.Current.FullName.Should().Be($"d1{Path.DirectorySeparatorChar}");
2021-06-10 19:52:11 +03:00
entries.MoveNext().Should().BeTrue();
Assert.NotNull(entries.Current);
entries.Current.Name.Should().Be("f1");
entries.Current.FullName.Should().Be($"d1{Path.DirectorySeparatorChar}f1");
2021-06-10 19:52:11 +03:00
using var reader = new StreamReader(entries.Current.Open());
reader.ReadToEnd().Should().Be("f1 stuff");
entries.MoveNext().Should().BeFalse();
}
[Fact]
public void ToStream_Should_Archive_Directories()
{
// Arrange
2021-06-15 19:32:24 +03:00
var archiver = new ZipPackager();
2021-06-10 19:52:11 +03:00
using var tempDir = new TempDirectory();
2021-06-15 19:32:24 +03:00
var d1 = Path.Join(tempDir.FullPath, "d1");
2021-06-10 19:52:11 +03:00
Directory.CreateDirectory(d1);
var d2 = Path.Join(d1, "d2");
Directory.CreateDirectory(d2);
// Act
2021-06-15 19:32:24 +03:00
var buf = archiver.ToBytes(tempDir.FullPath);
2021-06-10 19:52:11 +03:00
// Assert
var zip = new ZipArchive(new MemoryStream(buf));
using var entries = zip.Entries.GetEnumerator();
entries.MoveNext().Should().BeTrue();
Assert.NotNull(entries.Current);
entries.Current.FullName.Should().Be($"d1{Path.DirectorySeparatorChar}");
2021-06-10 19:52:11 +03:00
using var reader = new StreamReader(entries.Current.Open());
reader.ReadToEnd().Should().BeEmpty();
entries.MoveNext().Should().BeTrue();
Assert.NotNull(entries.Current);
entries.Current.FullName.Should().Be($"d1{Path.DirectorySeparatorChar}d2{Path.DirectorySeparatorChar}");
2021-06-10 19:52:11 +03:00
entries.MoveNext().Should().BeFalse();
}
[Fact]
public void GetPackaging_Should_Be_application_zip()
{
// Arrange
2021-06-15 19:32:24 +03:00
var archiver = new ZipPackager();
2021-06-10 19:52:11 +03:00
// Act
var packaging = archiver.Name;
// Assert
packaging.Should().Be("zip");
}
[Fact]
public void GetFileExtension_Should_Be_zip()
{
// Arrange
2021-06-15 19:32:24 +03:00
var archiver = new ZipPackager();
2021-06-10 19:52:11 +03:00
// Act
var ext = archiver.FileExtension;
// Assert
ext.Should().Be(".zip");
}
[Fact]
public void MimeType_Should_Be_application_zip()
{
// Arrange
2021-06-15 19:32:24 +03:00
var archiver = new ZipPackager();
2021-06-10 19:52:11 +03:00
// Act
var ext = archiver.MimeType;
// Assert
ext.Should().Be("application/zip");
}
/* ----------------------------------------------------------------- *
* negative tests *
* ----------------------------------------------------------------- */
}
}