Skip to content

Commit

Permalink
fix: Directory.EnumerateDirectories with trailing slash in path (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vbreuss authored Oct 24, 2023
1 parent d84221f commit b053078
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public IEnumerable<IStorageLocation> EnumerateLocations(
enumerationOptions ??= EnumerationOptionsHelper.Compatible;

string fullPath = location.FullPath;
string fullPathWithoutTrailingSlash = fullPath;
#if NETSTANDARD2_0
if (!fullPath.EndsWith($"{_fileSystem.Path.DirectorySeparatorChar}"))
#else
Expand All @@ -181,6 +182,10 @@ public IEnumerable<IStorageLocation> EnumerateLocations(
{
fullPath += _fileSystem.Path.DirectorySeparatorChar;
}
else if (_fileSystem.Path.GetPathRoot(fullPath) != fullPath)
{
fullPathWithoutTrailingSlash = fullPathWithoutTrailingSlash.TrimEnd(_fileSystem.Path.DirectorySeparatorChar);
}

foreach (KeyValuePair<IStorageLocation, IStorageContainer> item in _containers
.Where(x => x.Key.FullPath.StartsWith(fullPath,
Expand All @@ -192,7 +197,7 @@ public IEnumerable<IStorageLocation> EnumerateLocations(
item.Key.FullPath.TrimEnd(_fileSystem.Path
.DirectorySeparatorChar));
if (!enumerationOptions.RecurseSubdirectories &&
parentPath?.Equals(location.FullPath,
parentPath?.Equals(fullPathWithoutTrailingSlash,
InMemoryLocation.StringComparisonMode) != true)
{
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,16 @@ public void

result.Count().Should().Be(2);
}

[SkippableFact]
public void EnumerateDirectories_WithTrailingSlash_ShouldEnumerateSubdirectories()
{
string queryPath = @"foo" + FileSystem.Path.DirectorySeparatorChar;
string expectedPath = FileSystem.Path.Combine("foo", "bar");
FileSystem.Directory.CreateDirectory(expectedPath);

IEnumerable<string> actualResult = FileSystem.Directory.EnumerateDirectories(queryPath);

actualResult.Should().BeEquivalentTo(expectedPath);
}
}

0 comments on commit b053078

Please sign in to comment.