Ninja
test.cc
Go to the documentation of this file.
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "test.h"
16 
17 #include <algorithm>
18 
19 #include <errno.h>
20 
21 #include "build_log.h"
22 #include "manifest_parser.h"
23 #include "util.h"
24 
25 #ifdef _WIN32
26 #include <windows.h>
27 #endif
28 
29 namespace {
30 
31 #ifdef _WIN32
32 #ifndef _mktemp_s
33 /// mingw has no mktemp. Implement one with the same type as the one
34 /// found in the Windows API.
35 int _mktemp_s(char* templ) {
36  char* ofs = strchr(templ, 'X');
37  sprintf(ofs, "%d", rand() % 1000000);
38  return 0;
39 }
40 #endif
41 
42 /// Windows has no mkdtemp. Implement it in terms of _mktemp_s.
43 char* mkdtemp(char* name_template) {
44  int err = _mktemp_s(name_template);
45  if (err < 0) {
46  perror("_mktemp_s");
47  return NULL;
48  }
49 
50  err = _mkdir(name_template);
51  if (err < 0) {
52  perror("mkdir");
53  return NULL;
54  }
55 
56  return name_template;
57 }
58 #endif // _WIN32
59 
60 string GetSystemTempDir() {
61 #ifdef _WIN32
62  char buf[1024];
63  if (!GetTempPath(sizeof(buf), buf))
64  return "";
65  return buf;
66 #else
67  const char* tempdir = getenv("TMPDIR");
68  if (tempdir)
69  return tempdir;
70  return "/tmp";
71 #endif
72 }
73 
74 } // anonymous namespace
75 
78 }
79 
81  AssertParse(state,
82 "rule cat\n"
83 " command = cat $in > $out\n");
84 }
85 
87  return state_.GetNode(path);
88 }
89 
90 void AssertParse(State* state, const char* input) {
91  ManifestParser parser(state, NULL);
92  string err;
93  ASSERT_TRUE(parser.ParseTest(input, &err)) << err;
94  ASSERT_EQ("", err);
95 }
96 
97 void AssertHash(const char* expected, uint64_t actual) {
98  ASSERT_EQ(BuildLog::LogEntry::HashCommand(expected), actual);
99 }
100 
101 void VirtualFileSystem::Create(const string& path,
102  const string& contents) {
103  files_[path].mtime = now_;
104  files_[path].contents = contents;
105  files_created_.insert(path);
106 }
107 
108 TimeStamp VirtualFileSystem::Stat(const string& path) {
109  FileMap::iterator i = files_.find(path);
110  if (i != files_.end())
111  return i->second.mtime;
112  return 0;
113 }
114 
115 bool VirtualFileSystem::WriteFile(const string& path, const string& contents) {
116  Create(path, contents);
117  return true;
118 }
119 
120 bool VirtualFileSystem::MakeDir(const string& path) {
121  directories_made_.push_back(path);
122  return true; // success
123 }
124 
125 string VirtualFileSystem::ReadFile(const string& path, string* err) {
126  files_read_.push_back(path);
127  FileMap::iterator i = files_.find(path);
128  if (i != files_.end())
129  return i->second.contents;
130  return "";
131 }
132 
133 int VirtualFileSystem::RemoveFile(const string& path) {
134  if (find(directories_made_.begin(), directories_made_.end(), path)
135  != directories_made_.end())
136  return -1;
137  FileMap::iterator i = files_.find(path);
138  if (i != files_.end()) {
139  files_.erase(i);
140  files_removed_.insert(path);
141  return 0;
142  } else {
143  return 1;
144  }
145 }
146 
147 void ScopedTempDir::CreateAndEnter(const string& name) {
148  // First change into the system temp dir and save it for cleanup.
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));
154 
155  // Create a temporary subdirectory of that.
156  char name_template[1024];
157  strcpy(name_template, name.c_str());
158  strcat(name_template, "-XXXXXX");
159  char* tempname = mkdtemp(name_template);
160  if (!tempname)
161  Fatal("mkdtemp: %s", strerror(errno));
162  temp_dir_name_ = tempname;
163 
164  // chdir into the new temporary directory.
165  if (chdir(temp_dir_name_.c_str()) < 0)
166  Fatal("chdir: %s", strerror(errno));
167 }
168 
170  if (temp_dir_name_.empty())
171  return; // Something went wrong earlier.
172 
173  // Move out of the directory we're about to clobber.
174  if (chdir(start_dir_.c_str()) < 0)
175  Fatal("chdir: %s", strerror(errno));
176 
177 #ifdef _WIN32
178  string command = "rmdir /s /q " + temp_dir_name_;
179 #else
180  string command = "rm -rf " + temp_dir_name_;
181 #endif
182  if (system(command.c_str()) < 0)
183  Fatal("system: %s", strerror(errno));
184 
185  temp_dir_name_.clear();
186 }
FileMap files_
Definition: test.h:77
virtual TimeStamp Stat(const string &path)
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
Definition: test.cc:108
Node * GetNode(const string &path)
Short way to get a Node by its path from state_.
Definition: test.cc:86
Node * GetNode(StringPiece path)
Definition: state.cc:112
Information about a node in the dependency graph: the file, whether it's dirty, mtime, etc.
Definition: graph.h:35
set< string > files_removed_
Definition: test.h:78
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...
Definition: test.cc:115
int now_
A simple fake timestamp for file operations.
Definition: test.h:82
int TimeStamp
Definition: timestamp.h:22
void AssertParse(State *state, const char *input)
Definition: test.cc:90
void Create(const string &path, const string &contents)
"Create" a file with contents.
Definition: test.cc:101
set< string > files_created_
Definition: test.h:79
vector< string > directories_made_
Definition: test.h:74
void AddCatRule(State *state)
Add a "cat" rule to state.
Definition: test.cc:80
Parses .ninja files.
string start_dir_
The temp directory containing our dir.
Definition: test.h:93
virtual bool MakeDir(const string &path)
Create a directory, returning false on failure.
Definition: test.cc:120
void Fatal(const char *msg,...)
Log a fatal message and exit.
Definition: util.cc:51
void Cleanup()
Clean up the temporary directory.
Definition: test.cc:169
static uint64_t HashCommand(StringPiece command)
Definition: build_log.cc:90
void CreateAndEnter(const string &name)
Create a temporary directory and chdir into it.
Definition: test.cc:147
virtual string ReadFile(const string &path, string *err)
Read a file to a string. Fill in |err| on error.
Definition: test.cc:125
Global state (file status, loaded rules) for a single run.
Definition: state.h:83
vector< string > files_read_
Definition: test.h:75
virtual int RemoveFile(const string &path)
Remove the file named path.
Definition: test.cc:133
unsigned long long uint64_t
Definition: win32port.h:22
void AssertHash(const char *expected, uint64_t actual)
Definition: test.cc:97
string temp_dir_name_
The subdirectory name for our dir, or empty if it hasn't been set up.
Definition: test.h:95
bool ParseTest(const string &input, string *err)
Parse a text string of input. Used by tests.