31 virtual void SetUp() {
33 temp_dir_.CreateAndEnter(
"Ninja-DiskInterfaceTest");
36 virtual void TearDown() {
40 bool Touch(
const char* path) {
41 FILE *f = fopen(path,
"w");
44 return fclose(f) == 0;
51 TEST_F(DiskInterfaceTest, StatMissingFile) {
53 EXPECT_EQ(0, disk_.Stat(
"nosuchfile", &err));
58 EXPECT_EQ(0, disk_.Stat(
"nosuchdir/nosuchfile", &err));
64 EXPECT_EQ(0, disk_.Stat(
"notadir/nosuchfile", &err));
68 TEST_F(DiskInterfaceTest, StatBadPath) {
71 string bad_path(
"cc:\\foo");
72 EXPECT_EQ(-1, disk_.Stat(bad_path, &err));
75 string too_long_name(512,
'x');
76 EXPECT_EQ(-1, disk_.Stat(too_long_name, &err));
81 TEST_F(DiskInterfaceTest, StatExistingFile) {
88 TEST_F(DiskInterfaceTest, StatExistingDir) {
98 EXPECT_GT(disk_.Stat(
"subdir/subsubdir", &err), 1);
102 disk_.Stat(
"subdir/.", &err));
104 disk_.Stat(
"subdir/subsubdir/..", &err));
105 EXPECT_EQ(disk_.Stat(
"subdir/subsubdir", &err),
106 disk_.Stat(
"subdir/subsubdir/.", &err));
110 TEST_F(DiskInterfaceTest, StatCache) {
121 disk_.AllowStatCache(
false);
122 TimeStamp parent_stat_uncached = disk_.Stat(
"..", &err);
123 disk_.AllowStatCache(
true);
130 EXPECT_GT(disk_.Stat(
"subdir/subfile2", &err), 1);
132 EXPECT_GT(disk_.Stat(
"sUbdir\\suBFile1", &err), 1);
139 EXPECT_GT(disk_.Stat(
"subdir", &err), 1);
141 EXPECT_GT(disk_.Stat(
"subdir/subsubdir", &err), 1);
144 #ifndef _MSC_VER // TODO: Investigate why. Also see https://github.com/ninja-build/ninja/pull/1423 146 disk_.Stat(
"subdir/.", &err));
149 disk_.Stat(
"subdir/subsubdir/..", &err));
152 EXPECT_EQ(disk_.Stat(
"..", &err), parent_stat_uncached);
154 EXPECT_EQ(disk_.Stat(
"subdir/subsubdir", &err),
155 disk_.Stat(
"subdir/subsubdir/.", &err));
159 string bad_path(
"cc:\\foo");
160 EXPECT_EQ(-1, disk_.Stat(bad_path, &err));
162 EXPECT_EQ(-1, disk_.Stat(bad_path, &err));
164 EXPECT_EQ(0, disk_.Stat(
"nosuchfile", &err));
166 EXPECT_EQ(0, disk_.Stat(
"nosuchdir/nosuchfile", &err));
175 disk_.ReadFile(
"foobar", &content, &err));
180 const char* kTestFile =
"testfile";
181 FILE* f = fopen(kTestFile,
"wb");
183 const char* kTestContent =
"test content\nok";
184 fprintf(f,
"%s", kTestContent);
188 disk_.ReadFile(kTestFile, &content, &err));
193 TEST_F(DiskInterfaceTest, MakeDirs) {
194 string path =
"path/with/double//slash/";
196 FILE* f = fopen((path +
"a_file").c_str(),
"w");
200 string path2 =
"another\\with\\back\\\\slashes\\";
202 FILE* f2 = fopen((path2 +
"a_file").c_str(),
"w");
208 TEST_F(DiskInterfaceTest, RemoveFile) {
209 const char* kFileName =
"file-to-remove";
211 EXPECT_EQ(0, disk_.RemoveFile(kFileName));
212 EXPECT_EQ(1, disk_.RemoveFile(kFileName));
213 EXPECT_EQ(1, disk_.RemoveFile(
"does not exist"));
218 StatTest() : scan_(&state_, NULL, NULL, this, NULL) {}
221 virtual TimeStamp Stat(
const string& path,
string* err)
const;
222 virtual bool WriteFile(
const string& path,
const string& contents) {
226 virtual bool MakeDir(
const string& path) {
230 virtual Status
ReadFile(
const string& path,
string* contents,
string* err) {
234 virtual int RemoveFile(
const string& path) {
240 map<string, TimeStamp> mtimes_;
241 mutable vector<string> stats_;
244 TimeStamp StatTest::Stat(
const string& path,
string* err)
const {
245 stats_.push_back(path);
246 map<string, TimeStamp>::const_iterator i = mtimes_.find(path);
247 if (i == mtimes_.end())
252 TEST_F(StatTest, Simple) {
254 "build out: cat in\n"));
256 Node* out = GetNode(
"out");
261 scan_.RecomputeDirty(out, NULL);
267 TEST_F(StatTest, TwoStep) {
269 "build out: cat mid\n" 270 "build mid: cat in\n"));
272 Node* out = GetNode(
"out");
277 scan_.RecomputeDirty(out, NULL);
288 "build out: cat mid1 mid2\n" 289 "build mid1: cat in11 in12\n" 290 "build mid2: cat in21 in22\n"));
292 Node* out = GetNode(
"out");
297 scan_.RecomputeDirty(out, NULL);
304 TEST_F(StatTest, Middle) {
306 "build out: cat mid\n" 307 "build mid: cat in\n"));
313 Node* out = GetNode(
"out");
318 scan_.RecomputeDirty(out, NULL);
bool Stat(DiskInterface *disk_interface, std::string *err)
Return false on error.
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.
Implementation of DiskInterface that actually hits the disk.
int ReadFile(const string &path, string *contents, string *err)
DependencyScan manages the process of scanning the files in a graph and updating the dirty/outputs_re...
#define ASSERT_NO_FATAL_FAILURE(a)