summaryrefslogtreecommitdiffstats
path: root/src/disk_interface_test.cc
diff options
context:
space:
mode:
authorEvan Martin <martine@danga.com>2012-01-06 19:34:10 (GMT)
committerEvan Martin <martine@danga.com>2012-01-06 19:34:10 (GMT)
commit36660e473b7f38317a38cc5a297ba18166191cf5 (patch)
tree063a8bc98ce298b911a81193ee2999ed2be03d9d /src/disk_interface_test.cc
parentc633f316d74375b49808a0e98511ff224d519746 (diff)
downloadNinja-36660e473b7f38317a38cc5a297ba18166191cf5.zip
Ninja-36660e473b7f38317a38cc5a297ba18166191cf5.tar.gz
Ninja-36660e473b7f38317a38cc5a297ba18166191cf5.tar.bz2
migrate tempdir code to test.cc
Diffstat (limited to 'src/disk_interface_test.cc')
-rw-r--r--src/disk_interface_test.cc75
1 files changed, 4 insertions, 71 deletions
diff --git a/src/disk_interface_test.cc b/src/disk_interface_test.cc
index c431e75..b8ad5e6 100644
--- a/src/disk_interface_test.cc
+++ b/src/disk_interface_test.cc
@@ -23,87 +23,20 @@
#include "graph.h"
#include "test.h"
-using namespace std;
-
namespace {
-#ifdef _WIN32
-#ifndef _mktemp_s
-/// mingw has no mktemp. Implement one with the same type as the one
-/// found in the Windows API.
-int _mktemp_s(char* templ) {
- char* ofs = strchr(templ, 'X');
- sprintf(ofs, "%d", rand() % 1000000);
- return 0;
-}
-#endif
-
-/// Windows has no mkdtemp. Implement it in terms of _mktemp_s.
-char* mkdtemp(char* name_template) {
- int err = _mktemp_s(name_template);
- if (err < 0) {
- perror("_mktemp_s");
- return NULL;
- }
-
- err = _mkdir(name_template);
- if (err < 0) {
- perror("mkdir");
- return NULL;
- }
-
- return name_template;
-}
-#endif
-
class DiskInterfaceTest : public testing::Test {
public:
virtual void SetUp() {
- // Because we do real disk accesses, we create a temp dir within
- // the system temporary directory.
-
- // First change into the system temp dir and save it for cleanup.
- start_dir_ = GetSystemTempDir();
- ASSERT_EQ(0, chdir(start_dir_.c_str()));
-
- // Then create and change into a temporary subdirectory of that.
- temp_dir_name_ = MakeTempDir();
- ASSERT_FALSE(temp_dir_name_.empty());
- ASSERT_EQ(0, chdir(temp_dir_name_.c_str()));
+ // These tests do real disk accesses, so create a temp dir.
+ temp_dir_.CreateAndEnter("Ninja-DiskInterfaceTest");
}
virtual void TearDown() {
- // Move out of the directory we're about to clobber.
- ASSERT_EQ(0, chdir(start_dir_.c_str()));
-#ifdef _WIN32
- ASSERT_EQ(0, system(("rmdir /s /q " + temp_dir_name_).c_str()));
-#else
- ASSERT_EQ(0, system(("rm -rf " + temp_dir_name_).c_str()));
-#endif
- }
-
- string GetSystemTempDir() {
-#ifdef _WIN32
- char buf[1024];
- if (!GetTempPath(sizeof(buf), buf))
- return "";
- return buf;
-#else
- const char* tempdir = getenv("TMPDIR");
- if (tempdir)
- return tempdir;
- return "/tmp";
-#endif
- }
-
- string MakeTempDir() {
- char name_template[] = "DiskInterfaceTest-XXXXXX";
- char* name = mkdtemp(name_template);
- return name ? name : "";
+ temp_dir_.Cleanup();
}
- string start_dir_;
- string temp_dir_name_;
+ ScopedTempDir temp_dir_;
RealDiskInterface disk_;
};