1 #include "chdir.h" 2 3 #include <cstring> 4 #include <unistd.h> 5 6 #include "utils.h" 7 Chdir(const std::string & path)8TestHelpers::Chdir::Chdir(const std::string& path) 9 { 10 m_old_path = newsboat::utils::getcwd(); 11 const int result = ::chdir(path.c_str()); 12 if (result != 0) { 13 const auto saved_errno = errno; 14 auto msg = std::string("TestHelpers::Chdir: ") 15 + "couldn't change current directory to `" 16 + path 17 + "': (" 18 + std::to_string(saved_errno) 19 + ") " 20 + strerror(saved_errno); 21 throw std::runtime_error(msg); 22 } 23 } 24 ~Chdir()25TestHelpers::Chdir::~Chdir() 26 { 27 // Ignore the return value, because even if the call failed, we 28 // can't do anything useful. 29 const int result = ::chdir(m_old_path.c_str()); 30 (void)result; 31 } 32