Fix issues with parentable path on Linux

This commit is contained in:
Matt Nadareski
2024-04-16 11:50:31 -04:00
parent 1bdb483205
commit c19b59dc1c
2 changed files with 26 additions and 3 deletions

View File

@@ -18,6 +18,14 @@ namespace SabreTools.IO.Test
[InlineData("C:\\Directory\\SubDir\\Filename.ext", "C:\\Directory", true, "SubDir-Filename.ext")]
public void NormalizedFileNameTest(string current, string? parent, bool sanitize, string? expected)
{
// Hack to support Windows paths on Linux for testing only
if (System.IO.Path.DirectorySeparatorChar == '/')
{
current = current.Replace('\\', '/');
parent = parent?.Replace('\\', '/');
expected = expected?.Replace('\\', '/');
}
var path = new ParentablePath(current, parent);
string? actual = path.GetNormalizedFileName(sanitize);
Assert.Equal(expected, actual);
@@ -62,6 +70,15 @@ namespace SabreTools.IO.Test
if (expected?.Contains("%cd%") == true)
expected = expected.Replace("%cd%", Environment.CurrentDirectory.TrimEnd('\\', '/'));
// Hack to support Windows paths on Linux for testing only
if (System.IO.Path.DirectorySeparatorChar == '/')
{
current = current.Replace('\\', '/');
parent = parent?.Replace('\\', '/');
outDir = outDir?.Replace('\\', '/');
expected = expected?.Replace('\\', '/');
}
var path = new ParentablePath(current, parent);
string? actual = path.GetOutputPath(outDir, inplace);
Assert.Equal(expected, actual);

View File

@@ -84,13 +84,19 @@ namespace SabreTools.IO
// If we are processing a path that is coming from a directory and we are outputting to the current directory, we want to get the subfolder to write to
if (outDir == Environment.CurrentDirectory)
workingParent = Path.GetDirectoryName(ParentPath ?? string.Empty) ?? string.Empty;
// Handle bizarre Windows-like paths on Linux
if (workingParent.EndsWith(":") && Path.DirectorySeparatorChar == '/')
workingParent += '/';
// Determine the correct subfolder based on the working parent directory
int extraLength = workingParent.EndsWith(":")
|| workingParent.EndsWith(Path.DirectorySeparatorChar.ToString())
|| workingParent.EndsWith(Path.AltDirectorySeparatorChar.ToString()) ? 0 : 1;
|| workingParent.EndsWith("\\")
|| workingParent.EndsWith("/") ? 0 : 1;
return Path.GetDirectoryName(Path.Combine(outDir!, CurrentPath.Remove(0, workingParent.Length + extraLength)));
string strippedPath = CurrentPath.Remove(0, workingParent.Length + extraLength);
string combinedPath = Path.Combine(outDir!, strippedPath);
return Path.GetDirectoryName(combinedPath);
}
/// <summary>