]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Use std::filesystem::current_path
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 17 Jul 2023 07:10:16 +0000 (09:10 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 18 Jul 2023 19:36:50 +0000 (21:36 +0200)
src/util/filesystem.hpp
unittest/TestUtil.cpp
unittest/TestUtil.hpp
unittest/main.cpp
unittest/test_Util.cpp

index 45c2d0add9703f433a099e26df9a4c085087ace7..3d4c25ac6399b63bd5e301131f263a9c7c7c983d 100644 (file)
@@ -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)
index 362624ba9652a23955836fdfa8e40062d0fe9679..0f0a489d895c16dbc9871570cb673b407a96fec6 100644 (file)
@@ -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
index 6be4969b239f991090010e708ccc759d5be0a580..a3588a84117ce9401c60adcd142aa6a0a64b9bf1 100644 (file)
@@ -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
index ef11fcb1bbde6f042e48f51bb00a4788844433a6..4a97f2d941f51b9a9f22e1751b559831908a71e1 100644 (file)
@@ -17,7 +17,6 @@
 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 #include "../src/fmtmacros.hpp"
-#include "TestUtil.hpp"
 
 #include <util/environment.hpp>
 #include <util/filesystem.hpp>
@@ -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);
index 065657836670698e75525ba2ccb2b89837d988a8..31aa068f4a7752745cdb9473509776c857c81017 100644 (file)
@@ -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")