From f5e43be39e9faf2a599c36f7110e0e46c95d6afc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sun, 10 Sep 2023 10:55:13 +0200 Subject: [PATCH] feat: enable assertions on `IFileSystemInfo` (#51) Enable assertions directly on `IFileSystemInfo`, e.g. when returned from `ResolveLinkTarget`. --- .../FileSystemExtensions.cs | 7 ++ .../FileSystemInfoAssertions.cs | 15 ++++ .../FileSystemInfoAssertionsTests.cs | 78 +++++++++++++++++++ 3 files changed, 100 insertions(+) diff --git a/Source/Testably.Abstractions.FluentAssertions/FileSystemExtensions.cs b/Source/Testably.Abstractions.FluentAssertions/FileSystemExtensions.cs index ec32f01..d93221f 100644 --- a/Source/Testably.Abstractions.FluentAssertions/FileSystemExtensions.cs +++ b/Source/Testably.Abstractions.FluentAssertions/FileSystemExtensions.cs @@ -19,6 +19,13 @@ public static DirectoryInfoAssertions Should(this IDirectoryInfo? instance) public static FileInfoAssertions Should(this IFileInfo? instance) => new(instance); + /// + /// Returns a object that can be used to + /// assert the current . + /// + public static FileSystemInfoAssertions Should(this IFileSystemInfo? instance) + => new(instance); + /// /// Returns a object that can be used to /// assert the current . diff --git a/Source/Testably.Abstractions.FluentAssertions/FileSystemInfoAssertions.cs b/Source/Testably.Abstractions.FluentAssertions/FileSystemInfoAssertions.cs index 55e941c..0c60016 100644 --- a/Source/Testably.Abstractions.FluentAssertions/FileSystemInfoAssertions.cs +++ b/Source/Testably.Abstractions.FluentAssertions/FileSystemInfoAssertions.cs @@ -1,5 +1,20 @@ namespace Testably.Abstractions.FluentAssertions; +/// +/// Assertions on . +/// +public class FileSystemInfoAssertions : + FileSystemInfoAssertions +{ + /// + protected override string Identifier => "file system info"; + + internal FileSystemInfoAssertions(IFileSystemInfo? instance) + : base(instance) + { + } +} + /// /// Assertions on . /// diff --git a/Tests/Testably.Abstractions.FluentAssertions.Tests/FileSystemInfoAssertionsTests.cs b/Tests/Testably.Abstractions.FluentAssertions.Tests/FileSystemInfoAssertionsTests.cs index a30681e..1d4d724 100644 --- a/Tests/Testably.Abstractions.FluentAssertions.Tests/FileSystemInfoAssertionsTests.cs +++ b/Tests/Testably.Abstractions.FluentAssertions.Tests/FileSystemInfoAssertionsTests.cs @@ -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) @@ -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 }