35 int _mktemp_s(
char* templ) {
36 char* ofs = strchr(templ,
'X');
37 sprintf(ofs,
"%d", rand() % 1000000);
43 char* mkdtemp(
char* name_template) {
44 int err = _mktemp_s(name_template);
50 err = _mkdir(name_template);
60 string GetSystemTempDir() {
63 if (!GetTempPath(
sizeof(buf), buf))
67 const char* tempdir = getenv(
"TMPDIR");
83 " command = cat $in > $out\n");
93 ASSERT_TRUE(parser.
ParseTest(input, &err)) << err;
102 const string& contents) {
103 files_[path].mtime = now_;
104 files_[path].contents = contents;
105 files_created_.insert(path);
109 FileMap::const_iterator i = files_.find(path);
110 if (i != files_.end())
111 return i->second.mtime;
116 Create(path, contents);
121 directories_made_.push_back(path);
126 files_read_.push_back(path);
127 FileMap::iterator i = files_.find(path);
128 if (i != files_.end())
129 return i->second.contents;
134 if (find(directories_made_.begin(), directories_made_.end(), path)
135 != directories_made_.end())
137 FileMap::iterator i = files_.find(path);
138 if (i != files_.end()) {
140 files_removed_.insert(path);
149 start_dir_ = GetSystemTempDir();
150 if (start_dir_.empty())
151 Fatal(
"couldn't get system temp dir");
152 if (chdir(start_dir_.c_str()) < 0)
153 Fatal(
"chdir: %s", strerror(errno));
156 char name_template[1024];
157 strcpy(name_template, name.c_str());
158 strcat(name_template,
"-XXXXXX");
159 char* tempname = mkdtemp(name_template);
161 Fatal(
"mkdtemp: %s", strerror(errno));
162 temp_dir_name_ = tempname;
165 if (chdir(temp_dir_name_.c_str()) < 0)
166 Fatal(
"chdir: %s", strerror(errno));
170 if (temp_dir_name_.empty())
174 if (chdir(start_dir_.c_str()) < 0)
175 Fatal(
"chdir: %s", strerror(errno));
178 string command =
"rmdir /s /q " + temp_dir_name_;
180 string command =
"rm -rf " + temp_dir_name_;
182 if (system(command.c_str()) < 0)
183 Fatal(
"system: %s", strerror(errno));
185 temp_dir_name_.clear();
Node * GetNode(const string &path)
Short way to get a Node by its path from state_.
Node * GetNode(StringPiece path)
Information about a node in the dependency graph: the file, whether it's dirty, mtime, etc.
virtual bool WriteFile(const string &path, const string &contents)
Create a file, with the specified name and contents Returns true on success, false on failure...
StateTestWithBuiltinRules()
void AssertParse(State *state, const char *input)
void Create(const string &path, const string &contents)
"Create" a file with contents.
void AddCatRule(State *state)
Add a "cat" rule to state.
virtual bool MakeDir(const string &path)
Create a directory, returning false on failure.
virtual TimeStamp Stat(const string &path) const
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
void Fatal(const char *msg,...)
Log a fatal message and exit.
void Cleanup()
Clean up the temporary directory.
static uint64_t HashCommand(StringPiece command)
void CreateAndEnter(const string &name)
Create a temporary directory and chdir into it.
virtual string ReadFile(const string &path, string *err)
Read a file to a string. Fill in |err| on error.
Global state (file status, loaded rules) for a single run.
virtual int RemoveFile(const string &path)
Remove the file named path.
unsigned long long uint64_t
void AssertHash(const char *expected, uint64_t actual)
bool ParseTest(const string &input, string *err)
Parse a text string of input. Used by tests.