Move with_directory and with_temporary_directory to test_utils.

This commit is contained in:
Joe Thornber 2013-05-08 11:47:15 +01:00
parent bac19b3c94
commit f6acd473a7
2 changed files with 68 additions and 67 deletions

View File

@ -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<with_directory> dir_;
};
//--------------------------------
class metadata_builder {

View File

@ -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<with_directory> dir_;
};
}
//----------------------------------------------------------------