From 68659055cbd2c1664308dd9ab07277014b8d0580 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Sun, 31 Jan 2021 10:53:16 +1000 Subject: [PATCH] Add Unix implementation matching docs for GetRelativePath, add tests --- src/BizHawk.Common/Extensions/PathExtensions.cs | 8 ++++++-- .../Common.PathExtensions/PathExtensionTests.cs | 17 +++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/BizHawk.Common/Extensions/PathExtensions.cs b/src/BizHawk.Common/Extensions/PathExtensions.cs index e7c04b4ed5..e03f11057e 100644 --- a/src/BizHawk.Common/Extensions/PathExtensions.cs +++ b/src/BizHawk.Common/Extensions/PathExtensions.cs @@ -58,7 +58,11 @@ namespace BizHawk.Common.PathExtensions public static string? GetRelativePath(string? fromPath, string? toPath) { if (fromPath == null || toPath == null) return null; - if (OSTailoredCode.IsUnixHost) return toPath.MakeRelativeTo(fromPath); + if (OSTailoredCode.IsUnixHost) + { + var realpathOutput = OSTailoredCode.SimpleSubshell("realpath", $"--relative-to=\"{fromPath}\" \"{toPath}\"", $"invalid path {toPath}, invalid path {fromPath}, or missing realpath binary"); + return !realpathOutput.StartsWith("../") && realpathOutput != "." && realpathOutput != ".." ? $"./{realpathOutput}" : realpathOutput; + } //TODO merge this with the Windows implementation in MakeRelativeTo static FileAttributes GetPathAttribute(string path1) @@ -101,7 +105,7 @@ namespace BizHawk.Common.PathExtensions if (!absolutePath.IsSubfolderOf(basePath)) return absolutePath; if (!OSTailoredCode.IsUnixHost) return absolutePath.Replace(basePath, ".").RemoveSuffix(Path.DirectorySeparatorChar); #if true // Unix implementation using realpath - var realpathOutput = OSTailoredCode.SimpleSubshell("realpath", $"--relative-to=\"{basePath}\" \"{absolutePath}\"", $"invalid path {absolutePath}, invalid path {basePath}, or missing realpath binary"); + var realpathOutput = OSTailoredCode.SimpleSubshell("realpath", $"--relative-base=\"{basePath}\" \"{absolutePath}\"", $"invalid path {absolutePath}, invalid path {basePath}, or missing realpath binary"); return !realpathOutput.StartsWith("../") && realpathOutput != "." && realpathOutput != ".." ? $"./{realpathOutput}" : realpathOutput; #else // for some reason there were two Unix implementations in the codebase before me? --yoshi // alt. #1 diff --git a/src/BizHawk.Tests/PlatformTests/Common.PathExtensions/PathExtensionTests.cs b/src/BizHawk.Tests/PlatformTests/Common.PathExtensions/PathExtensionTests.cs index 297500a9ac..faa302efac 100644 --- a/src/BizHawk.Tests/PlatformTests/Common.PathExtensions/PathExtensionTests.cs +++ b/src/BizHawk.Tests/PlatformTests/Common.PathExtensions/PathExtensionTests.cs @@ -58,16 +58,25 @@ namespace BizHawk.Tests.Common.PathExtensions Assert.AreEqual(expectedIsSubfolder, childPath.IsSubfolderOf(parentPath)); } -#if false // don't bother, GetRelativePath simply calls MakeRelativeTo on Unix - public void TestGetRelativePathUnix() {} -#endif + [TestMethod] + [DataRow("./share", "/usr/share", "/usr")] + [DataRow(".", "/usr", "/usr")] + [DataRow("..", "/usr", "/usr/share")] + [DataRow("../bin", "/usr/bin", "/usr/share")] + [DataRow("../../etc", "/etc", "/usr/share")] + public void TestGetRelativePathUnix(string expectedRelPath, string toPath, string fromPath) // swapped here instead of in data + { + PlatformTestUtils.OnlyRunOnRealUnix(); + + Assert.AreEqual(expectedRelPath, PE.GetRelativePath(fromPath: fromPath, toPath: toPath)); + } [TestMethod] [DataRow("./share", "/usr/share", "/usr")] [DataRow(".", "/usr", "/usr")] [DataRow("/usr", "/usr", "/usr/share")] // not `..` [DataRow("/usr/bin", "/usr/bin", "/usr/share")] // not `../bin` - [DataRow("/bin", "/bin", "/usr/share")] // not `../../bin` + [DataRow("/etc", "/etc", "/usr/share")] // not `../../etc` public void TestMakeRelativeToUnix(string expectedRelPath, string absolutePath, string basePath) { PlatformTestUtils.OnlyRunOnRealUnix();