Skip to content

Commit

Permalink
[Paths] Remove lower casing of paths which can cause issues on case s…
Browse files Browse the repository at this point in the history
…ensitive platforms
  • Loading branch information
kudaba committed Feb 8, 2022
1 parent ccca5aa commit 393fc93
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
34 changes: 11 additions & 23 deletions Sharpmake.UnitTests/UtilTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,35 +62,27 @@ public void LeavesEmptyStringsUntouched()
public void LeavesVariablesUntouched()
{
string expectedResult = "$(Console_SdkPackagesRoot)";
if (!Util.IsRunningInMono())
expectedResult = expectedResult.ToLower();
Assert.That(Util.PathMakeStandard("$(Console_SdkPackagesRoot)"), Is.EqualTo(expectedResult));
}

[Test]
public void ProcessesPathWithTrailingBackslash()
{
string expectedResult = Path.Combine("rd", "project", "dev", "projects", "sharpmake", "..", "..", "extern", "Geometrics");
if (!Util.IsRunningInMono())
expectedResult = expectedResult.ToLower();
Assert.That(Util.PathMakeStandard(@"rd\project\dev\projects\sharpmake\..\..\extern\Geometrics\"), Is.EqualTo(expectedResult));
}

[Test]
public void ProcessesPathWithTrailingBackslashAndADot()
{
var expectedResult = Path.Combine("rd", "project", "dev", "projects", "sharpmake", "..", "..", "extern", "Microsoft.CNG", "Lib");
if (!Util.IsRunningInMono())
expectedResult = expectedResult.ToLower();
Assert.That(Util.PathMakeStandard(@"rd\project\dev\projects\sharpmake\..\..\extern\Microsoft.CNG\Lib\"), Is.EqualTo(expectedResult));
}

[Test]
public void ProcessesPathWithMultipleTrailingBackslashes()
{
var expectedResult = Path.Combine("rd", "project", "dev", "projects", "sharpmake", "..", "..", "extern", "Microsoft.CNG", "Lib");
if (!Util.IsRunningInMono())
expectedResult = expectedResult.ToLower();
Assert.That(Util.PathMakeStandard(@"rd\project\dev\projects\sharpmake\..\..\extern\Microsoft.CNG\Lib\\\"), Is.EqualTo(expectedResult));
}

Expand All @@ -108,9 +100,6 @@ public void ProcessesWithAListAsArgument()
};
var expectedList = listString;

if (!Util.IsRunningInMono())
expectedList = expectedList.Select((p) => p.ToLower()).ToList();

Util.PathMakeStandard(listString);

Assert.AreEqual(expectedList, listString);
Expand Down Expand Up @@ -753,7 +742,7 @@ public void CanBeComputedFromOutputPath()
var referenceFileFullPath = outputFileFullPath.ReplaceHeadPath(outputPath, referencePath);

Assert.That(referenceFileFullPath, Is.EqualTo(
Util.PathMakeStandard(@"F:\OnePath\With\Reference\with\a\file.cs", false)));
Util.PathMakeStandard(@"F:\OnePath\With\Reference\with\a\file.cs")));
}

[Test]
Expand All @@ -766,7 +755,7 @@ public void IsCaseInsensitiveButPreservesCase()
var referenceFileFullPath = outputFileFullPath.ReplaceHeadPath(outputPath, referencePath);

Assert.That(referenceFileFullPath, Is.EqualTo(
Util.PathMakeStandard(@"F:\OnePath\with\Reference\with\a\File.cs", false)));
Util.PathMakeStandard(@"F:\OnePath\with\Reference\with\a\File.cs")));
}

[Test]
Expand All @@ -779,7 +768,7 @@ public void AcceptsOutputPathWithoutTrailingSlash()
var referenceFileFullPath = outputFileFullPath.ReplaceHeadPath(outputPath, referencePath);

Assert.That(referenceFileFullPath, Is.EqualTo(
Util.PathMakeStandard(@"F:\OnePath\With\Reference\with\a\file.cs", false)));
Util.PathMakeStandard(@"F:\OnePath\With\Reference\with\a\file.cs")));
}
}

Expand Down Expand Up @@ -1000,7 +989,7 @@ public void PathGetAbsoluteStringsReturnList()
string filename = Path.GetFileName(mockPath);
string stringsSource = Path.GetDirectoryName(mockPath);

Assert.AreEqual(Path.Combine(stringsSource.ToLower(), filename), Util.PathGetAbsolute(stringsSource, new Strings(filename))[0]);
Assert.AreEqual(Path.Combine(stringsSource, filename), Util.PathGetAbsolute(stringsSource, new Strings(filename))[0]);

File.Delete(mockPath);
}
Expand All @@ -1015,7 +1004,7 @@ public void PathGetAbsoluteStringsReturnString()
var separator = Path.DirectorySeparatorChar;
string stringsSource = mockPath.Substring(0, mockPath.IndexOf(separator));
string stringsDest = mockPath.Substring(mockPath.IndexOf(separator, mockPath.IndexOf(separator) + 1));
string expectedOutputPath = stringsSource.ToLower() + stringsDest;
string expectedOutputPath = stringsSource + stringsDest;
//v
Assert.AreEqual(expectedOutputPath, Util.PathGetAbsolute(stringsSource, stringsDest));

Expand All @@ -1032,7 +1021,6 @@ public void ResolvePathString()
Strings paths = new Strings(Path.GetDirectoryName(mockPath));
string root = mockPath.Substring(0, mockPath.IndexOf(Path.DirectorySeparatorChar));
Strings expectedOutputPath = new Strings(paths.Select((p) => Path.Combine(root, p)));
expectedOutputPath.ToLower();

Util.ResolvePath(root, ref paths);

Expand All @@ -1057,7 +1045,7 @@ public void ResolvePathOrderableString()
mockPath3.Substring(mockPath3.IndexOf(Path.DirectorySeparatorChar)),
};
string root = mockPath1.Substring(0, mockPath1.IndexOf(Path.DirectorySeparatorChar));
OrderableStrings expectedOutputPath = new OrderableStrings(paths.Select((p) => (root + p).ToLower()));
OrderableStrings expectedOutputPath = new OrderableStrings(paths.Select((p) => (root + p)));

Util.ResolvePath(root, ref paths);
expectedOutputPath.Sort();
Expand Down Expand Up @@ -1114,8 +1102,8 @@ public void PathGetRelativeOrderableStrings()
Util.PathMakeStandard(stringsDest);
OrderableStrings listResult = Util.PathGetRelative(stringsSource, stringsDest, false);

Assert.AreEqual(Util.PathMakeStandard(@"..\Sharpmake.Generators\Generic", !Util.IsRunningInMono()), listResult[0]);
Assert.AreEqual(Util.PathMakeStandard(@"subdir\test.txt", !Util.IsRunningInMono()), listResult[1]);
Assert.AreEqual(Util.PathMakeStandard(@"..\Sharpmake.Generators\Generic"), listResult[0]);
Assert.AreEqual(Util.PathMakeStandard(@"subdir\test.txt"), listResult[1]);
Assert.AreEqual("test2.txt", listResult[2]);
}

Expand Down Expand Up @@ -1192,11 +1180,11 @@ public void GetConvertedRelativePathRoot()
var root = @"C:\SharpMake\sharpmake\Sharpmake.Application\Properties";

var newRelativeToFullPath = "";
Assert.AreEqual(Path.Combine(absolutePath.ToLower(), fileName), Util.GetConvertedRelativePath(absolutePath, fileName, newRelativeToFullPath, false, null));
Assert.AreEqual(Path.Combine(absolutePath, fileName), Util.GetConvertedRelativePath(absolutePath, fileName, newRelativeToFullPath, false, null));
Assert.AreEqual(mockPath, Util.GetConvertedRelativePath(absolutePath, mockPath, newRelativeToFullPath, false, root));
Assert.AreEqual(Path.Combine(absolutePath.ToLower(), fileName), Util.GetConvertedRelativePath(absolutePath, fileName, newRelativeToFullPath, false, ""));
Assert.AreEqual(Path.Combine(absolutePath, fileName), Util.GetConvertedRelativePath(absolutePath, fileName, newRelativeToFullPath, false, ""));
Assert.AreEqual(absolutePath, Util.GetConvertedRelativePath(absolutePath, null, newRelativeToFullPath, false, null));
Assert.AreEqual(Path.Combine(root.ToLower(), Path.GetTempPath()), Util.GetConvertedRelativePath(root, Path.GetTempPath(), newRelativeToFullPath, false, null));
Assert.AreEqual(Path.Combine(root, Path.GetTempPath()), Util.GetConvertedRelativePath(root, Path.GetTempPath(), newRelativeToFullPath, false, null));

File.Delete(mockPath);
}
Expand Down
14 changes: 4 additions & 10 deletions Sharpmake/PathUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,10 @@ public static void PathMakeStandard(IList<string> paths)
}

public static string PathMakeStandard(string path)
{
return PathMakeStandard(path, !Util.IsRunningOnUnix());
}

public static string PathMakeStandard(string path, bool forceToLower)
{
// cleanup the path by replacing the other separator by the correct one for this OS
// then trim every trailing separators
var standardPath = path.Replace(OtherSeparator, Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar);
return forceToLower ? standardPath.ToLower() : standardPath;
return path.Replace(OtherSeparator, Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar);
}

public static string EnsureTrailingSeparator(string path)
Expand Down Expand Up @@ -797,9 +791,9 @@ internal static string ConvertToMountedUnixPath(string path)
public static string ReplaceHeadPath(this string fullInputPath, string inputHeadPath, string replacementHeadPath)
{
// Normalize paths before comparing and combining them, to prevent false mismatch between '\\' and '/'.
fullInputPath = Util.PathMakeStandard(fullInputPath, false);
inputHeadPath = Util.PathMakeStandard(inputHeadPath, false);
replacementHeadPath = Util.PathMakeStandard(replacementHeadPath, false);
fullInputPath = PathMakeStandard(fullInputPath);
inputHeadPath = PathMakeStandard(inputHeadPath);
replacementHeadPath = PathMakeStandard(replacementHeadPath);

inputHeadPath = EnsureTrailingSeparator(inputHeadPath);

Expand Down

0 comments on commit 393fc93

Please sign in to comment.