From a7e455a785d6d7c05ed9c5144c3983e29b57c494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Mon, 24 Jul 2023 07:30:12 +0200 Subject: [PATCH] fix: implementation of `Path.GetRelativePath` (#336) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prototype implementation for https://github.com/TestableIO/System.IO.Abstractions/issues/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ß --- .../Helpers/PathSystemBase.cs | 2 +- .../FileSystem/PathMock.cs | 14 +++++++++++++ .../FileSystem/Path/GetRelativePathTests.cs | 20 +++++++++++++------ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Source/Testably.Abstractions.Interface/Helpers/PathSystemBase.cs b/Source/Testably.Abstractions.Interface/Helpers/PathSystemBase.cs index 03cb8d8f1..e6e280b70 100644 --- a/Source/Testably.Abstractions.Interface/Helpers/PathSystemBase.cs +++ b/Source/Testably.Abstractions.Interface/Helpers/PathSystemBase.cs @@ -146,7 +146,7 @@ public string GetRandomFileName() #if FEATURE_PATH_RELATIVE /// - public string GetRelativePath(string relativeTo, string path) + public virtual string GetRelativePath(string relativeTo, string path) => Path.GetRelativePath(relativeTo, path); #endif diff --git a/Source/Testably.Abstractions.Testing/FileSystem/PathMock.cs b/Source/Testably.Abstractions.Testing/FileSystem/PathMock.cs index a3b10b295..f1789cb4c 100644 --- a/Source/Testably.Abstractions.Testing/FileSystem/PathMock.cs +++ b/Source/Testably.Abstractions.Testing/FileSystem/PathMock.cs @@ -41,4 +41,18 @@ public override string GetFullPath(string path) _fileSystem.Storage.CurrentDirectory, path)); } + +#if FEATURE_PATH_RELATIVE + /// + 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 } diff --git a/Tests/Testably.Abstractions.Tests/FileSystem/Path/GetRelativePathTests.cs b/Tests/Testably.Abstractions.Tests/FileSystem/Path/GetRelativePathTests.cs index 458bb69c4..e4baa0f91 100644 --- a/Tests/Testably.Abstractions.Tests/FileSystem/Path/GetRelativePathTests.cs +++ b/Tests/Testably.Abstractions.Tests/FileSystem/Path/GetRelativePathTests.cs @@ -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'); @@ -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( @@ -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();