Skip to content

Commit f5e43be

Browse files
authored
feat: enable assertions on IFileSystemInfo (#51)
Enable assertions directly on `IFileSystemInfo`, e.g. when returned from `ResolveLinkTarget`.
1 parent 7c58521 commit f5e43be

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

Source/Testably.Abstractions.FluentAssertions/FileSystemExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ public static DirectoryInfoAssertions Should(this IDirectoryInfo? instance)
1919
public static FileInfoAssertions Should(this IFileInfo? instance)
2020
=> new(instance);
2121

22+
/// <summary>
23+
/// Returns a <see cref="FileAssertions" /> object that can be used to
24+
/// assert the current <see cref="IFileInfo" />.
25+
/// </summary>
26+
public static FileSystemInfoAssertions Should(this IFileSystemInfo? instance)
27+
=> new(instance);
28+
2229
/// <summary>
2330
/// Returns a <see cref="FileSystemAssertions" /> object that can be used to
2431
/// assert the current <see cref="IFileSystem" />.

Source/Testably.Abstractions.FluentAssertions/FileSystemInfoAssertions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
namespace Testably.Abstractions.FluentAssertions;
22

3+
/// <summary>
4+
/// Assertions on <see cref="IFileSystemInfo" />.
5+
/// </summary>
6+
public class FileSystemInfoAssertions :
7+
FileSystemInfoAssertions<IFileSystemInfo, FileSystemInfoAssertions>
8+
{
9+
/// <inheritdoc cref="ReferenceTypeAssertions{TSubject,TAssertions}.Identifier" />
10+
protected override string Identifier => "file system info";
11+
12+
internal FileSystemInfoAssertions(IFileSystemInfo? instance)
13+
: base(instance)
14+
{
15+
}
16+
}
17+
318
/// <summary>
419
/// Assertions on <see cref="IFileSystemInfo" />.
520
/// </summary>

Tests/Testably.Abstractions.FluentAssertions.Tests/FileSystemInfoAssertionsTests.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,45 @@ public void Exist_ForFileInfo_WithSameDirectory_ShouldThrow(
149149
.Be($"Expected file \"{fileName}\" to exist {because}, but it did not.");
150150
}
151151

152+
#if NET
153+
[SkippableTheory]
154+
[AutoData]
155+
public void Exist_ForFileSystemInfo_WithExistingFile_ShouldNotThrow(
156+
string path, string pathToTarget, string because)
157+
{
158+
MockFileSystem fileSystem = new();
159+
string targetFullPath = fileSystem.Path.GetFullPath(pathToTarget);
160+
fileSystem.Directory.CreateDirectory(pathToTarget);
161+
fileSystem.Directory.CreateSymbolicLink(path, targetFullPath);
162+
IFileSystemInfo? sut =
163+
fileSystem.Directory.ResolveLinkTarget(path, false);
164+
165+
sut.Should().Exist(because);
166+
}
167+
168+
[SkippableTheory]
169+
[AutoData]
170+
public void Exist_ForFileSystemInfo_WithoutExistingFile_ShouldThrow(
171+
string path, string pathToTarget, string because)
172+
{
173+
MockFileSystem fileSystem = new();
174+
string targetFullPath = fileSystem.Path.GetFullPath(pathToTarget);
175+
fileSystem.Directory.CreateSymbolicLink(path, targetFullPath);
176+
IFileSystemInfo? sut =
177+
fileSystem.Directory.ResolveLinkTarget(path, false);
178+
179+
Exception? exception = Record.Exception(() =>
180+
{
181+
sut.Should().Exist(because);
182+
});
183+
184+
exception.Should().NotBeNull();
185+
exception!.Message.Should()
186+
.Be(
187+
$"Expected file system info \"{pathToTarget}\" to exist {because}, but it did not.");
188+
}
189+
#endif
190+
152191
[Theory]
153192
[AutoData]
154193
public void NotExist_ForDirectoryInfo_Null_ShouldThrow(string because)
@@ -275,4 +314,43 @@ public void NotExist_ForFileInfo_WithSameDirectory_ShouldNotThrow(
275314

276315
sut.Should().NotExist(because);
277316
}
317+
318+
#if NET
319+
[SkippableTheory]
320+
[AutoData]
321+
public void NotExist_ForFileSystemInfo_WithExistingFile_ShouldThrow(
322+
string path, string pathToTarget, string because)
323+
{
324+
MockFileSystem fileSystem = new();
325+
string targetFullPath = fileSystem.Path.GetFullPath(pathToTarget);
326+
fileSystem.Directory.CreateDirectory(pathToTarget);
327+
fileSystem.Directory.CreateSymbolicLink(path, targetFullPath);
328+
IFileSystemInfo? sut =
329+
fileSystem.Directory.ResolveLinkTarget(path, false);
330+
331+
Exception? exception = Record.Exception(() =>
332+
{
333+
sut.Should().NotExist(because);
334+
});
335+
336+
exception.Should().NotBeNull();
337+
exception!.Message.Should()
338+
.Be(
339+
$"Expected file system info \"{pathToTarget}\" not to exist {because}, but it did.");
340+
}
341+
342+
[SkippableTheory]
343+
[AutoData]
344+
public void NotExist_ForFileSystemInfo_WithoutExistingFile_ShouldNotThrow(
345+
string path, string pathToTarget, string because)
346+
{
347+
MockFileSystem fileSystem = new();
348+
string targetFullPath = fileSystem.Path.GetFullPath(pathToTarget);
349+
fileSystem.Directory.CreateSymbolicLink(path, targetFullPath);
350+
IFileSystemInfo? sut =
351+
fileSystem.Directory.ResolveLinkTarget(path, false);
352+
353+
sut.Should().NotExist(because);
354+
}
355+
#endif
278356
}

0 commit comments

Comments
 (0)