Skip to content

Commit

Permalink
feat: enable assertions on IFileSystemInfo (#51)
Browse files Browse the repository at this point in the history
Enable assertions directly on `IFileSystemInfo`, e.g. when returned from
`ResolveLinkTarget`.
  • Loading branch information
vbreuss committed Sep 10, 2023
1 parent 7c58521 commit f5e43be
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ public static DirectoryInfoAssertions Should(this IDirectoryInfo? instance)
public static FileInfoAssertions Should(this IFileInfo? instance)
=> new(instance);

/// <summary>
/// Returns a <see cref="FileAssertions" /> object that can be used to
/// assert the current <see cref="IFileInfo" />.
/// </summary>
public static FileSystemInfoAssertions Should(this IFileSystemInfo? instance)
=> new(instance);

/// <summary>
/// Returns a <see cref="FileSystemAssertions" /> object that can be used to
/// assert the current <see cref="IFileSystem" />.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
namespace Testably.Abstractions.FluentAssertions;

/// <summary>
/// Assertions on <see cref="IFileSystemInfo" />.
/// </summary>
public class FileSystemInfoAssertions :
FileSystemInfoAssertions<IFileSystemInfo, FileSystemInfoAssertions>
{
/// <inheritdoc cref="ReferenceTypeAssertions{TSubject,TAssertions}.Identifier" />
protected override string Identifier => "file system info";

internal FileSystemInfoAssertions(IFileSystemInfo? instance)
: base(instance)
{
}
}

/// <summary>
/// Assertions on <see cref="IFileSystemInfo" />.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,45 @@ public void Exist_ForFileInfo_WithExistingFile_ShouldNotThrow(string fileName)
.Be($"Expected file \"{fileName}\" to exist {because}, but it did not.");
}

#if NET
[SkippableTheory]
[AutoData]
public void Exist_ForFileSystemInfo_WithExistingFile_ShouldNotThrow(
string path, string pathToTarget, string because)
{
MockFileSystem fileSystem = new();
string targetFullPath = fileSystem.Path.GetFullPath(pathToTarget);
fileSystem.Directory.CreateDirectory(pathToTarget);
fileSystem.Directory.CreateSymbolicLink(path, targetFullPath);
IFileSystemInfo? sut =
fileSystem.Directory.ResolveLinkTarget(path, false);

sut.Should().Exist(because);
}

[SkippableTheory]
[AutoData]
public void Exist_ForFileSystemInfo_WithoutExistingFile_ShouldThrow(
string path, string pathToTarget, string because)
{
MockFileSystem fileSystem = new();
string targetFullPath = fileSystem.Path.GetFullPath(pathToTarget);
fileSystem.Directory.CreateSymbolicLink(path, targetFullPath);
IFileSystemInfo? sut =
fileSystem.Directory.ResolveLinkTarget(path, false);

Exception? exception = Record.Exception(() =>
{
sut.Should().Exist(because);
});

exception.Should().NotBeNull();
exception!.Message.Should()
.Be(
$"Expected file system info \"{pathToTarget}\" to exist {because}, but it did not.");
}
#endif

[Theory]
[AutoData]
public void NotExist_ForDirectoryInfo_Null_ShouldThrow(string because)
Expand Down Expand Up @@ -275,4 +314,43 @@ public void NotExist_ForFileInfo_WithoutExistingFile_ShouldNotThrow(string fileN

sut.Should().NotExist(because);
}

#if NET
[SkippableTheory]
[AutoData]
public void NotExist_ForFileSystemInfo_WithExistingFile_ShouldThrow(
string path, string pathToTarget, string because)
{
MockFileSystem fileSystem = new();
string targetFullPath = fileSystem.Path.GetFullPath(pathToTarget);
fileSystem.Directory.CreateDirectory(pathToTarget);
fileSystem.Directory.CreateSymbolicLink(path, targetFullPath);
IFileSystemInfo? sut =
fileSystem.Directory.ResolveLinkTarget(path, false);

Exception? exception = Record.Exception(() =>
{
sut.Should().NotExist(because);
});

exception.Should().NotBeNull();
exception!.Message.Should()
.Be(
$"Expected file system info \"{pathToTarget}\" not to exist {because}, but it did.");
}

[SkippableTheory]
[AutoData]
public void NotExist_ForFileSystemInfo_WithoutExistingFile_ShouldNotThrow(
string path, string pathToTarget, string because)
{
MockFileSystem fileSystem = new();
string targetFullPath = fileSystem.Path.GetFullPath(pathToTarget);
fileSystem.Directory.CreateSymbolicLink(path, targetFullPath);
IFileSystemInfo? sut =
fileSystem.Directory.ResolveLinkTarget(path, false);

sut.Should().NotExist(because);
}
#endif
}

0 comments on commit f5e43be

Please sign in to comment.