Skip to content

Commit bf54ae1

Browse files
authored
fix: support relative path with drive information in Path.GetFullPath (#411)
Support relative paths with drive information in `Path.GetFullPath` as described in TestableIO/System.IO.Abstractions#1044.
1 parent e8141c2 commit bf54ae1

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

Source/Testably.Abstractions.Testing/FileSystem/PathMock.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ public override string GetFullPath(string path)
3737
{
3838
path.EnsureValidArgument(FileSystem, nameof(path));
3939

40+
string? pathRoot = Path.GetPathRoot(path);
41+
string? directoryRoot = Path.GetPathRoot(_fileSystem.Storage.CurrentDirectory);
42+
if (!string.IsNullOrEmpty(pathRoot) && !string.IsNullOrEmpty(directoryRoot))
43+
{
44+
if (char.ToUpperInvariant(pathRoot[0]) != char.ToUpperInvariant(directoryRoot[0]))
45+
{
46+
return Path.GetFullPath(path);
47+
}
48+
49+
if (pathRoot.Length < directoryRoot.Length)
50+
{
51+
path = path.Substring(pathRoot.Length);
52+
}
53+
}
54+
4055
return Path.GetFullPath(Path.Combine(
4156
_fileSystem.Storage.CurrentDirectory,
4257
path));

Tests/Testably.Abstractions.Tests/FileSystem/Path/GetFullPathTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,49 @@ public abstract partial class GetFullPathTests<TFileSystem>
55
: FileSystemTestBase<TFileSystem>
66
where TFileSystem : IFileSystem
77
{
8+
[SkippableFact]
9+
public void GetFullPath_Dot_ShouldReturnToCurrentDirectory()
10+
{
11+
string expectedFullPath = FileSystem.Directory.GetCurrentDirectory();
12+
13+
string result = FileSystem.Path.GetFullPath(".");
14+
15+
result.Should().Be(expectedFullPath);
16+
}
17+
18+
[SkippableFact]
19+
public void GetFullPath_RelativePathWithDrive_ShouldReturnExpectedValue()
20+
{
21+
Skip.IfNot(Test.RunsOnWindows);
22+
23+
string currentDirectory = FileSystem.Directory.GetCurrentDirectory();
24+
string drive = currentDirectory.Substring(0, 1);
25+
string input = $"{drive}:test.txt";
26+
string expectedFullPath = FileSystem.Path.Combine(currentDirectory, "test.txt");
27+
28+
string result = FileSystem.Path.GetFullPath(input);
29+
30+
result.Should().Be(expectedFullPath);
31+
}
32+
33+
[SkippableFact]
34+
public void
35+
GetFullPath_RelativePathWithDrive_WhenCurrentDirectoryIsDifferent_ShouldReturnExpectedValue()
36+
{
37+
Skip.IfNot(Test.RunsOnWindows);
38+
39+
string currentDirectory = FileSystem.Directory.GetCurrentDirectory();
40+
string otherDrive = currentDirectory
41+
.Substring(0,1)
42+
.Equals("x", StringComparison.OrdinalIgnoreCase) ? "Y" : "X";
43+
string input = $"{otherDrive}:test.txt";
44+
string expectedFullPath = $@"{otherDrive}:\test.txt";
45+
46+
string result = FileSystem.Path.GetFullPath(input);
47+
48+
result.Should().Be(expectedFullPath);
49+
}
50+
851
[SkippableTheory]
952
[InlineData(@"top/../most/file", @"most/file")]
1053
[InlineData(@"top/../most/../dir/file", @"dir/file")]

0 commit comments

Comments
 (0)