29 virtual void SetUp() {
31 temp_dir_.CreateAndEnter(
"Ninja-DiskInterfaceTest");
38 bool Touch(
const char* path) {
39 FILE *f = fopen(path,
"w");
42 return fclose(f) == 0;
49 TEST_F(DiskInterfaceTest, StatMissingFile) {
51 EXPECT_EQ(0, disk_.Stat(
"nosuchfile", &err));
56 EXPECT_EQ(0, disk_.Stat(
"nosuchdir/nosuchfile", &err));
62 EXPECT_EQ(0, disk_.Stat(
"notadir/nosuchfile", &err));
66 TEST_F(DiskInterfaceTest, StatBadPath) {
69 string bad_path(
"cc:\\foo");
70 EXPECT_EQ(-1, disk_.Stat(bad_path, &err));
73 string too_long_name(512,
'x');
74 EXPECT_EQ(-1, disk_.Stat(too_long_name, &err));
79 TEST_F(DiskInterfaceTest, StatExistingFile) {
86 TEST_F(DiskInterfaceTest, StatExistingDir) {
96 EXPECT_GT(disk_.Stat(
"subdir/subsubdir", &err), 1);
100 disk_.Stat(
"subdir/.", &err));
102 disk_.Stat(
"subdir/subsubdir/..", &err));
103 EXPECT_EQ(disk_.Stat(
"subdir/subsubdir", &err),
104 disk_.Stat(
"subdir/subsubdir/.", &err));
108 TEST_F(DiskInterfaceTest, StatCache) {
119 disk_.AllowStatCache(
false);
120 TimeStamp parent_stat_uncached = disk_.Stat(
"..", &err);
121 disk_.AllowStatCache(
true);
128 EXPECT_GT(disk_.Stat(
"subdir/subfile2", &err), 1);
130 EXPECT_GT(disk_.Stat(
"sUbdir\\suBFile1", &err), 1);
137 EXPECT_GT(disk_.Stat(
"subdir", &err), 1);
139 EXPECT_GT(disk_.Stat(
"subdir/subsubdir", &err), 1);
142 #ifndef _MSC_VER // TODO: Investigate why. Also see https://github.com/ninja-build/ninja/pull/1423 144 disk_.Stat(
"subdir/.", &err));
147 disk_.Stat(
"subdir/subsubdir/..", &err));
150 EXPECT_EQ(disk_.Stat(
"..", &err), parent_stat_uncached);
152 EXPECT_EQ(disk_.Stat(
"subdir/subsubdir", &err),
153 disk_.Stat(
"subdir/subsubdir/.", &err));
157 string bad_path(
"cc:\\foo");
158 EXPECT_EQ(-1, disk_.Stat(bad_path, &err));
160 EXPECT_EQ(-1, disk_.Stat(bad_path, &err));
162 EXPECT_EQ(0, disk_.Stat(
"nosuchfile", &err));
164 EXPECT_EQ(0, disk_.Stat(
"nosuchdir/nosuchfile", &err));
173 disk_.ReadFile(
"foobar", &content, &err));
178 const char* kTestFile =
"testfile";
179 FILE* f = fopen(kTestFile,
"wb");
181 const char* kTestContent =
"test content\nok";
182 fprintf(f,
"%s", kTestContent);
186 disk_.ReadFile(kTestFile, &content, &err));
191 TEST_F(DiskInterfaceTest, MakeDirs) {
192 string path =
"path/with/double//slash/";
194 FILE* f = fopen((path +
"a_file").c_str(),
"w");
198 string path2 =
"another\\with\\back\\\\slashes\\";
200 FILE* f2 = fopen((path2 +
"a_file").c_str(),
"w");
206 TEST_F(DiskInterfaceTest, RemoveFile) {
207 const char* kFileName =
"file-to-remove";
209 EXPECT_EQ(0, disk_.RemoveFile(kFileName));
210 EXPECT_EQ(1, disk_.RemoveFile(kFileName));
211 EXPECT_EQ(1, disk_.RemoveFile(
"does not exist"));
216 StatTest() : scan_(&state_, NULL, NULL, this, NULL) {}
219 virtual TimeStamp Stat(
const string& path,
string* err)
const;
220 virtual bool WriteFile(
const string& path,
const string& contents) {
224 virtual bool MakeDir(
const string& path) {
228 virtual Status
ReadFile(
const string& path,
string* contents,
string* err) {
238 map<string, TimeStamp> mtimes_;
239 mutable vector<string> stats_;
242 TimeStamp StatTest::Stat(
const string& path,
string* err)
const {
243 stats_.push_back(path);
244 map<string, TimeStamp>::const_iterator i = mtimes_.find(path);
245 if (i == mtimes_.end())
250 TEST_F(StatTest, Simple) {
252 "build out: cat in\n"));
254 Node* out = GetNode(
"out");
259 scan_.RecomputeDirty(out, NULL);
265 TEST_F(StatTest, TwoStep) {
267 "build out: cat mid\n" 268 "build mid: cat in\n"));
270 Node* out = GetNode(
"out");
275 scan_.RecomputeDirty(out, NULL);
286 "build out: cat mid1 mid2\n" 287 "build mid1: cat in11 in12\n" 288 "build mid2: cat in21 in22\n"));
290 Node* out = GetNode(
"out");
295 scan_.RecomputeDirty(out, NULL);
302 TEST_F(StatTest, Middle) {
304 "build out: cat mid\n" 305 "build mid: cat in\n"));
311 Node* out = GetNode(
"out");
316 scan_.RecomputeDirty(out, NULL);
void AssertParse(State *state, const char *input, ManifestParserOptions opts)
Information about a node in the dependency graph: the file, whether it's dirty, mtime, etc.
Interface for accessing the disk.
A base test fixture that includes a State object with a builtin "cat" rule.
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.
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).
virtual Status ReadFile(const string &path, string *contents, string *err)=0
Read and store in given string.
DependencyScan manages the process of scanning the files in a graph and updating the dirty/outputs_re...
#define ASSERT_NO_FATAL_FAILURE(a)
bool Stat(DiskInterface *disk_interface, string *err)
Return false on error.
virtual int RemoveFile(const string &path)=0
Remove the file named path.
virtual bool MakeDir(const string &path)=0
Create a directory, returning false on failure.