FileAPI: Fix paths relative to root directory

We normalize directory paths to not end in `/`.  However, a root
directory path always ends in `/`.  Avoid removing the first character
after it.

Fixes: #27308
This commit is contained in:
Richard
2025-10-22 09:25:39 -04:00
committed by Brad King
parent 3f7f2368d6
commit 41be8cb44a
2 changed files with 46 additions and 1 deletions
+1 -1
View File
@@ -2034,7 +2034,7 @@ std::string cmSystemTools::RelativeIfUnder(std::string const& top,
if (in == top) {
out = ".";
} else if (cmSystemTools::IsSubDirectory(in, top)) {
out = in.substr(top.size() + 1);
out = in.substr(top.size() + (top.back() == '/' ? 0 : 1));
} else {
out = in;
}
+45
View File
@@ -96,6 +96,50 @@ static bool testStrVersCmp()
return true;
}
static bool testRelativeIfUnder()
{
std::cout << "testRelativeIfUnder()\n";
std::string result = cmSystemTools::RelativeIfUnder("/a/b", "/a/b");
if (result != ".") {
std::cout << "cmSystemTools::RelativeIfUnder failed on /a/b, /a/b: "
<< result << "\n";
return false;
}
result = cmSystemTools::RelativeIfUnder("/a/b", "/a/b/c/d");
if (result != "c/d") {
std::cout << "cmSystemTools::RelativeIfUnder failed on /a/b, /a/b/c/d: "
<< result << "\n";
return false;
}
result = cmSystemTools::RelativeIfUnder("/d/f/", "/a/b/c/d");
if (result != "/a/b/c/d") {
std::cout << "cmSystemTools::RelativeIfUnder failed on /d/f/, /a/b/c/d: "
<< result << "\n";
return false;
}
result = cmSystemTools::RelativeIfUnder("/", "/a/b");
if (result != "a/b") {
std::cout << "cmSystemTools::RelativeIfUnder failed on /, /a/b: " << result
<< "\n";
return false;
}
result = cmSystemTools::RelativeIfUnder("I:/", "I:/CMakeLists.txt");
if (result != "CMakeLists.txt") {
std::cout
<< "cmSystemTools::RelativeIfUnder failed on I:/, I:/CMakeLists.txt: "
<< result << "\n";
return false;
}
result = cmSystemTools::RelativeIfUnder("", "/a/b");
if (result != "/a/b") {
std::cout << "cmSystemTools::RelativeIfUnder failed on \"\", /a/b: "
<< result << "\n";
return false;
}
return true;
}
static bool testMakeTempDirectory()
{
std::cout << "testMakeTempDirectory()\n";
@@ -124,6 +168,7 @@ int testSystemTools(int /*unused*/, char* /*unused*/[])
testUpperCase,
testVersionCompare,
testStrVersCmp,
testRelativeIfUnder,
testMakeTempDirectory,
});
}