From f6acd473a71b037cb8c8406c063aa3627f6f0c18 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Wed, 8 May 2013 11:47:15 +0100 Subject: [PATCH] Move with_directory and with_temporary_directory to test_utils. --- unit-tests/metadata_checker_t.cc | 67 ------------------------------- unit-tests/test_utils.h | 68 ++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 67 deletions(-) diff --git a/unit-tests/metadata_checker_t.cc b/unit-tests/metadata_checker_t.cc index 51a8c54..e9fa9e6 100644 --- a/unit-tests/metadata_checker_t.cc +++ b/unit-tests/metadata_checker_t.cc @@ -25,73 +25,6 @@ namespace { block_address const BLOCK_SIZE = 4096; block_address const NR_BLOCKS = 102400; - // FIXME: move to utils - class with_directory { - public: - with_directory(std::string const &path) - : old_path_(pwd()) { - chdir(path); - } - - ~with_directory() { - chdir(old_path_); - } - - private: - std::string pwd() const { - char buffer[PATH_MAX]; - char *ptr = getcwd(buffer, sizeof(buffer)); - if (!ptr) { - // FIXME: still need a standard syscall failed exception - throw std::runtime_error("getcwd failed"); - } - - return ptr; - } - - void chdir(std::string const &path) { - int r = ::chdir(path.c_str()); - if (r < 0) - throw std::runtime_error("chdir failed"); - } - - std::string old_path_; - std::string new_path_; - }; - - class with_temp_directory { - public: - with_temp_directory() { - std::string name("./tmp"); - - rm_rf(name); - mkdir(name); - - dir_.reset(new with_directory(name)); - } - - private: - void rm_rf(std::string const &name) { - std::string cmd("rm -rf "); - cmd += name; - system(cmd); - } - - void mkdir(std::string const &name) { - std::string cmd("mkdir "); - cmd += name; - system(cmd); - } - - void system(std::string const &cmd) { - int r = ::system(cmd.c_str()); - if (r < 0) - throw std::runtime_error("system failed"); - } - - std::auto_ptr dir_; - }; - //-------------------------------- class metadata_builder { diff --git a/unit-tests/test_utils.h b/unit-tests/test_utils.h index caaa413..ee6d95f 100644 --- a/unit-tests/test_utils.h +++ b/unit-tests/test_utils.h @@ -41,6 +41,74 @@ namespace test { transaction_manager::ptr open_temporary_tm(block_manager<>::ptr bm); void zero_block(block_manager<>::ptr bm, block_address b); + + //-------------------------------- + + class with_directory { + public: + with_directory(std::string const &path) + : old_path_(pwd()) { + chdir(path); + } + + ~with_directory() { + chdir(old_path_); + } + + private: + std::string pwd() const { + char buffer[PATH_MAX]; + char *ptr = getcwd(buffer, sizeof(buffer)); + if (!ptr) { + // FIXME: still need a standard syscall failed exception + throw std::runtime_error("getcwd failed"); + } + + return ptr; + } + + void chdir(std::string const &path) { + int r = ::chdir(path.c_str()); + if (r < 0) + throw std::runtime_error("chdir failed"); + } + + std::string old_path_; + std::string new_path_; + }; + + class with_temp_directory { + public: + with_temp_directory() { + std::string name("./tmp"); + + rm_rf(name); + mkdir(name); + + dir_.reset(new with_directory(name)); + } + + private: + void rm_rf(std::string const &name) { + std::string cmd("rm -rf "); + cmd += name; + system(cmd); + } + + void mkdir(std::string const &name) { + std::string cmd("mkdir "); + cmd += name; + system(cmd); + } + + void system(std::string const &cmd) { + int r = ::system(cmd.c_str()); + if (r < 0) + throw std::runtime_error("system failed"); + } + + std::auto_ptr dir_; + }; } //----------------------------------------------------------------