From: Joel Rosdahl Date: Sun, 4 Jul 2021 05:12:53 +0000 (+0200) Subject: Add util::to_absolute_path X-Git-Tag: v4.4~161 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f8b256b6f6041e6cb79491fb811f0c4f77f4cf15;p=thirdparty%2Fccache.git Add util::to_absolute_path --- diff --git a/src/util/path_utils.cpp b/src/util/path_utils.cpp index ef0ffa388..de5f81f46 100644 --- a/src/util/path_utils.cpp +++ b/src/util/path_utils.cpp @@ -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 diff --git a/src/util/path_utils.hpp b/src/util/path_utils.hpp index 9423e57ba..3ef23cbaf 100644 --- a/src/util/path_utils.hpp +++ b/src/util/path_utils.hpp @@ -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 diff --git a/test/suites/base.bash b/test/suites/base.bash index 948e5a997..fd26f8be3 100644 --- a/test/suites/base.bash +++ b/test/suites/base.bash @@ -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" diff --git a/unittest/test_util_path_utils.cpp b/unittest/test_util_path_utils.cpp index 8f9e3f0ad..0d6a773e1 100644 --- a/unittest/test_util_path_utils.cpp +++ b/unittest/test_util_path_utils.cpp @@ -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 +#include #include #include @@ -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))); +}