Skip to content

Commit a1d26e7

Browse files
authored
fix: enumerate directories with trailing slash (#803)
This PR ensures that directories with trailing slashes are properly enumerated. - Adds logic to trim trailing directory separators before calling `GetFileName` in enumeration - Includes a test case to verify empty directories with trailing slashes are properly enumerated
1 parent d3f8cdb commit a1d26e7

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,13 @@ public IEnumerable<IStorageLocation> EnumerateLocations(
252252
if (type.HasFlag(item.Value.Type) &&
253253
IncludeItemInEnumeration(item, fullPathWithoutTrailingSlash, enumerationOptions))
254254
{
255-
string name = _fileSystem.Execute.Path.GetFileName(item.Key.FullPath);
255+
string? itemPath = item.Key.FullPath;
256+
if (itemPath.EndsWith(_fileSystem.Path.DirectorySeparatorChar))
257+
{
258+
itemPath = itemPath.TrimEnd(_fileSystem.Path.DirectorySeparatorChar);
259+
}
260+
261+
string name = _fileSystem.Execute.Path.GetFileName(itemPath);
256262
if (EnumerationOptionsHelper.MatchesPattern(
257263
_fileSystem.Execute,
258264
enumerationOptions,
@@ -967,9 +973,8 @@ private bool IncludeItemInEnumeration(
967973
sourceContainer.Attributes |= FileAttributes.Archive;
968974
}
969975

970-
rollbacks?.Add(new Rollback(
971-
() => MoveInternal(destination, source, true, false,
972-
sourceType)));
976+
rollbacks?.Add(new Rollback(() => MoveInternal(destination, source, true, false,
977+
sourceType)));
973978
_fileSystem.ChangeHandler.NotifyCompletedChange(fileSystemChange);
974979
return destination;
975980
}

Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateDirectoriesTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,18 @@ await That(result).HasSingle().Which.EndsWith(extension)
181181
}
182182
}
183183

184+
[Fact]
185+
public async Task EnumerateDirectories_ShouldIncludeEmptyDirectoriesWithTrailingSlash()
186+
{
187+
string rootDirectory = "RootDir";
188+
string emptyDirectory = FileSystem.Path.Combine(rootDirectory, "EmptyDir") + FileSystem.Path.DirectorySeparatorChar;
189+
190+
FileSystem.Directory.CreateDirectory(emptyDirectory);
191+
192+
await That(FileSystem.Directory.Exists(emptyDirectory)).IsTrue();
193+
await That(FileSystem.Directory.EnumerateDirectories(rootDirectory)).HasCount(1);
194+
}
195+
184196
[Fact]
185197
public async Task EnumerateDirectories_ShouldSupportExtendedLengthPaths1()
186198
{

0 commit comments

Comments
 (0)