From: Joel Rosdahl Date: Mon, 17 Jul 2023 07:10:16 +0000 (+0200) Subject: refactor: Use std::filesystem::current_path X-Git-Tag: v4.9~112 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ffd5a5864fc892f4673ebc512466b0b42f091a64;p=thirdparty%2Fccache.git refactor: Use std::filesystem::current_path --- diff --git a/src/util/filesystem.hpp b/src/util/filesystem.hpp index 45c2d0add..3d4c25ac6 100644 --- a/src/util/filesystem.hpp +++ b/src/util/filesystem.hpp @@ -105,6 +105,7 @@ DEF_WRAP_1_R(create_directories, bool, const path&, p) DEF_WRAP_1_R(create_directory, bool, const path&, p) DEF_WRAP_2_V(create_hard_link, void, const path&, target, const path&, link) DEF_WRAP_0_R(current_path, path) +DEF_WRAP_1_V(current_path, void, const path&, p) DEF_WRAP_1_P(exists, bool, const path&, p) DEF_WRAP_1_P(is_directory, bool, const path&, p) DEF_WRAP_1_R(read_symlink, path, const path&, p) diff --git a/unittest/TestUtil.cpp b/unittest/TestUtil.cpp index 362624ba9..0f0a489d8 100644 --- a/unittest/TestUtil.cpp +++ b/unittest/TestUtil.cpp @@ -44,25 +44,16 @@ TestContext::TestContext() : m_test_dir(util::actual_cwd()) ++m_subdir_counter; std::string subtest_dir = FMT("{}/test_{}", m_test_dir, m_subdir_counter); fs::create_directories(subtest_dir); - if (chdir(subtest_dir.c_str()) != 0) { + if (!fs::current_path(subtest_dir)) { abort(); } } TestContext::~TestContext() { - if (chdir(m_test_dir.c_str()) != 0) { + if (!fs::current_path(m_test_dir)) { abort(); } } -void -check_chdir(const std::string& dir) -{ - if (chdir(dir.c_str()) != 0) { - throw core::Error( - FMT("failed to change directory to {}: {}", dir, strerror(errno))); - } -} - } // namespace TestUtil diff --git a/unittest/TestUtil.hpp b/unittest/TestUtil.hpp index 6be4969b2..a3588a841 100644 --- a/unittest/TestUtil.hpp +++ b/unittest/TestUtil.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2021 Joel Rosdahl and other contributors +// Copyright (C) 2020-2023 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -40,7 +40,4 @@ private: static size_t m_subdir_counter; }; -// Change directory to `dir`, throwing Error on failure. -void check_chdir(const std::string& dir); - } // namespace TestUtil diff --git a/unittest/main.cpp b/unittest/main.cpp index ef11fcb1b..4a97f2d94 100644 --- a/unittest/main.cpp +++ b/unittest/main.cpp @@ -17,7 +17,6 @@ // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #include "../src/fmtmacros.hpp" -#include "TestUtil.hpp" #include #include @@ -43,14 +42,14 @@ main(int argc, char** argv) std::string testdir = FMT("testdir/{}", getpid()); fs::remove_all(testdir); fs::create_directories(testdir); - TestUtil::check_chdir(testdir); + fs::current_path(testdir); doctest::Context context; context.applyCommandLine(argc, argv); int result = context.run(); if (result == 0) { - TestUtil::check_chdir(dir_before); + fs::current_path(dir_before); fs::remove_all(testdir); } else { PRINT(stderr, "Note: Test data has been left in {}\n", testdir); diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index 065657836..31aa068f4 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -251,7 +251,7 @@ TEST_CASE("Util::make_relative_path") #ifndef _WIN32 REQUIRE(symlink("d", "s") == 0); #endif - REQUIRE(chdir("d") == 0); + REQUIRE(fs::current_path("d")); util::setenv("PWD", apparent_cwd); SUBCASE("No base directory")