Add Unix implementation matching docs for GetRelativePath, add tests
This commit is contained in:
parent
f26ba1dca5
commit
68659055cb
src
BizHawk.Common/Extensions
BizHawk.Tests/PlatformTests/Common.PathExtensions
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue