Skip to content

Commit 3a679fb

Browse files
authored
refactor: improve test coverage (#368)
Improve test coverage
1 parent 5631aca commit 3a679fb

File tree

20 files changed

+571
-34
lines changed

20 files changed

+571
-34
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<PackageVersion Include="xunit" Version="2.5.0" />
3232
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.0" />
3333
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
34+
<PackageVersion Include="NSubstitute" Version="5.0.0" />
3435
</ItemGroup>
3536

3637
<ItemGroup>

Source/Testably.Abstractions.AccessControl/Testably.Abstractions.AccessControl.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
<_Parameter1>true</_Parameter1>
2626
<_Parameter1_TypeName>System.Boolean</_Parameter1_TypeName>
2727
</AssemblyAttribute>
28+
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
29+
<_Parameter1>Testably.Abstractions.AccessControl.Tests</_Parameter1>
30+
</AssemblyAttribute>
2831
</ItemGroup>
2932

3033
</Project>

Source/Testably.Abstractions.Compression/Internal/ZipUtilities.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace Testably.Abstractions.Internal;
77

88
internal static class ZipUtilities
99
{
10+
private const string SearchPattern = "*";
11+
1012
internal static IZipArchiveEntry CreateEntryFromFile(
1113
IZipArchive destination,
1214
string sourceFileName,
@@ -28,7 +30,7 @@ internal static IZipArchiveEntry CreateEntryFromFile(
2830
DateTime lastWrite =
2931
destination.FileSystem.File.GetLastWriteTime(sourceFileName);
3032

31-
if (lastWrite.Year < 1980 || lastWrite.Year > 2107)
33+
if (lastWrite.Year is < 1980 or > 2107)
3234
{
3335
lastWrite = new DateTime(1980, 1, 1, 0, 0, 0);
3436
}
@@ -79,7 +81,7 @@ internal static void CreateFromDirectory(IFileSystem fileSystem,
7981
}
8082

8183
foreach (IFileSystemInfo file in di
82-
.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
84+
.EnumerateFileSystemInfos(SearchPattern, SearchOption.AllDirectories))
8385
{
8486
directoryIsEmpty = false;
8587

Source/Testably.Abstractions.Compression/Testably.Abstractions.Compression.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@
2020
<ProjectReference Include="..\Testably.Abstractions.Interface\Testably.Abstractions.Interface.csproj" />
2121
</ItemGroup>
2222

23+
<ItemGroup>
24+
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
25+
<_Parameter1>Testably.Abstractions.Compression.Tests</_Parameter1>
26+
</AssemblyAttribute>
27+
</ItemGroup>
28+
2329
</Project>

Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,15 @@ internal IReadOnlyList<IStorageContainer> GetContainers()
512512
.Select(x => x.Value)
513513
.ToList();
514514

515+
/// <summary>
516+
/// Removes the drive with the given <paramref name="driveName" />.
517+
/// </summary>
518+
internal IStorageDrive? RemoveDrive(string driveName)
519+
{
520+
_drives.TryRemove(driveName, out IStorageDrive? drive);
521+
return drive;
522+
}
523+
515524
private void CheckAndAdjustParentDirectoryTimes(IStorageLocation location)
516525
{
517526
IStorageContainer? parentContainer = GetContainer(location.GetParent());

Source/Testably.Abstractions.Testing/Storage/LocationExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ internal static class LocationExtensions
88
{
99
[return: NotNullIfNotNull("location")]
1010
public static IStorageLocation? ThrowIfNotFound(
11-
this IStorageLocation? location, MockFileSystem fileSystem,
11+
this IStorageLocation? location,
12+
MockFileSystem fileSystem,
1213
Action fileNotFoundException,
1314
Action? directoryNotFoundException = null)
1415
{
@@ -38,7 +39,8 @@ internal static class LocationExtensions
3839
}
3940

4041
public static IStorageLocation ThrowExceptionIfNotFound(
41-
this IStorageLocation location, MockFileSystem fileSystem,
42+
this IStorageLocation location,
43+
MockFileSystem fileSystem,
4244
bool allowMissingFile = false,
4345
Func<string, Exception>? onDirectoryNotFound = null,
4446
Func<string, Exception>? onFileNotFound = null)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.IO;
2+
3+
namespace Testably.Abstractions.AccessControl.Tests;
4+
5+
// ReSharper disable once PartialTypeWithSinglePart
6+
public abstract partial class AccessControlHelperTests<TFileSystem>
7+
: FileSystemTestBase<TFileSystem>
8+
where TFileSystem : IFileSystem
9+
{
10+
[Fact]
11+
public void GetExtensibilityOrThrow_DirectoryInfo_ShouldNotThrow()
12+
{
13+
IDirectoryInfo sut = FileSystem.DirectoryInfo.New("foo");
14+
15+
Exception? exception = Record.Exception(() =>
16+
{
17+
sut.GetExtensibilityOrThrow();
18+
});
19+
20+
exception.Should().BeNull();
21+
}
22+
23+
[Fact]
24+
public void GetExtensibilityOrThrow_FileSystemStream_ShouldNotThrow()
25+
{
26+
FileSystemStream sut = FileSystem.FileStream.New("foo", FileMode.Create);
27+
28+
Exception? exception = Record.Exception(() =>
29+
{
30+
sut.GetExtensibilityOrThrow();
31+
});
32+
33+
exception.Should().BeNull();
34+
}
35+
36+
[Fact]
37+
public void GetExtensibilityOrThrow_FileInfo_ShouldNotThrow()
38+
{
39+
IFileInfo sut = FileSystem.FileInfo.New("foo");
40+
41+
Exception? exception = Record.Exception(() =>
42+
{
43+
sut.GetExtensibilityOrThrow();
44+
});
45+
46+
exception.Should().BeNull();
47+
}
48+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using NSubstitute;
2+
using System.IO;
3+
using Testably.Abstractions.Helpers;
4+
5+
namespace Testably.Abstractions.AccessControl.Tests.Internal;
6+
7+
public sealed class AccessControlHelperTests
8+
{
9+
[Fact]
10+
public void ThrowIfMissing_MissingDirectoryInfo_ShouldThrowDirectoryNotFoundException()
11+
{
12+
MockFileSystem fileSystem = new();
13+
IDirectoryInfo sut = fileSystem.DirectoryInfo.New("foo");
14+
15+
Exception? exception = Record.Exception(() =>
16+
{
17+
sut.ThrowIfMissing();
18+
});
19+
20+
exception.Should().BeOfType<DirectoryNotFoundException>()
21+
.Which.HResult.Should().Be(-2147024893);
22+
exception!.Message.Should().Contain($"'{sut.FullName}'");
23+
}
24+
25+
[Fact]
26+
public void ThrowIfMissing_ExistingDirectoryInfo_ShouldNotThrow()
27+
{
28+
MockFileSystem fileSystem = new();
29+
IDirectoryInfo sut = fileSystem.DirectoryInfo.New("foo");
30+
fileSystem.Directory.CreateDirectory("foo");
31+
32+
Exception? exception = Record.Exception(() =>
33+
{
34+
sut.ThrowIfMissing();
35+
});
36+
37+
exception.Should().BeNull();
38+
}
39+
40+
[Fact]
41+
public void ThrowIfMissing_MissingFileInfo_ShouldThrowFileNotFoundException()
42+
{
43+
MockFileSystem fileSystem = new();
44+
IFileInfo sut = fileSystem.FileInfo.New("foo");
45+
46+
Exception? exception = Record.Exception(() =>
47+
{
48+
sut.ThrowIfMissing();
49+
});
50+
51+
exception.Should().BeOfType<FileNotFoundException>()
52+
.Which.HResult.Should().Be(-2147024894);
53+
exception!.Message.Should().Contain($"'{sut.FullName}'");
54+
}
55+
56+
[Fact]
57+
public void ThrowIfMissing_ExistingFileInfo_ShouldNotThrow()
58+
{
59+
MockFileSystem fileSystem = new();
60+
IFileInfo sut = fileSystem.FileInfo.New("foo");
61+
fileSystem.File.WriteAllText("foo", "some content");
62+
63+
Exception? exception = Record.Exception(() =>
64+
{
65+
sut.ThrowIfMissing();
66+
});
67+
68+
exception.Should().BeNull();
69+
}
70+
71+
[Fact]
72+
public void GetExtensibilityOrThrow_CustomDirectoryInfo_ShouldThrowNotSupportedException()
73+
{
74+
IDirectoryInfo? sut = Substitute.For<IDirectoryInfo>();
75+
76+
Exception? exception = Record.Exception(() =>
77+
{
78+
sut.GetExtensibilityOrThrow();
79+
});
80+
81+
exception.Should().BeOfType<NotSupportedException>()
82+
.Which.Message.Should()
83+
.Contain(nameof(IFileSystemExtensibility)).And
84+
.Contain(sut.GetType().Name);
85+
}
86+
87+
[Fact]
88+
public void GetExtensibilityOrThrow_CustomFileSystemStream_ShouldThrowNotSupportedException()
89+
{
90+
FileSystemStream sut = new CustomFileSystemStream();
91+
92+
Exception? exception = Record.Exception(() =>
93+
{
94+
sut.GetExtensibilityOrThrow();
95+
});
96+
97+
exception.Should().BeOfType<NotSupportedException>()
98+
.Which.Message.Should()
99+
.Contain(nameof(IFileSystemExtensibility)).And
100+
.Contain(sut.GetType().Name);
101+
}
102+
103+
[Fact]
104+
public void GetExtensibilityOrThrow_CustomFileInfo_ShouldThrowNotSupportedException()
105+
{
106+
IFileInfo? sut = Substitute.For<IFileInfo>();
107+
108+
Exception? exception = Record.Exception(() =>
109+
{
110+
sut.GetExtensibilityOrThrow();
111+
});
112+
113+
exception.Should().BeOfType<NotSupportedException>()
114+
.Which.Message.Should()
115+
.Contain(nameof(IFileSystemExtensibility)).And
116+
.Contain(sut.GetType().Name);
117+
}
118+
119+
private class CustomFileSystemStream : FileSystemStream
120+
{
121+
/// <inheritdoc />
122+
public CustomFileSystemStream() : base(Null, ".", false)
123+
{
124+
}
125+
}
126+
}

Tests/Testably.Abstractions.AccessControl.Tests/Testably.Abstractions.AccessControl.Tests.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<ProjectReference
16-
Include="..\Helpers\Testably.Abstractions.Tests.SourceGenerator\Testably.Abstractions.Tests.SourceGenerator.csproj"
17-
OutputItemType="Analyzer" />
15+
<ProjectReference Include="..\Helpers\Testably.Abstractions.Tests.SourceGenerator\Testably.Abstractions.Tests.SourceGenerator.csproj" OutputItemType="Analyzer" />
16+
<PackageReference Include="NSubstitute" />
1817
</ItemGroup>
1918

2019
<PropertyGroup>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Testably.Abstractions.Internal;
2+
3+
namespace Testably.Abstractions.Compression.Tests.Internal;
4+
5+
public sealed class ExecuteTests
6+
{
7+
[Fact]
8+
public void WhenRealFileSystem_MockFileSystem_ShouldExecuteOnMockFileSystem()
9+
{
10+
bool onRealFileSystemExecuted = false;
11+
bool onMockFileSystemExecuted = false;
12+
MockFileSystem fileSystem = new();
13+
Execute.WhenRealFileSystem(fileSystem,
14+
() => onRealFileSystemExecuted = true,
15+
() => onMockFileSystemExecuted = true);
16+
17+
onRealFileSystemExecuted.Should().BeFalse();
18+
onMockFileSystemExecuted.Should().BeTrue();
19+
}
20+
21+
[Fact]
22+
public void WhenRealFileSystem_RealFileSystem_ShouldExecuteOnRealFileSystem()
23+
{
24+
bool onRealFileSystemExecuted = false;
25+
bool onMockFileSystemExecuted = false;
26+
RealFileSystem fileSystem = new();
27+
Execute.WhenRealFileSystem(fileSystem,
28+
() => onRealFileSystemExecuted = true,
29+
() => onMockFileSystemExecuted = true);
30+
31+
onRealFileSystemExecuted.Should().BeTrue();
32+
onMockFileSystemExecuted.Should().BeFalse();
33+
}
34+
}

0 commit comments

Comments
 (0)