summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKlaus Aehlig <klaus.aehlig@huawei.com>2022-05-11 16:49:27 +0200
committerKlaus Aehlig <klaus.aehlig@huawei.com>2022-05-12 09:10:51 +0200
commita79c1c5b8718347047ffbed4979c696d7a10484a (patch)
treee732d83e5a47af0a8389125e86912fc04d1c5437
parentb1b3f5bc981724a984ee4183ccb1fc456423411d (diff)
downloadjustbuild-a79c1c5b8718347047ffbed4979c696d7a10484a.tar.gz
Ensure consistent path normalisation
In particular, ensure that the empty path and "." have the same normal form.
-rw-r--r--src/utils/cpp/path.hpp5
-rw-r--r--test/utils/TARGETS6
-rw-r--r--test/utils/cpp/TARGETS13
-rw-r--r--test/utils/cpp/path.test.cpp20
4 files changed, 42 insertions, 2 deletions
diff --git a/src/utils/cpp/path.hpp b/src/utils/cpp/path.hpp
index 045ece0e..aa2c8350 100644
--- a/src/utils/cpp/path.hpp
+++ b/src/utils/cpp/path.hpp
@@ -7,7 +7,10 @@
-> std::filesystem::path {
auto n = p.lexically_normal();
if (not n.has_filename()) {
- return n.parent_path();
+ n = n.parent_path();
+ }
+ if (n.empty()) {
+ return std::filesystem::path{"."};
}
return n;
}
diff --git a/test/utils/TARGETS b/test/utils/TARGETS
index a0d637b1..c7c2f8fc 100644
--- a/test/utils/TARGETS
+++ b/test/utils/TARGETS
@@ -54,5 +54,9 @@
]
, "stage": ["test", "utils"]
}
-, "TESTS": {"type": "install", "tainted": ["test"]}
+, "TESTS":
+ { "type": "install"
+ , "tainted": ["test"]
+ , "dirs": [[["./", "cpp", "TESTS"], "cpp"]]
+ }
}
diff --git a/test/utils/cpp/TARGETS b/test/utils/cpp/TARGETS
new file mode 100644
index 00000000..5e11d258
--- /dev/null
+++ b/test/utils/cpp/TARGETS
@@ -0,0 +1,13 @@
+{ "path":
+ { "type": ["@", "rules", "CC/test", "test"]
+ , "name": ["path"]
+ , "srcs": ["path.test.cpp"]
+ , "deps":
+ [ ["@", "catch2", "", "catch2"]
+ , ["test", "catch-main"]
+ , ["src/utils/cpp", "path"]
+ ]
+ , "stage": ["test", "utils", "cpp"]
+ }
+, "TESTS": {"type": "install", "tainted": ["test"], "deps": ["path"]}
+}
diff --git a/test/utils/cpp/path.test.cpp b/test/utils/cpp/path.test.cpp
new file mode 100644
index 00000000..b0e7eff8
--- /dev/null
+++ b/test/utils/cpp/path.test.cpp
@@ -0,0 +1,20 @@
+#include <filesystem>
+
+#include "catch2/catch.hpp"
+#include "src/utils/cpp/path.hpp"
+
+TEST_CASE("normalization", "[path]") {
+ CHECK(ToNormalPath(std::filesystem::path{""}) ==
+ ToNormalPath(std::filesystem::path{"."}));
+ CHECK(ToNormalPath(std::filesystem::path{""}).string() == ".");
+ CHECK(ToNormalPath(std::filesystem::path{"."}).string() == ".");
+
+ CHECK(ToNormalPath(std::filesystem::path{"foo/bar/.."}).string() == "foo");
+ CHECK(ToNormalPath(std::filesystem::path{"foo/bar/../"}).string() == "foo");
+ CHECK(ToNormalPath(std::filesystem::path{"foo/bar/../baz"}).string() ==
+ "foo/baz");
+ CHECK(ToNormalPath(std::filesystem::path{"./foo/bar"}).string() ==
+ "foo/bar");
+ CHECK(ToNormalPath(std::filesystem::path{"foo/.."}).string() == ".");
+ CHECK(ToNormalPath(std::filesystem::path{"./foo/.."}).string() == ".");
+}