Skip to content

Commit

Permalink
Add ZipUtilitiesTests
Browse files Browse the repository at this point in the history
  • Loading branch information
vbreuss committed Apr 9, 2024
1 parent e9bffe9 commit e90c7ce
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,7 @@ internal static void CreateFromDirectory(
ArgumentNullException.ThrowIfNull(destination);
if (!destination.CanWrite)
{
throw new ArgumentException("The stream is unwritable.", nameof(destination))
{
HResult = -2147024809
};
throw new ArgumentException("The stream is unwritable.", nameof(destination));
}

sourceDirectoryName = fileSystem.Path.GetFullPath(sourceDirectoryName);
Expand Down Expand Up @@ -275,10 +272,7 @@ internal static void ExtractToDirectory(IFileSystem fileSystem,
ArgumentNullException.ThrowIfNull(source);
if (!source.CanRead)
{
throw new ArgumentException("The stream is unreadable.", nameof(source))
{
HResult = -2147024809
};
throw new ArgumentException("The stream is unreadable.", nameof(source));
}

using (ZipArchive archive = new(source, ZipArchiveMode.Read, true, entryNameEncoding))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System.IO;
using Testably.Abstractions.Internal;

namespace Testably.Abstractions.Compression.Tests.Internal;

public sealed class ZipUtilitiesTests
{
[Theory]
[AutoData]
public void ExtractRelativeToDirectory_FileWithTrailingSlash_ShouldThrowIOException(
byte[] bytes)
{
MockFileSystem fileSystem = new();
using MemoryStream stream = new(bytes);
DummyZipArchiveEntry zipArchiveEntry = new(fileSystem, fullName: "foo/", stream: stream);

Exception? exception = Record.Exception(() =>
{
zipArchiveEntry.ExtractRelativeToDirectory("foo", false);
});

exception.Should().BeException<IOException>(
messageContains:
"Zip entry name ends in directory separator character but contains data");
}

[Fact]
public void ExtractRelativeToDirectory_WithSubdirectory_ShouldCreateSubdirectory()
{
MockFileSystem fileSystem = new();
DummyZipArchiveEntry zipArchiveEntry = new(fileSystem, fullName: "foo/");

zipArchiveEntry.ExtractRelativeToDirectory("bar", false);

fileSystem.Directory.Exists("bar").Should().BeTrue();
fileSystem.Directory.Exists("bar/foo").Should().BeTrue();
}

public class DummyZipArchiveEntry(
IFileSystem fileSystem,
IZipArchive? archive = null,
string? fullName = "",

Check warning on line 42 in Tests/Testably.Abstractions.Compression.Tests/Internal/ZipUtilitiesTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Tests/Testably.Abstractions.Compression.Tests/Internal/ZipUtilitiesTests.cs#L42

Make 'fullName' 'readonly'.
string? name = "",

Check warning on line 43 in Tests/Testably.Abstractions.Compression.Tests/Internal/ZipUtilitiesTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Tests/Testably.Abstractions.Compression.Tests/Internal/ZipUtilitiesTests.cs#L43

Make 'name' 'readonly'.
string comment = "",

Check warning on line 44 in Tests/Testably.Abstractions.Compression.Tests/Internal/ZipUtilitiesTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Tests/Testably.Abstractions.Compression.Tests/Internal/ZipUtilitiesTests.cs#L44

Make 'comment' 'readonly'.
bool isEncrypted = false,

Check warning on line 45 in Tests/Testably.Abstractions.Compression.Tests/Internal/ZipUtilitiesTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Tests/Testably.Abstractions.Compression.Tests/Internal/ZipUtilitiesTests.cs#L45

Make 'isEncrypted' 'readonly'.

Check notice on line 45 in Tests/Testably.Abstractions.Compression.Tests/Internal/ZipUtilitiesTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Tests/Testably.Abstractions.Compression.Tests/Internal/ZipUtilitiesTests.cs#L45

Remove this initialization to 'isEncrypted', the compiler will do that for you.
Stream? stream = null)
: IZipArchiveEntry
{
#region IZipArchiveEntry Members

/// <inheritdoc cref="IZipArchiveEntry.Archive" />
public IZipArchive Archive => archive ?? throw new NotSupportedException();

/// <inheritdoc cref="IZipArchiveEntry.Comment" />
public string Comment { get; set; } = comment;

/// <inheritdoc cref="IZipArchiveEntry.CompressedLength" />
public long CompressedLength => stream?.Length ?? 0L;

/// <inheritdoc cref="IZipArchiveEntry.Crc32" />
public uint Crc32 => 0u;

/// <inheritdoc cref="IZipArchiveEntry.ExternalAttributes" />
public int ExternalAttributes { get; set; }

/// <inheritdoc cref="IFileSystemEntity.FileSystem" />
public IFileSystem FileSystem { get; } = fileSystem;

/// <inheritdoc cref="IZipArchiveEntry.FullName" />
public string FullName { get; } = fullName ?? "";

/// <inheritdoc cref="IZipArchiveEntry.IsEncrypted" />
public bool IsEncrypted { get; } = isEncrypted;

/// <inheritdoc cref="IZipArchiveEntry.LastWriteTime" />
public DateTimeOffset LastWriteTime { get; set; }

/// <inheritdoc cref="IZipArchiveEntry.Length" />
public long Length => stream?.Length ?? 0L;

/// <inheritdoc cref="IZipArchiveEntry.Name" />
public string Name { get; } = name ?? "";

/// <inheritdoc cref="IZipArchiveEntry.Delete()" />
public void Delete()
=> throw new NotSupportedException();

/// <inheritdoc cref="IZipArchiveEntry.ExtractToFile(string)" />
public void ExtractToFile(string destinationFileName)
=> throw new NotSupportedException();

/// <inheritdoc cref="IZipArchiveEntry.ExtractToFile(string, bool)" />
public void ExtractToFile(string destinationFileName, bool overwrite)
=> throw new NotSupportedException();

/// <inheritdoc cref="IZipArchiveEntry.Open()" />
public Stream Open()
=> stream ?? throw new NotSupportedException();

#endregion
}
}

0 comments on commit e90c7ce

Please sign in to comment.