]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add util::to_absolute_path
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 4 Jul 2021 05:12:53 +0000 (07:12 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 4 Jul 2021 10:12:16 +0000 (12:12 +0200)
src/util/path_utils.cpp
src/util/path_utils.hpp
test/suites/base.bash
unittest/test_util_path_utils.cpp

index ef0ffa3881a03abdfea8c2dd4dd1d04145cbf2ba..de5f81f467c6e387fbae4e67f550ab4322bcde26 100644 (file)
@@ -36,4 +36,15 @@ is_absolute_path(nonstd::string_view path)
   return !path.empty() && path[0] == '/';
 }
 
+std::string
+to_absolute_path(nonstd::string_view path)
+{
+  if (util::is_absolute_path(path)) {
+    return to_string(path);
+  } else {
+    return Util::normalize_absolute_path(
+      FMT("{}/{}", Util::get_actual_cwd(), path));
+  }
+}
+
 } // namespace util
index 9423e57ba68e7d0c2e1c49e56ac24d619da9ca46..3ef23cbaf18a71bb4d24119a9c74225327e9560b 100644 (file)
@@ -27,4 +27,7 @@ namespace util {
 // Return whether `path` is absolute.
 bool is_absolute_path(nonstd::string_view path);
 
+// Make `path` an absolute path.
+std::string to_absolute_path(nonstd::string_view path);
+
 } // namespace util
index 948e5a9979b3c7656002b32ac47e388eb84fa17c..fd26f8be3a9a90d4ac1ec2be75d0b04289941d03 100644 (file)
@@ -358,6 +358,12 @@ fi
     expect_stat 'unsupported compiler option' 1
     expect_exists test1.o.ccache-log
 
+    # -------------------------------------------------------------------------
+    TEST "CCACHE_DEBUGDIR"
+
+    CCACHE_DEBUG=1 CCACHE_DEBUGDIR=debugdir $CCACHE_COMPILE -c test1.c
+    expect_contains debugdir"$(pwd -P)"/test1.o.ccache-log "Result: cache miss"
+
     # -------------------------------------------------------------------------
     TEST "CCACHE_DISABLE"
 
index 8f9e3f0ad8f3bbddbbfd918861b22a48ae7bc8d4..0d6a773e1a2338a5b52c3dfc808b5e7d08ee7b4e 100644 (file)
@@ -16,6 +16,8 @@
 // this program; if not, write to the Free Software Foundation, Inc., 51
 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
+#include <Util.hpp>
+#include <fmtmacros.hpp>
 #include <util/path_utils.hpp>
 
 #include <third_party/doctest.h>
@@ -35,3 +37,21 @@ TEST_CASE("util::is_absolute_path")
   CHECK(!util::is_absolute_path(""));
   CHECK(!util::is_absolute_path("foo/fie"));
 }
+
+TEST_CASE("util::to_absolute_path")
+{
+  CHECK(util::to_absolute_path("/foo/bar") == "/foo/bar");
+
+#ifdef _WIN32
+  CHECK(util::to_absolute_path("C:\\foo\\bar") == "C:\\foo\\bar");
+#endif
+
+  const auto cwd = Util::get_actual_cwd();
+
+  CHECK(util::to_absolute_path("") == cwd);
+  CHECK(util::to_absolute_path(".") == cwd);
+  CHECK(util::to_absolute_path("..") == Util::dir_name(cwd));
+  CHECK(util::to_absolute_path("foo") == FMT("{}/foo", cwd));
+  CHECK(util::to_absolute_path("../foo/bar")
+        == FMT("{}/foo/bar", Util::dir_name(cwd)));
+}