From 8f22f4193869d1138afbd91b57d6ffe8e7f7da2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Tue, 24 Oct 2023 09:44:06 +0200 Subject: [PATCH] Fix EnumerateDirectories with trailing slash --- .../Storage/InMemoryStorage.cs | 7 ++++- .../Directory/EnumerateDirectoriesTests.cs | 27 +++++++++---------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs b/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs index f85f45beb..12d5e9be5 100644 --- a/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs +++ b/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs @@ -173,6 +173,7 @@ public IEnumerable EnumerateLocations( enumerationOptions ??= EnumerationOptionsHelper.Compatible; string fullPath = location.FullPath; + string fullPathWithoutTrailingSlash = fullPath; #if NETSTANDARD2_0 if (!fullPath.EndsWith($"{_fileSystem.Path.DirectorySeparatorChar}")) #else @@ -181,6 +182,10 @@ public IEnumerable EnumerateLocations( { fullPath += _fileSystem.Path.DirectorySeparatorChar; } + else if (_fileSystem.Path.GetPathRoot(fullPath) != fullPath) + { + fullPathWithoutTrailingSlash = fullPathWithoutTrailingSlash.TrimEnd(_fileSystem.Path.DirectorySeparatorChar); + } foreach (KeyValuePair item in _containers .Where(x => x.Key.FullPath.StartsWith(fullPath, @@ -192,7 +197,7 @@ public IEnumerable EnumerateLocations( item.Key.FullPath.TrimEnd(_fileSystem.Path .DirectorySeparatorChar)); if (!enumerationOptions.RecurseSubdirectories && - parentPath?.Equals(location.FullPath, + parentPath?.Equals(fullPathWithoutTrailingSlash, InMemoryLocation.StringComparisonMode) != true) { continue; diff --git a/Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateDirectoriesTests.cs b/Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateDirectoriesTests.cs index 2f9a22d4a..2352ba67e 100644 --- a/Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateDirectoriesTests.cs +++ b/Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateDirectoriesTests.cs @@ -9,21 +9,6 @@ public abstract partial class EnumerateDirectoriesTests : FileSystemTestBase where TFileSystem : IFileSystem { - [SkippableTheory] - [InlineData("Folder", @"Folder\SubFolder")] - [InlineData(@"Folder\", @"Folder\SubFolder")] - [InlineData(@"Folder\..\.\Folder", @"Folder\..\.\Folder\SubFolder")] - public void MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath( - string queryPath, string expectedPath) - { - var fileSystem = new MockFileSystem(); - fileSystem.Directory.CreateDirectory("Folder/SubFolder"); - - var actualResult = fileSystem.Directory.EnumerateDirectories(queryPath); - - actualResult.Should().BeEquivalentTo(new[] { expectedPath }); - } - [SkippableFact] public void EnumerateDirectories_AbsolutePath_ShouldNotIncludeTrailingSlash() { @@ -261,4 +246,16 @@ public void result.Count().Should().Be(2); } + + [SkippableFact] + public void EnumerateDirectories_WithTrailingSlash_ShouldEnumerateSubdirectories() + { + string queryPath = @"Folder\"; + string expectedPath = @"Folder\SubFolder"; + FileSystem.Directory.CreateDirectory("Folder/SubFolder"); + + IEnumerable actualResult = FileSystem.Directory.EnumerateDirectories(queryPath); + + actualResult.Should().BeEquivalentTo(expectedPath); + } }