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  EXPECT_EQ(0, disk_.Stat("nosuchfile"));
51 
52  // On Windows, the errno for a file in a nonexistent directory
53  // is different.
54  EXPECT_EQ(0, disk_.Stat("nosuchdir/nosuchfile"));
55 
56  // On POSIX systems, the errno is different if a component of the
57  // path prefix is not a directory.
58  ASSERT_TRUE(Touch("notadir"));
59  EXPECT_EQ(0, disk_.Stat("notadir/nosuchfile"));
60 }
61 
62 TEST_F(DiskInterfaceTest, StatBadPath) {
63  disk_.quiet_ = true;
64 #ifdef _WIN32
65  string bad_path("cc:\\foo");
66  EXPECT_EQ(-1, disk_.Stat(bad_path));
67 #else
68  string too_long_name(512, 'x');
69  EXPECT_EQ(-1, disk_.Stat(too_long_name));
70 #endif
71  disk_.quiet_ = false;
72 }
73 
74 TEST_F(DiskInterfaceTest, StatExistingFile) {
75  ASSERT_TRUE(Touch("file"));
76  EXPECT_GT(disk_.Stat("file"), 1);
77 }
78 
79 TEST_F(DiskInterfaceTest, StatExistingDir) {
80  ASSERT_TRUE(disk_.MakeDir("subdir"));
81  ASSERT_TRUE(disk_.MakeDir("subdir/subsubdir"));
82  EXPECT_GT(disk_.Stat("."), 1);
83  EXPECT_GT(disk_.Stat("subdir"), 1);
84  EXPECT_GT(disk_.Stat("subdir/subsubdir"), 1);
85 
86  EXPECT_EQ(disk_.Stat("subdir"), disk_.Stat("subdir/."));
87  EXPECT_EQ(disk_.Stat("subdir"), disk_.Stat("subdir/subsubdir/.."));
88  EXPECT_EQ(disk_.Stat("subdir/subsubdir"), disk_.Stat("subdir/subsubdir/."));
89 }
90 
91 #ifdef _WIN32
92 TEST_F(DiskInterfaceTest, StatCache) {
93  disk_.AllowStatCache(true);
94 
95  ASSERT_TRUE(Touch("file1"));
96  ASSERT_TRUE(Touch("fiLE2"));
97  ASSERT_TRUE(disk_.MakeDir("subdir"));
98  ASSERT_TRUE(disk_.MakeDir("subdir/subsubdir"));
99  ASSERT_TRUE(Touch("subdir\\subfile1"));
100  ASSERT_TRUE(Touch("subdir\\SUBFILE2"));
101  ASSERT_TRUE(Touch("subdir\\SUBFILE3"));
102 
103  EXPECT_GT(disk_.Stat("FIle1"), 1);
104  EXPECT_GT(disk_.Stat("file1"), 1);
105 
106  EXPECT_GT(disk_.Stat("subdir/subfile2"), 1);
107  EXPECT_GT(disk_.Stat("sUbdir\\suBFile1"), 1);
108 
109  EXPECT_GT(disk_.Stat("."), 1);
110  EXPECT_GT(disk_.Stat("subdir"), 1);
111  EXPECT_GT(disk_.Stat("subdir/subsubdir"), 1);
112 
113  EXPECT_EQ(disk_.Stat("subdir"), disk_.Stat("subdir/."));
114  EXPECT_EQ(disk_.Stat("subdir"), disk_.Stat("subdir/subsubdir/.."));
115  EXPECT_EQ(disk_.Stat("subdir/subsubdir"), disk_.Stat("subdir/subsubdir/."));
116 
117  // Test error cases.
118  disk_.quiet_ = true;
119  string bad_path("cc:\\foo");
120  EXPECT_EQ(-1, disk_.Stat(bad_path));
121  EXPECT_EQ(-1, disk_.Stat(bad_path));
122  EXPECT_EQ(0, disk_.Stat("nosuchfile"));
123  EXPECT_EQ(0, disk_.Stat("nosuchdir/nosuchfile"));
124 }
125 #endif
126 
127 TEST_F(DiskInterfaceTest, ReadFile) {
128  string err;
129  EXPECT_EQ("", disk_.ReadFile("foobar", &err));
130  EXPECT_EQ("", err);
131 
132  const char* kTestFile = "testfile";
133  FILE* f = fopen(kTestFile, "wb");
134  ASSERT_TRUE(f);
135  const char* kTestContent = "test content\nok";
136  fprintf(f, "%s", kTestContent);
137  ASSERT_EQ(0, fclose(f));
138 
139  EXPECT_EQ(kTestContent, disk_.ReadFile(kTestFile, &err));
140  EXPECT_EQ("", err);
141 }
142 
143 TEST_F(DiskInterfaceTest, MakeDirs) {
144  string path = "path/with/double//slash/";
145  EXPECT_TRUE(disk_.MakeDirs(path.c_str()));
146  FILE* f = fopen((path + "a_file").c_str(), "w");
147  EXPECT_TRUE(f);
148  EXPECT_EQ(0, fclose(f));
149 #ifdef _WIN32
150  string path2 = "another\\with\\back\\\\slashes\\";
151  EXPECT_TRUE(disk_.MakeDirs(path2.c_str()));
152  FILE* f2 = fopen((path2 + "a_file").c_str(), "w");
153  EXPECT_TRUE(f2);
154  EXPECT_EQ(0, fclose(f2));
155 #endif
156 }
157 
158 TEST_F(DiskInterfaceTest, RemoveFile) {
159  const char* kFileName = "file-to-remove";
160  ASSERT_TRUE(Touch(kFileName));
161  EXPECT_EQ(0, disk_.RemoveFile(kFileName));
162  EXPECT_EQ(1, disk_.RemoveFile(kFileName));
163  EXPECT_EQ(1, disk_.RemoveFile("does not exist"));
164 }
165 
166 struct StatTest : public StateTestWithBuiltinRules,
167  public DiskInterface {
168  StatTest() : scan_(&state_, NULL, NULL, this) {}
169 
170  // DiskInterface implementation.
171  virtual TimeStamp Stat(const string& path) const;
172  virtual bool WriteFile(const string& path, const string& contents) {
173  assert(false);
174  return true;
175  }
176  virtual bool MakeDir(const string& path) {
177  assert(false);
178  return false;
179  }
180  virtual string ReadFile(const string& path, string* err) {
181  assert(false);
182  return "";
183  }
184  virtual int RemoveFile(const string& path) {
185  assert(false);
186  return 0;
187  }
188 
189  DependencyScan scan_;
190  map<string, TimeStamp> mtimes_;
191  mutable vector<string> stats_;
192 };
193 
194 TimeStamp StatTest::Stat(const string& path) const {
195  stats_.push_back(path);
196  map<string, TimeStamp>::const_iterator i = mtimes_.find(path);
197  if (i == mtimes_.end())
198  return 0; // File not found.
199  return i->second;
200 }
201 
202 TEST_F(StatTest, Simple) {
204 "build out: cat in\n"));
205 
206  Node* out = GetNode("out");
207  out->Stat(this);
208  ASSERT_EQ(1u, stats_.size());
209  scan_.RecomputeDirty(out->in_edge(), NULL);
210  ASSERT_EQ(2u, stats_.size());
211  ASSERT_EQ("out", stats_[0]);
212  ASSERT_EQ("in", stats_[1]);
213 }
214 
215 TEST_F(StatTest, TwoStep) {
217 "build out: cat mid\n"
218 "build mid: cat in\n"));
219 
220  Node* out = GetNode("out");
221  out->Stat(this);
222  ASSERT_EQ(1u, stats_.size());
223  scan_.RecomputeDirty(out->in_edge(), NULL);
224  ASSERT_EQ(3u, stats_.size());
225  ASSERT_EQ("out", stats_[0]);
226  ASSERT_TRUE(GetNode("out")->dirty());
227  ASSERT_EQ("mid", stats_[1]);
228  ASSERT_TRUE(GetNode("mid")->dirty());
229  ASSERT_EQ("in", stats_[2]);
230 }
231 
232 TEST_F(StatTest, Tree) {
234 "build out: cat mid1 mid2\n"
235 "build mid1: cat in11 in12\n"
236 "build mid2: cat in21 in22\n"));
237 
238  Node* out = GetNode("out");
239  out->Stat(this);
240  ASSERT_EQ(1u, stats_.size());
241  scan_.RecomputeDirty(out->in_edge(), NULL);
242  ASSERT_EQ(1u + 6u, stats_.size());
243  ASSERT_EQ("mid1", stats_[1]);
244  ASSERT_TRUE(GetNode("mid1")->dirty());
245  ASSERT_EQ("in11", stats_[2]);
246 }
247 
248 TEST_F(StatTest, Middle) {
250 "build out: cat mid\n"
251 "build mid: cat in\n"));
252 
253  mtimes_["in"] = 1;
254  mtimes_["mid"] = 0; // missing
255  mtimes_["out"] = 1;
256 
257  Node* out = GetNode("out");
258  out->Stat(this);
259  ASSERT_EQ(1u, stats_.size());
260  scan_.RecomputeDirty(out->in_edge(), NULL);
261  ASSERT_FALSE(GetNode("in")->dirty());
262  ASSERT_TRUE(GetNode("mid")->dirty());
263  ASSERT_TRUE(GetNode("out")->dirty());
264 }
265 
266 } // 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:84
void AssertParse(State *state, const char *input)
Definition: test.cc:96
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
bool Stat(DiskInterface *disk_interface)
Return true if the file exists (mtime_ got a value).
Definition: graph.cc:30
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
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:340
#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:247
#define EXPECT_GT(a, b)
Definition: test.h:67
#define ASSERT_NO_FATAL_FAILURE(a)
Definition: test.h:96
virtual int RemoveFile(const string &path)=0
Remove the file named path.
#define ASSERT_TRUE(a)
Definition: test.h:92
virtual TimeStamp Stat(const string &path) const =0
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
virtual bool MakeDir(const string &path)=0
Create a directory, returning false on failure.