Skip to content

Commit

Permalink
coverage: add missing tests (#525)
Browse files Browse the repository at this point in the history
- Add missing tests for `ZipFile.ExtractToDirectory`
- Add test for initializaltion of statistic counter
- Add missing tests for `ParameterDescription`
- Add missing test for `Path.GetInvalidPathChars`
- Add test for correct exception when calling `GetUnixFileMode` on
windows
- Add test for correct exception when accessing a missing file via a
`SafeFileHandle`
  • Loading branch information
vbreuss committed Mar 26, 2024
1 parent 6a23c3a commit a7dd413
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,32 @@ public void ExtractToDirectory_Overwrite_ShouldOverwriteFile(
}
#endif

#if FEATURE_COMPRESSION_OVERWRITE
[SkippableTheory]
[AutoData]
public void ExtractToDirectory_WithEncoding_Overwrite_ShouldOverwriteFile(
string contents,
Encoding encoding)
{
FileSystem.Initialize()
.WithSubdirectory("bar").Initialized(s => s
.WithFile("test.txt"))
.WithSubdirectory("foo").Initialized(s => s
.WithFile("test.txt"));
FileSystem.File.WriteAllText(FileSystem.Path.Combine("foo", "test.txt"),
contents);

FileSystem.ZipFile().CreateFromDirectory("foo", "destination.zip");

FileSystem.ZipFile().ExtractToDirectory("destination.zip", "bar", encoding, true);

FileSystem.File.Exists(FileSystem.Path.Combine("bar", "test.txt"))
.Should().BeTrue();
FileSystem.File.ReadAllText(FileSystem.Path.Combine("bar", "test.txt"))
.Should().Be(contents);
}
#endif

[SkippableTheory]
[AutoData]
public void ExtractToDirectory_WithEncoding_ShouldZipDirectoryContent(
Expand Down Expand Up @@ -221,6 +247,33 @@ public void ExtractToDirectory_WithStream_Overwrite_ShouldOverwriteFile(
}
#endif

#if FEATURE_COMPRESSION_STREAM
[SkippableTheory]
[AutoData]
public void ExtractToDirectory_WithStream_WithEncoding_Overwrite_ShouldOverwriteFile(
string contents,
Encoding encoding)
{
FileSystem.Initialize()
.WithSubdirectory("bar").Initialized(s => s
.WithFile("test.txt"))
.WithSubdirectory("foo").Initialized(s => s
.WithFile("test.txt"));
FileSystem.File.WriteAllText(FileSystem.Path.Combine("foo", "test.txt"),
contents);
using MemoryStream stream = new();

FileSystem.ZipFile().CreateFromDirectory("foo", stream);

FileSystem.ZipFile().ExtractToDirectory(stream, "bar", encoding, true);

FileSystem.File.Exists(FileSystem.Path.Combine("bar", "test.txt"))
.Should().BeTrue();
FileSystem.File.ReadAllText(FileSystem.Path.Combine("bar", "test.txt"))
.Should().Be(contents);
}
#endif

#if FEATURE_COMPRESSION_STREAM
[SkippableTheory]
[AutoData]
Expand Down Expand Up @@ -267,6 +320,7 @@ public void ExtractToDirectory_WithStream_WithoutOverwriteAndExistingFile_Should

Exception? exception = Record.Exception(() =>
{
// ReSharper disable once AccessToDisposedClosure
FileSystem.ZipFile().ExtractToDirectory(stream, "bar");
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,50 @@ namespace Testably.Abstractions.Testing.Tests.FileSystem;

public class FileMockTests
{
#if FEATURE_FILESYSTEM_SAFEFILEHANDLE
[Theory]
[AutoData]
public void GetAttributes_SafeFileHandle_WithMissingFile_ShouldThrowFileNotFoundException(
string path)
{
SafeFileHandle fileHandle = new();
MockFileSystem fileSystem = new();
fileSystem.WithSafeFileHandleStrategy(
new DefaultSafeFileHandleStrategy(_ => new SafeFileHandleMock(path)));

Exception? exception = Record.Exception(() =>
{
_ = fileSystem.File.GetAttributes(fileHandle);
});

exception.Should().BeOfType<FileNotFoundException>();
}
#endif
#if FEATURE_FILESYSTEM_SAFEFILEHANDLE
[SkippableTheory]
[AutoData]
public void GetUnixFileMode_SafeFileHandle_ShouldThrowPlatformNotSupportedExceptionOnWindows(
string path)
{
Skip.IfNot(Test.RunsOnWindows);

SafeFileHandle fileHandle = new();
MockFileSystem fileSystem = new();
fileSystem.File.WriteAllText(path, "some content");
fileSystem.WithSafeFileHandleStrategy(
new DefaultSafeFileHandleStrategy(_ => new SafeFileHandleMock(path)));

Exception? exception = Record.Exception(() =>
{
#pragma warning disable CA1416
fileSystem.File.GetUnixFileMode(fileHandle);
#pragma warning restore CA1416
});

exception.Should().BeOfType<PlatformNotSupportedException>();
}
#endif

#if FEATURE_FILESYSTEM_SAFEFILEHANDLE
[Theory]
[AutoData]
Expand All @@ -30,6 +74,7 @@ public void SetAttributes_SafeFileHandle_ShouldUpdateValue(
result.Should().Be(expectedAttributes);
}
#endif

[Theory]
[AutoData]
public void SetCreationTime(string path, DateTime creationTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ namespace Testably.Abstractions.Testing.Tests.Statistics;

public sealed class MethodStatisticsTests
{
[Fact]
public void Counter_ShouldBeInitializedWithOne()
{
MockFileSystem fileSystem = new();
fileSystem.File.WriteAllText("foo", "bar");
MethodStatistic sut = fileSystem.Statistics.File.Methods.First();

sut.Counter.Should().Be(1);
}

[Fact]
public void ToString_ShouldContainName()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,31 @@ public void FromParameter_WithSpan_ShouldSetIsOutParameterToFalse(int[] buffer)
}
#endif

[Theory]
[InlineAutoData(true)]
[InlineAutoData(false)]
public void Is_WithComparer_ShouldUseComparerResult(bool comparerResult, string value)
{
ParameterDescription sut = ParameterDescription.FromParameter(value);

bool result = sut.Is<string>(_ => comparerResult);

result.Should().Be(comparerResult);
}

[Theory]
[InlineAutoData(true)]
[InlineAutoData(false)]
public void Is_WithComparer_WithIncompatibleType_ShouldReturnFalse(bool comparerResult,
string value)
{
ParameterDescription sut = ParameterDescription.FromParameter(value);

bool result = sut.Is<int>(_ => comparerResult);

result.Should().BeFalse();
}

[Theory]
[AutoData]
public void Is_WithFromOutParameter_ShouldCheckForMatchingValue(int value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ namespace Testably.Abstractions.Testing.Tests.Statistics;

public sealed class PropertyStatisticsTests
{
[Fact]
public void Counter_ShouldBeInitializedWithOne()
{
MockFileSystem fileSystem = new();
_ = fileSystem.Path.DirectorySeparatorChar;
PropertyStatistic sut = fileSystem.Statistics.Path.Properties.First();

sut.Counter.Should().Be(1);
}

[Fact]
public void ToString_Get_ShouldContainNameAndGet()
{
Expand Down
8 changes: 8 additions & 0 deletions Tests/Testably.Abstractions.Tests/FileSystem/Path/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public void GetInvalidFileNameChars_ShouldReturnDefaultValue()
result.Should().BeEquivalentTo(System.IO.Path.GetInvalidFileNameChars());
}

[SkippableFact]
public void GetInvalidPathChars_ShouldReturnDefaultValue()
{
char[] result = FileSystem.Path.GetInvalidPathChars();

result.Should().BeEquivalentTo(System.IO.Path.GetInvalidPathChars());
}

[SkippableTheory]
[AutoData]
public void GetPathRoot_ShouldReturnDefaultValue(string path)
Expand Down

0 comments on commit a7dd413

Please sign in to comment.