diff --git a/SabreTools.IO.Test/ParentablePathTests.cs b/SabreTools.IO.Test/ParentablePathTests.cs index 05c4272..29969f5 100644 --- a/SabreTools.IO.Test/ParentablePathTests.cs +++ b/SabreTools.IO.Test/ParentablePathTests.cs @@ -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); diff --git a/SabreTools.IO/ParentablePath.cs b/SabreTools.IO/ParentablePath.cs index 6a240f9..8ecb49d 100644 --- a/SabreTools.IO/ParentablePath.cs +++ b/SabreTools.IO/ParentablePath.cs @@ -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); } ///