move test virtual time "now_" into VirtualFileSystem
[platform/upstream/ninja.git] / src / test.cc
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
76 StateTestWithBuiltinRules::StateTestWithBuiltinRules() {
77   AssertParse(&state_,
78 "rule cat\n"
79 "  command = cat $in > $out\n");
80 }
81
82 Node* StateTestWithBuiltinRules::GetNode(const string& path) {
83   return state_.GetNode(path);
84 }
85
86 void AssertParse(State* state, const char* input) {
87   ManifestParser parser(state, NULL);
88   string err;
89   ASSERT_TRUE(parser.ParseTest(input, &err)) << err;
90   ASSERT_EQ("", err);
91 }
92
93 void AssertHash(const char* expected, uint64_t actual) {
94   ASSERT_EQ(BuildLog::LogEntry::HashCommand(expected), actual);
95 }
96
97 void VirtualFileSystem::Create(const string& path,
98                                const string& contents) {
99   files_[path].mtime = now_;
100   files_[path].contents = contents;
101   files_created_.insert(path);
102 }
103
104 TimeStamp VirtualFileSystem::Stat(const string& path) {
105   FileMap::iterator i = files_.find(path);
106   if (i != files_.end())
107     return i->second.mtime;
108   return 0;
109 }
110
111 bool VirtualFileSystem::WriteFile(const string& path, const string& contents) {
112   Create(path, contents);
113   return true;
114 }
115
116 bool VirtualFileSystem::MakeDir(const string& path) {
117   directories_made_.push_back(path);
118   return true;  // success
119 }
120
121 string VirtualFileSystem::ReadFile(const string& path, string* err) {
122   files_read_.push_back(path);
123   FileMap::iterator i = files_.find(path);
124   if (i != files_.end())
125     return i->second.contents;
126   return "";
127 }
128
129 int VirtualFileSystem::RemoveFile(const string& path) {
130   if (find(directories_made_.begin(), directories_made_.end(), path)
131       != directories_made_.end())
132     return -1;
133   FileMap::iterator i = files_.find(path);
134   if (i != files_.end()) {
135     files_.erase(i);
136     files_removed_.insert(path);
137     return 0;
138   } else {
139     return 1;
140   }
141 }
142
143 void ScopedTempDir::CreateAndEnter(const string& name) {
144   // First change into the system temp dir and save it for cleanup.
145   start_dir_ = GetSystemTempDir();
146   if (start_dir_.empty())
147     Fatal("couldn't get system temp dir");
148   if (chdir(start_dir_.c_str()) < 0)
149     Fatal("chdir: %s", strerror(errno));
150
151   // Create a temporary subdirectory of that.
152   char name_template[1024];
153   strcpy(name_template, name.c_str());
154   strcat(name_template, "-XXXXXX");
155   char* tempname = mkdtemp(name_template);
156   if (!tempname)
157     Fatal("mkdtemp: %s", strerror(errno));
158   temp_dir_name_ = tempname;
159
160   // chdir into the new temporary directory.
161   if (chdir(temp_dir_name_.c_str()) < 0)
162     Fatal("chdir: %s", strerror(errno));
163 }
164
165 void ScopedTempDir::Cleanup() {
166   if (temp_dir_name_.empty())
167     return;  // Something went wrong earlier.
168
169   // Move out of the directory we're about to clobber.
170   if (chdir(start_dir_.c_str()) < 0)
171     Fatal("chdir: %s", strerror(errno));
172
173 #ifdef _WIN32
174   string command = "rmdir /s /q " + temp_dir_name_;
175 #else
176   string command = "rm -rf " + temp_dir_name_;
177 #endif
178   if (system(command.c_str()) < 0)
179     Fatal("system: %s", strerror(errno));
180
181   temp_dir_name_.clear();
182 }