Skip to content

Commit

Permalink
fix: support relative path with drive information in `Path.GetFullPat…
Browse files Browse the repository at this point in the history
…h` (#411)

Support relative paths with drive information in `Path.GetFullPath` as
described in
TestableIO/System.IO.Abstractions#1044.
  • Loading branch information
vbreuss committed Oct 11, 2023
1 parent e8141c2 commit bf54ae1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Source/Testably.Abstractions.Testing/FileSystem/PathMock.cs
Expand Up @@ -37,6 +37,21 @@ public override string GetFullPath(string path)
{
path.EnsureValidArgument(FileSystem, nameof(path));

string? pathRoot = Path.GetPathRoot(path);
string? directoryRoot = Path.GetPathRoot(_fileSystem.Storage.CurrentDirectory);
if (!string.IsNullOrEmpty(pathRoot) && !string.IsNullOrEmpty(directoryRoot))
{
if (char.ToUpperInvariant(pathRoot[0]) != char.ToUpperInvariant(directoryRoot[0]))
{
return Path.GetFullPath(path);
}

if (pathRoot.Length < directoryRoot.Length)
{
path = path.Substring(pathRoot.Length);
}
}

return Path.GetFullPath(Path.Combine(
_fileSystem.Storage.CurrentDirectory,
path));
Expand Down
Expand Up @@ -5,6 +5,49 @@ public abstract partial class GetFullPathTests<TFileSystem>
: FileSystemTestBase<TFileSystem>
where TFileSystem : IFileSystem
{
[SkippableFact]
public void GetFullPath_Dot_ShouldReturnToCurrentDirectory()
{
string expectedFullPath = FileSystem.Directory.GetCurrentDirectory();

string result = FileSystem.Path.GetFullPath(".");

result.Should().Be(expectedFullPath);
}

[SkippableFact]
public void GetFullPath_RelativePathWithDrive_ShouldReturnExpectedValue()
{
Skip.IfNot(Test.RunsOnWindows);

string currentDirectory = FileSystem.Directory.GetCurrentDirectory();
string drive = currentDirectory.Substring(0, 1);
string input = $"{drive}:test.txt";
string expectedFullPath = FileSystem.Path.Combine(currentDirectory, "test.txt");

string result = FileSystem.Path.GetFullPath(input);

result.Should().Be(expectedFullPath);
}

[SkippableFact]
public void
GetFullPath_RelativePathWithDrive_WhenCurrentDirectoryIsDifferent_ShouldReturnExpectedValue()
{
Skip.IfNot(Test.RunsOnWindows);

string currentDirectory = FileSystem.Directory.GetCurrentDirectory();
string otherDrive = currentDirectory
.Substring(0,1)
.Equals("x", StringComparison.OrdinalIgnoreCase) ? "Y" : "X";
string input = $"{otherDrive}:test.txt";
string expectedFullPath = $@"{otherDrive}:\test.txt";

string result = FileSystem.Path.GetFullPath(input);

result.Should().Be(expectedFullPath);
}

[SkippableTheory]
[InlineData(@"top/../most/file", @"most/file")]
[InlineData(@"top/../most/../dir/file", @"dir/file")]
Expand Down

0 comments on commit bf54ae1

Please sign in to comment.