Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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