Ninja
disk_interface_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 <assert.h>
16 #include <stdio.h>
17 #ifdef _WIN32
18 #include <io.h>
19 #include <windows.h>
20 #endif
21 
22 #include "disk_interface.h"
23 #include "graph.h"
24 #include "test.h"
25 
26 namespace {
27 
28 struct DiskInterfaceTest : public testing::Test {
29  virtual void SetUp() {
30  // These tests do real disk accesses, so create a temp dir.
31  temp_dir_.CreateAndEnter("Ninja-DiskInterfaceTest");
32  }
33 
34  virtual void TearDown() {
35  temp_dir_.Cleanup();
36  }
37 
38  bool Touch(const char* path) {
39  FILE *f = fopen(path, "w");
40  if (!f)
41  return false;
42  return fclose(f) == 0;
43  }
44 
45  ScopedTempDir temp_dir_;
46  RealDiskInterface disk_;
47 };
48 
49 TEST_F(DiskInterfaceTest, StatMissingFile) {
50  string err;
51  EXPECT_EQ(0, disk_.Stat("nosuchfile", &err));
52  EXPECT_EQ("", err);
53 
54  // On Windows, the errno for a file in a nonexistent directory
55  // is different.
56  EXPECT_EQ(0, disk_.Stat("nosuchdir/nosuchfile", &err));
57  EXPECT_EQ("", err);
58 
59  // On POSIX systems, the errno is different if a component of the
60  // path prefix is not a directory.
61  ASSERT_TRUE(Touch("notadir"));
62  EXPECT_EQ(0, disk_.Stat("notadir/nosuchfile", &err));
63  EXPECT_EQ("", err);
64 }
65 
66 TEST_F(DiskInterfaceTest, StatBadPath) {
67  string err;
68 #ifdef _WIN32
69  string bad_path("cc:\\foo");
70  EXPECT_EQ(-1, disk_.Stat(bad_path, &err));
71  EXPECT_NE("", err);
72 #else
73  string too_long_name(512, 'x');
74  EXPECT_EQ(-1, disk_.Stat(too_long_name, &err));
75  EXPECT_NE("", err);
76 #endif
77 }
78 
79 TEST_F(DiskInterfaceTest, StatExistingFile) {
80  string err;
81  ASSERT_TRUE(Touch("file"));
82  EXPECT_GT(disk_.Stat("file", &err), 1);
83  EXPECT_EQ("", err);
84 }
85 
86 TEST_F(DiskInterfaceTest, StatExistingDir) {
87  string err;
88  ASSERT_TRUE(disk_.MakeDir("subdir"));
89  ASSERT_TRUE(disk_.MakeDir("subdir/subsubdir"));
90  EXPECT_GT(disk_.Stat(".", &err), 1);
91  EXPECT_EQ("", err);
92  EXPECT_GT(disk_.Stat("subdir", &err), 1);
93  EXPECT_EQ("", err);
94  EXPECT_GT(disk_.Stat("subdir/subsubdir", &err), 1);
95  EXPECT_EQ("", err);
96 
97  EXPECT_EQ(disk_.Stat("subdir", &err),
98  disk_.Stat("subdir/.", &err));
99  EXPECT_EQ(disk_.Stat("subdir", &err),
100  disk_.Stat("subdir/subsubdir/..", &err));
101  EXPECT_EQ(disk_.Stat("subdir/subsubdir", &err),
102  disk_.Stat("subdir/subsubdir/.", &err));
103 }
104 
105 #ifdef _WIN32
106 TEST_F(DiskInterfaceTest, StatCache) {
107  string err;
108  disk_.AllowStatCache(true);
109 
110  ASSERT_TRUE(Touch("file1"));
111  ASSERT_TRUE(Touch("fiLE2"));
112  ASSERT_TRUE(disk_.MakeDir("subdir"));
113  ASSERT_TRUE(disk_.MakeDir("subdir/subsubdir"));
114  ASSERT_TRUE(Touch("subdir\\subfile1"));
115  ASSERT_TRUE(Touch("subdir\\SUBFILE2"));
116  ASSERT_TRUE(Touch("subdir\\SUBFILE3"));
117 
118  EXPECT_GT(disk_.Stat("FIle1", &err), 1);
119  EXPECT_EQ("", err);
120  EXPECT_GT(disk_.Stat("file1", &err), 1);
121  EXPECT_EQ("", err);
122 
123  EXPECT_GT(disk_.Stat("subdir/subfile2", &err), 1);
124  EXPECT_EQ("", err);
125  EXPECT_GT(disk_.Stat("sUbdir\\suBFile1", &err), 1);
126  EXPECT_EQ("", err);
127 
128  EXPECT_GT(disk_.Stat(".", &err), 1);
129  EXPECT_EQ("", err);
130  EXPECT_GT(disk_.Stat("subdir", &err), 1);
131  EXPECT_EQ("", err);
132  EXPECT_GT(disk_.Stat("subdir/subsubdir", &err), 1);
133  EXPECT_EQ("", err);
134 
135  EXPECT_EQ(disk_.Stat("subdir", &err),
136  disk_.Stat("subdir/.", &err));
137  EXPECT_EQ("", err);
138  EXPECT_EQ(disk_.Stat("subdir", &err),
139  disk_.Stat("subdir/subsubdir/..", &err));
140  EXPECT_EQ("", err);
141  EXPECT_EQ(disk_.Stat("subdir/subsubdir", &err),
142  disk_.Stat("subdir/subsubdir/.", &err));
143  EXPECT_EQ("", err);
144 
145  // Test error cases.
146  string bad_path("cc:\\foo");
147  EXPECT_EQ(-1, disk_.Stat(bad_path, &err));
148  EXPECT_NE("", err); err.clear();
149  EXPECT_EQ(-1, disk_.Stat(bad_path, &err));
150  EXPECT_NE("", err); err.clear();
151  EXPECT_EQ(0, disk_.Stat("nosuchfile", &err));
152  EXPECT_EQ("", err);
153  EXPECT_EQ(0, disk_.Stat("nosuchdir/nosuchfile", &err));
154  EXPECT_EQ("", err);
155 }
156 #endif
157 
158 TEST_F(DiskInterfaceTest, ReadFile) {
159  string err;
160  EXPECT_EQ("", disk_.ReadFile("foobar", &err));
161  EXPECT_EQ("", err);
162 
163  const char* kTestFile = "testfile";
164  FILE* f = fopen(kTestFile, "wb");
165  ASSERT_TRUE(f);
166  const char* kTestContent = "test content\nok";
167  fprintf(f, "%s", kTestContent);
168  ASSERT_EQ(0, fclose(f));
169 
170  EXPECT_EQ(kTestContent, disk_.ReadFile(kTestFile, &err));
171  EXPECT_EQ("", err);
172 }
173 
174 TEST_F(DiskInterfaceTest, MakeDirs) {
175  string path = "path/with/double//slash/";
176  EXPECT_TRUE(disk_.MakeDirs(path.c_str()));
177  FILE* f = fopen((path + "a_file").c_str(), "w");
178  EXPECT_TRUE(f);
179  EXPECT_EQ(0, fclose(f));
180 #ifdef _WIN32
181  string path2 = "another\\with\\back\\\\slashes\\";
182  EXPECT_TRUE(disk_.MakeDirs(path2.c_str()));
183  FILE* f2 = fopen((path2 + "a_file").c_str(), "w");
184  EXPECT_TRUE(f2);
185  EXPECT_EQ(0, fclose(f2));
186 #endif
187 }
188 
189 TEST_F(DiskInterfaceTest, RemoveFile) {
190  const char* kFileName = "file-to-remove";
191  ASSERT_TRUE(Touch(kFileName));
192  EXPECT_EQ(0, disk_.RemoveFile(kFileName));
193  EXPECT_EQ(1, disk_.RemoveFile(kFileName));
194  EXPECT_EQ(1, disk_.RemoveFile("does not exist"));
195 }
196 
197 struct StatTest : public StateTestWithBuiltinRules,
198  public DiskInterface {
199  StatTest() : scan_(&state_, NULL, NULL, this) {}
200 
201  // DiskInterface implementation.
202  virtual TimeStamp Stat(const string& path, string* err) const;
203  virtual bool WriteFile(const string& path, const string& contents) {
204  assert(false);
205  return true;
206  }
207  virtual bool MakeDir(const string& path) {
208  assert(false);
209  return false;
210  }
211  virtual string ReadFile(const string& path, string* err) {
212  assert(false);
213  return "";
214  }
215  virtual int RemoveFile(const string& path) {
216  assert(false);
217  return 0;
218  }
219 
220  DependencyScan scan_;
221  map<string, TimeStamp> mtimes_;
222  mutable vector<string> stats_;
223 };
224 
225 TimeStamp StatTest::Stat(const string& path, string* err) const {
226  stats_.push_back(path);
227  map<string, TimeStamp>::const_iterator i = mtimes_.find(path);
228  if (i == mtimes_.end())
229  return 0; // File not found.
230  return i->second;
231 }
232 
233 TEST_F(StatTest, Simple) {
235 "build out: cat in\n"));
236 
237  Node* out = GetNode("out");
238  string err;
239  EXPECT_TRUE(out->Stat(this, &err));
240  EXPECT_EQ("", err);
241  ASSERT_EQ(1u, stats_.size());
242  scan_.RecomputeDirty(out->in_edge(), NULL);
243  ASSERT_EQ(2u, stats_.size());
244  ASSERT_EQ("out", stats_[0]);
245  ASSERT_EQ("in", stats_[1]);
246 }
247 
248 TEST_F(StatTest, TwoStep) {
250 "build out: cat mid\n"
251 "build mid: cat in\n"));
252 
253  Node* out = GetNode("out");
254  string err;
255  EXPECT_TRUE(out->Stat(this, &err));
256  EXPECT_EQ("", err);
257  ASSERT_EQ(1u, stats_.size());
258  scan_.RecomputeDirty(out->in_edge(), NULL);
259  ASSERT_EQ(3u, stats_.size());
260  ASSERT_EQ("out", stats_[0]);
261  ASSERT_TRUE(GetNode("out")->dirty());
262  ASSERT_EQ("mid", stats_[1]);
263  ASSERT_TRUE(GetNode("mid")->dirty());
264  ASSERT_EQ("in", stats_[2]);
265 }
266 
267 TEST_F(StatTest, Tree) {
269 "build out: cat mid1 mid2\n"
270 "build mid1: cat in11 in12\n"
271 "build mid2: cat in21 in22\n"));
272 
273  Node* out = GetNode("out");
274  string err;
275  EXPECT_TRUE(out->Stat(this, &err));
276  EXPECT_EQ("", err);
277  ASSERT_EQ(1u, stats_.size());
278  scan_.RecomputeDirty(out->in_edge(), NULL);
279  ASSERT_EQ(1u + 6u, stats_.size());
280  ASSERT_EQ("mid1", stats_[1]);
281  ASSERT_TRUE(GetNode("mid1")->dirty());
282  ASSERT_EQ("in11", stats_[2]);
283 }
284 
285 TEST_F(StatTest, Middle) {
287 "build out: cat mid\n"
288 "build mid: cat in\n"));
289 
290  mtimes_["in"] = 1;
291  mtimes_["mid"] = 0; // missing
292  mtimes_["out"] = 1;
293 
294  Node* out = GetNode("out");
295  string err;
296  EXPECT_TRUE(out->Stat(this, &err));
297  EXPECT_EQ("", err);
298  ASSERT_EQ(1u, stats_.size());
299  scan_.RecomputeDirty(out->in_edge(), NULL);
300  ASSERT_FALSE(GetNode("in")->dirty());
301  ASSERT_TRUE(GetNode("mid")->dirty());
302  ASSERT_TRUE(GetNode("out")->dirty());
303 }
304 
305 } // namespace
#define TEST_F(x, y)
Definition: test.h:60
virtual string ReadFile(const string &path, string *err)=0
Read a file to a string. Fill in |err| on error.
#define EXPECT_TRUE(a)
Definition: test.h:75
virtual void SetUp()
Definition: test.h:35
Information about a node in the dependency graph: the file, whether it's dirty, mtime, etc.
Definition: graph.h:35
Interface for accessing the disk.
Edge * in_edge() const
Definition: graph.h:83
void AssertParse(State *state, const char *input)
Definition: test.cc:97
int TimeStamp
Definition: timestamp.h:22
virtual void TearDown()
Definition: test.h:36
A base test fixture that includes a State object with a builtin "cat" rule.
Definition: test.h:112
virtual bool WriteFile(const string &path, const string &contents)=0
Create a file, with the specified name and contents Returns true on success, false on failure...
Implementation of DiskInterface that actually hits the disk.
#define EXPECT_EQ(a, b)
Definition: test.h:63
#define ASSERT_FALSE(a)
Definition: test.h:94
virtual TimeStamp Stat(const string &path, string *err) const =0
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
int ReadFile(const string &path, string *contents, string *err)
Read a file to a string (in text mode: with CRLF conversion on Windows).
Definition: util.cc:343
#define ASSERT_EQ(a, b)
Definition: test.h:80
DependencyScan manages the process of scanning the files in a graph and updating the dirty/outputs_re...
Definition: graph.h:226
#define EXPECT_GT(a, b)
Definition: test.h:67
#define ASSERT_NO_FATAL_FAILURE(a)
Definition: test.h:96
bool Stat(DiskInterface *disk_interface, string *err)
Return false on error.
Definition: graph.cc:30
virtual int RemoveFile(const string &path)=0
Remove the file named path.
#define ASSERT_TRUE(a)
Definition: test.h:92
virtual bool MakeDir(const string &path)=0
Create a directory, returning false on failure.
#define EXPECT_NE(a, b)
Definition: test.h:65