Skip to content

Commit

Permalink
fix: implementation of Path.GetRelativePath (#336)
Browse files Browse the repository at this point in the history
Prototype implementation for
TestableIO/System.IO.Abstractions#773, similar
to [the implementation in .NET
Runtime](https://github.com/dotnet/runtime/blob/release/7.0/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs#L886)

---------

Co-authored-by: Valentin Breuß <v.breuss@tig.at>
  • Loading branch information
vbreuss and vbtig authored Jul 24, 2023
1 parent 3cabe7d commit a7e455a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public string GetRandomFileName()

#if FEATURE_PATH_RELATIVE
/// <inheritdoc cref="Path.GetRelativePath(string, string)" />
public string GetRelativePath(string relativeTo, string path)
public virtual string GetRelativePath(string relativeTo, string path)
=> Path.GetRelativePath(relativeTo, path);
#endif

Expand Down
14 changes: 14 additions & 0 deletions Source/Testably.Abstractions.Testing/FileSystem/PathMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,18 @@ public override string GetFullPath(string path)
_fileSystem.Storage.CurrentDirectory,
path));
}

#if FEATURE_PATH_RELATIVE
/// <inheritdoc cref="IPath.GetRelativePath(string, string)" />
public override string GetRelativePath(string relativeTo, string path)
{
relativeTo.EnsureValidArgument(FileSystem, nameof(relativeTo));
path.EnsureValidArgument(FileSystem, nameof(path));

relativeTo = FileSystem.Path.GetFullPath(relativeTo);
path = FileSystem.Path.GetFullPath(path);

return Path.GetRelativePath(relativeTo, path);
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ public void GetRelativePath_CommonParentDirectory_ShouldReturnRelativePath(
public void GetRelativePath_DifferentDrives_ShouldReturnAbsolutePath(
string path1, string path2)
{
if (!Test.RunsOnWindows)
{
// Different drives are only supported on Windows
return;
}
Skip.IfNot(Test.RunsOnWindows, "Different drives are only supported on Windows");

path1 = FileTestHelper.RootDrive(path1, 'A');
path2 = FileTestHelper.RootDrive(path2, 'B');
Expand All @@ -38,6 +34,18 @@ public void GetRelativePath_DifferentDrives_ShouldReturnAbsolutePath(
result.Should().Be(path2);
}

[Fact]
public void GetRelativePath_FromAbsolutePathInCurrentDirectory_ShouldReturnRelativePath()
{
string rootedPath = FileSystem.Path.Combine(BasePath, "input");
FileSystem.Directory.CreateDirectory(rootedPath);
FileSystem.Directory.SetCurrentDirectory(rootedPath);

string result = FileSystem.Path.GetRelativePath(rootedPath, "a.txt");

Assert.Equal("a.txt", result);
}

[SkippableTheory]
[AutoData]
public void GetRelativePath_RootedPath_ShouldReturnAbsolutePath(
Expand All @@ -56,7 +64,7 @@ public void GetRelativePath_RootedPath_ShouldReturnAbsolutePath(
public void GetRelativePath_RootedPath_ShouldWorkOnAnyDrive()
{
Skip.IfNot(Test.RunsOnWindows);

string rootedPath = "/dir/subDirectory";
FileSystem.Directory.CreateDirectory(rootedPath);
string directory = FileSystem.Directory.GetDirectories("/dir").Single();
Expand Down

0 comments on commit a7e455a

Please sign in to comment.