Skip to content

Commit

Permalink
fix: correctly handle special directories for root paths (#591)
Browse files Browse the repository at this point in the history
* Add test for correct handling of special directories for root paths

* Fix implementation of `MockFileSystem`
  • Loading branch information
vbreuss authored May 5, 2024
1 parent d6b88dc commit d0d0039
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
13 changes: 9 additions & 4 deletions Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,20 @@ public IEnumerable<IStorageLocation> 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}..");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> 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(
Expand Down

0 comments on commit d0d0039

Please sign in to comment.