using System.IO.Abstractions; using FluentAssertions; using Testably.Abstractions.Testing; using Xunit; namespace MockFileTest; public sealed class MockTest : FsTests { public MockTest() : base(new MockFileSystem()) { } } public sealed class PhysicalTest : FsTests, IDisposable { public PhysicalTest() : base(new FileSystem()) { } public void Dispose() => Fs.Directory.Delete(BaseDir, true); } public abstract class FsTests { public static readonly TheoryData SepChars = new() { "", "\\", "/" }; protected readonly IFileSystem Fs; protected readonly string BaseDir; protected FsTests(IFileSystem fs) { Fs = fs; BaseDir = Path.Combine(fs.Path.GetTempPath(), Guid.NewGuid().ToString()); } [Theory] [MemberData(nameof(SepChars))] public void CreateDirWithTrailingSlash(string sepChar) { Fs.Directory.CreateDirectory(Path.Combine(BaseDir, "A")); Fs.Directory.CreateDirectory(Path.Combine(BaseDir, "B" + sepChar)); var dirInfo = Fs.DirectoryInfo.New(BaseDir); var result = dirInfo.EnumerateFileSystemInfos(); result.Should().HaveCount(2); // expect "B" and "C". } [Theory] [MemberData(nameof(SepChars))] public void CreateDirViaInfoWithTrailingSlash(string sepChar) { Fs.DirectoryInfo.New(Path.Combine(BaseDir, "A")).Create(); Fs.DirectoryInfo.New(Path.Combine(BaseDir, "B" + sepChar)).Create(); var dirInfo = Fs.DirectoryInfo.New(BaseDir); var result = dirInfo.EnumerateFileSystemInfos(); result.Should().HaveCount(2); // expect "B" and "C". } [Theory] [MemberData(nameof(SepChars))] public void MoveDirWithTrailingSlash(string sepChar) { Fs.Directory.CreateDirectory(Path.Combine(BaseDir, "A")); Fs.Directory.CreateDirectory(Path.Combine(BaseDir, "B")); Fs.Directory.Move(Path.Combine(BaseDir, "B"), Path.Combine(BaseDir, "X" + sepChar)); var dirInfo = Fs.DirectoryInfo.New(BaseDir); var result = dirInfo.EnumerateFileSystemInfos(); result.Should().HaveCount(2); // expect "B" and "C". } [Theory] [MemberData(nameof(SepChars))] public void RejectListingOfFiles(string sepChar) { Fs.Directory.CreateDirectory(Path.Combine(BaseDir, "A")); var filePath = Path.Combine(BaseDir, "A/B"); Fs.File.WriteAllText(filePath, "Hello"); var dirInfo = Fs.DirectoryInfo.New(filePath + sepChar); dirInfo.Invoking(d => d.EnumerateFileSystemInfos().ToList()) .Should().Throw(); } }