diff --git a/Source/Testably.Abstractions.Testing/Helpers/FileSystemExtensions.cs b/Source/Testably.Abstractions.Testing/Helpers/FileSystemExtensions.cs index 76d5762e..759c5bf6 100644 --- a/Source/Testably.Abstractions.Testing/Helpers/FileSystemExtensions.cs +++ b/Source/Testably.Abstractions.Testing/Helpers/FileSystemExtensions.cs @@ -58,6 +58,11 @@ internal static string GetSubdirectoryPath(this MockFileSystem fileSystem, { if (fileSystem.Execute.Path.IsPathRooted(givenPath)) { + if (friendlyName is "/." or "/..") + { + return friendlyName; + } + return fullFilePath; } diff --git a/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs b/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs index bb668030..69f22067 100644 --- a/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs +++ b/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs @@ -204,15 +204,20 @@ public IEnumerable EnumerateLocations( drive = _fileSystem.Storage.MainDrive; } + string prefix = + location.FriendlyName.EndsWith(_fileSystem.Execute.Path.DirectorySeparatorChar) + ? location.FriendlyName + : location.FriendlyName + _fileSystem.Execute.Path.DirectorySeparatorChar; + yield return InMemoryLocation.New(_fileSystem, drive, fullPath, - $"{location.FriendlyName}{_fileSystem.Execute.Path.DirectorySeparatorChar}."); + $"{prefix}."); string? parentPath = _fileSystem.Execute.Path.GetDirectoryName( fullPath.TrimEnd(_fileSystem.Execute.Path .DirectorySeparatorChar)); - if (parentPath != null) + if (parentPath != null || !_fileSystem.Execute.IsWindows) { - yield return InMemoryLocation.New(_fileSystem, drive, parentPath, - $"{location.FriendlyName}{_fileSystem.Execute.Path.DirectorySeparatorChar}.."); + yield return InMemoryLocation.New(_fileSystem, drive, parentPath ?? "/", + $"{prefix}.."); } } diff --git a/Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateDirectoriesTests.cs b/Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateDirectoriesTests.cs index d34cf6e2..fa761a12 100644 --- a/Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateDirectoriesTests.cs +++ b/Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateDirectoriesTests.cs @@ -433,6 +433,33 @@ public void } #endif +#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS + [SkippableFact] + public void + EnumerateDirectories_WithEnumerationOptions_ShouldConsiderReturnSpecialDirectoriesCorrectlyForPathRoots() + { + string root = FileSystem.Path.GetPathRoot(FileSystem.Directory.GetCurrentDirectory())!; + EnumerationOptions enumerationOptions = new() + { + ReturnSpecialDirectories = true + }; + + List result = FileSystem.Directory + .EnumerateDirectories(root, "*", enumerationOptions).ToList(); + + if (Test.RunsOnWindows) + { + result.Should().NotContain(FileSystem.Path.Combine(root, ".")); + result.Should().NotContain(FileSystem.Path.Combine(root, "..")); + } + else + { + result.Should().Contain(FileSystem.Path.Combine(root, ".")); + result.Should().Contain(FileSystem.Path.Combine(root, "..")); + } + } +#endif + [SkippableTheory] [AutoData] public void EnumerateDirectories_WithNewline_ShouldThrowArgumentException(