add a straightforward deps log test, fix the other one
[platform/upstream/ninja.git] / src / build_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 "build.h"
16
17 #include "build_log.h"
18 #include "deps_log.h"
19 #include "graph.h"
20 #include "test.h"
21
22 /// Fixture for tests involving Plan.
23 // Though Plan doesn't use State, it's useful to have one around
24 // to create Nodes and Edges.
25 struct PlanTest : public StateTestWithBuiltinRules {
26   Plan plan_;
27 };
28
29 TEST_F(PlanTest, Basic) {
30   AssertParse(&state_,
31 "build out: cat mid\n"
32 "build mid: cat in\n");
33   GetNode("mid")->MarkDirty();
34   GetNode("out")->MarkDirty();
35   string err;
36   EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
37   ASSERT_EQ("", err);
38   ASSERT_TRUE(plan_.more_to_do());
39
40   Edge* edge = plan_.FindWork();
41   ASSERT_TRUE(edge);
42   ASSERT_EQ("in",  edge->inputs_[0]->path());
43   ASSERT_EQ("mid", edge->outputs_[0]->path());
44
45   ASSERT_FALSE(plan_.FindWork());
46
47   plan_.EdgeFinished(edge);
48
49   edge = plan_.FindWork();
50   ASSERT_TRUE(edge);
51   ASSERT_EQ("mid", edge->inputs_[0]->path());
52   ASSERT_EQ("out", edge->outputs_[0]->path());
53
54   plan_.EdgeFinished(edge);
55
56   ASSERT_FALSE(plan_.more_to_do());
57   edge = plan_.FindWork();
58   ASSERT_EQ(0, edge);
59 }
60
61 // Test that two outputs from one rule can be handled as inputs to the next.
62 TEST_F(PlanTest, DoubleOutputDirect) {
63   AssertParse(&state_,
64 "build out: cat mid1 mid2\n"
65 "build mid1 mid2: cat in\n");
66   GetNode("mid1")->MarkDirty();
67   GetNode("mid2")->MarkDirty();
68   GetNode("out")->MarkDirty();
69
70   string err;
71   EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
72   ASSERT_EQ("", err);
73   ASSERT_TRUE(plan_.more_to_do());
74
75   Edge* edge;
76   edge = plan_.FindWork();
77   ASSERT_TRUE(edge);  // cat in
78   plan_.EdgeFinished(edge);
79
80   edge = plan_.FindWork();
81   ASSERT_TRUE(edge);  // cat mid1 mid2
82   plan_.EdgeFinished(edge);
83
84   edge = plan_.FindWork();
85   ASSERT_FALSE(edge);  // done
86 }
87
88 // Test that two outputs from one rule can eventually be routed to another.
89 TEST_F(PlanTest, DoubleOutputIndirect) {
90   AssertParse(&state_,
91 "build out: cat b1 b2\n"
92 "build b1: cat a1\n"
93 "build b2: cat a2\n"
94 "build a1 a2: cat in\n");
95   GetNode("a1")->MarkDirty();
96   GetNode("a2")->MarkDirty();
97   GetNode("b1")->MarkDirty();
98   GetNode("b2")->MarkDirty();
99   GetNode("out")->MarkDirty();
100   string err;
101   EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
102   ASSERT_EQ("", err);
103   ASSERT_TRUE(plan_.more_to_do());
104
105   Edge* edge;
106   edge = plan_.FindWork();
107   ASSERT_TRUE(edge);  // cat in
108   plan_.EdgeFinished(edge);
109
110   edge = plan_.FindWork();
111   ASSERT_TRUE(edge);  // cat a1
112   plan_.EdgeFinished(edge);
113
114   edge = plan_.FindWork();
115   ASSERT_TRUE(edge);  // cat a2
116   plan_.EdgeFinished(edge);
117
118   edge = plan_.FindWork();
119   ASSERT_TRUE(edge);  // cat b1 b2
120   plan_.EdgeFinished(edge);
121
122   edge = plan_.FindWork();
123   ASSERT_FALSE(edge);  // done
124 }
125
126 // Test that two edges from one output can both execute.
127 TEST_F(PlanTest, DoubleDependent) {
128   AssertParse(&state_,
129 "build out: cat a1 a2\n"
130 "build a1: cat mid\n"
131 "build a2: cat mid\n"
132 "build mid: cat in\n");
133   GetNode("mid")->MarkDirty();
134   GetNode("a1")->MarkDirty();
135   GetNode("a2")->MarkDirty();
136   GetNode("out")->MarkDirty();
137
138   string err;
139   EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
140   ASSERT_EQ("", err);
141   ASSERT_TRUE(plan_.more_to_do());
142
143   Edge* edge;
144   edge = plan_.FindWork();
145   ASSERT_TRUE(edge);  // cat in
146   plan_.EdgeFinished(edge);
147
148   edge = plan_.FindWork();
149   ASSERT_TRUE(edge);  // cat mid
150   plan_.EdgeFinished(edge);
151
152   edge = plan_.FindWork();
153   ASSERT_TRUE(edge);  // cat mid
154   plan_.EdgeFinished(edge);
155
156   edge = plan_.FindWork();
157   ASSERT_TRUE(edge);  // cat a1 a2
158   plan_.EdgeFinished(edge);
159
160   edge = plan_.FindWork();
161   ASSERT_FALSE(edge);  // done
162 }
163
164 TEST_F(PlanTest, DependencyCycle) {
165   AssertParse(&state_,
166 "build out: cat mid\n"
167 "build mid: cat in\n"
168 "build in: cat pre\n"
169 "build pre: cat out\n");
170   GetNode("out")->MarkDirty();
171   GetNode("mid")->MarkDirty();
172   GetNode("in")->MarkDirty();
173   GetNode("pre")->MarkDirty();
174
175   string err;
176   EXPECT_FALSE(plan_.AddTarget(GetNode("out"), &err));
177   ASSERT_EQ("dependency cycle: out -> mid -> in -> pre -> out", err);
178 }
179
180 TEST_F(PlanTest, PoolWithDepthOne) {
181   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
182 "pool foobar\n"
183 "  depth = 1\n"
184 "rule poolcat\n"
185 "  command = cat $in > $out\n"
186 "  pool = foobar\n"
187 "build out1: poolcat in\n"
188 "build out2: poolcat in\n"));
189   GetNode("out1")->MarkDirty();
190   GetNode("out2")->MarkDirty();
191   string err;
192   EXPECT_TRUE(plan_.AddTarget(GetNode("out1"), &err));
193   ASSERT_EQ("", err);
194   EXPECT_TRUE(plan_.AddTarget(GetNode("out2"), &err));
195   ASSERT_EQ("", err);
196   ASSERT_TRUE(plan_.more_to_do());
197
198   Edge* edge = plan_.FindWork();
199   ASSERT_TRUE(edge);
200   ASSERT_EQ("in",  edge->inputs_[0]->path());
201   ASSERT_EQ("out1", edge->outputs_[0]->path());
202
203   // This will be false since poolcat is serialized
204   ASSERT_FALSE(plan_.FindWork());
205
206   plan_.EdgeFinished(edge);
207
208   edge = plan_.FindWork();
209   ASSERT_TRUE(edge);
210   ASSERT_EQ("in", edge->inputs_[0]->path());
211   ASSERT_EQ("out2", edge->outputs_[0]->path());
212
213   ASSERT_FALSE(plan_.FindWork());
214
215   plan_.EdgeFinished(edge);
216
217   ASSERT_FALSE(plan_.more_to_do());
218   edge = plan_.FindWork();
219   ASSERT_EQ(0, edge);
220 }
221
222 TEST_F(PlanTest, PoolsWithDepthTwo) {
223   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
224 "pool foobar\n"
225 "  depth = 2\n"
226 "pool bazbin\n"
227 "  depth = 2\n"
228 "rule foocat\n"
229 "  command = cat $in > $out\n"
230 "  pool = foobar\n"
231 "rule bazcat\n"
232 "  command = cat $in > $out\n"
233 "  pool = bazbin\n"
234 "build out1: foocat in\n"
235 "build out2: foocat in\n"
236 "build out3: foocat in\n"
237 "build outb1: bazcat in\n"
238 "build outb2: bazcat in\n"
239 "build outb3: bazcat in\n"
240 "  pool =\n"
241 "build allTheThings: cat out1 out2 out3 outb1 outb2 outb3\n"
242 ));
243   // Mark all the out* nodes dirty
244   for (int i = 0; i < 3; ++i) {
245     GetNode("out" + string(1, '1' + i))->MarkDirty();
246     GetNode("outb" + string(1, '1' + i))->MarkDirty();
247   }
248   GetNode("allTheThings")->MarkDirty();
249
250   string err;
251   EXPECT_TRUE(plan_.AddTarget(GetNode("allTheThings"), &err));
252   ASSERT_EQ("", err);
253
254   // Grab the first 4 edges, out1 out2 outb1 outb2
255   deque<Edge*> edges;
256   for (int i = 0; i < 4; ++i) {
257     ASSERT_TRUE(plan_.more_to_do());
258     Edge* edge = plan_.FindWork();
259     ASSERT_TRUE(edge);
260     ASSERT_EQ("in",  edge->inputs_[0]->path());
261     string base_name(i < 2 ? "out" : "outb");
262     ASSERT_EQ(base_name + string(1, '1' + (i % 2)), edge->outputs_[0]->path());
263     edges.push_back(edge);
264   }
265
266   // outb3 is exempt because it has an empty pool
267   ASSERT_TRUE(plan_.more_to_do());
268   Edge* edge = plan_.FindWork();
269   ASSERT_TRUE(edge);
270   ASSERT_EQ("in",  edge->inputs_[0]->path());
271   ASSERT_EQ("outb3", edge->outputs_[0]->path());
272   edges.push_back(edge);
273
274   ASSERT_FALSE(plan_.FindWork());
275
276   // finish out1
277   plan_.EdgeFinished(edges.front());
278   edges.pop_front();
279
280   // out3 should be available
281   Edge* out3 = plan_.FindWork();
282   ASSERT_TRUE(out3);
283   ASSERT_EQ("in",  out3->inputs_[0]->path());
284   ASSERT_EQ("out3", out3->outputs_[0]->path());
285
286   ASSERT_FALSE(plan_.FindWork());
287
288   plan_.EdgeFinished(out3);
289
290   ASSERT_FALSE(plan_.FindWork());
291
292   for (deque<Edge*>::iterator it = edges.begin(); it != edges.end(); ++it) {
293     plan_.EdgeFinished(*it);
294   }
295
296   Edge* final = plan_.FindWork();
297   ASSERT_TRUE(final);
298   ASSERT_EQ("allTheThings", final->outputs_[0]->path());
299
300   plan_.EdgeFinished(final);
301
302   ASSERT_FALSE(plan_.more_to_do());
303   ASSERT_FALSE(plan_.FindWork());
304 }
305
306 TEST_F(PlanTest, PoolWithRedundantEdges) {
307   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
308     "pool compile\n"
309     "  depth = 1\n"
310     "rule gen_foo\n"
311     "  command = touch foo.cpp\n"
312     "rule gen_bar\n"
313     "  command = touch bar.cpp\n"
314     "rule echo\n"
315     "  command = echo $out > $out\n"
316     "build foo.cpp.obj: echo foo.cpp || foo.cpp\n"
317     "  pool = compile\n"
318     "build bar.cpp.obj: echo bar.cpp || bar.cpp\n"
319     "  pool = compile\n"
320     "build libfoo.a: echo foo.cpp.obj bar.cpp.obj\n"
321     "build foo.cpp: gen_foo\n"
322     "build bar.cpp: gen_bar\n"
323     "build all: phony libfoo.a\n"));
324   GetNode("foo.cpp")->MarkDirty();
325   GetNode("foo.cpp.obj")->MarkDirty();
326   GetNode("bar.cpp")->MarkDirty();
327   GetNode("bar.cpp.obj")->MarkDirty();
328   GetNode("libfoo.a")->MarkDirty();
329   GetNode("all")->MarkDirty();
330   string err;
331   EXPECT_TRUE(plan_.AddTarget(GetNode("all"), &err));
332   ASSERT_EQ("", err);
333   ASSERT_TRUE(plan_.more_to_do());
334
335   Edge* edge = NULL;
336
337   edge = plan_.FindWork();
338   ASSERT_TRUE(edge);
339   ASSERT_EQ("foo.cpp", edge->outputs_[0]->path());
340   plan_.EdgeFinished(edge);
341
342   edge = plan_.FindWork();
343   ASSERT_TRUE(edge);
344   ASSERT_EQ("foo.cpp", edge->inputs_[0]->path());
345   ASSERT_EQ("foo.cpp", edge->inputs_[1]->path());
346   ASSERT_EQ("foo.cpp.obj", edge->outputs_[0]->path());
347   plan_.EdgeFinished(edge);
348
349   edge = plan_.FindWork();
350   ASSERT_TRUE(edge);
351   ASSERT_EQ("bar.cpp", edge->outputs_[0]->path());
352   plan_.EdgeFinished(edge);
353
354   edge = plan_.FindWork();
355   ASSERT_TRUE(edge);
356   ASSERT_EQ("bar.cpp", edge->inputs_[0]->path());
357   ASSERT_EQ("bar.cpp", edge->inputs_[1]->path());
358   ASSERT_EQ("bar.cpp.obj", edge->outputs_[0]->path());
359   plan_.EdgeFinished(edge);
360
361   edge = plan_.FindWork();
362   ASSERT_TRUE(edge);
363   ASSERT_EQ("foo.cpp.obj", edge->inputs_[0]->path());
364   ASSERT_EQ("bar.cpp.obj", edge->inputs_[1]->path());
365   ASSERT_EQ("libfoo.a", edge->outputs_[0]->path());
366   plan_.EdgeFinished(edge);
367
368   edge = plan_.FindWork();
369   ASSERT_TRUE(edge);
370   ASSERT_EQ("libfoo.a", edge->inputs_[0]->path());
371   ASSERT_EQ("all", edge->outputs_[0]->path());
372   plan_.EdgeFinished(edge);
373
374   edge = plan_.FindWork();
375   ASSERT_FALSE(edge);
376   ASSERT_FALSE(plan_.more_to_do());
377 }
378
379 /// Fake implementation of CommandRunner, useful for tests.
380 struct FakeCommandRunner : public CommandRunner {
381   explicit FakeCommandRunner(VirtualFileSystem* fs) :
382       last_command_(NULL), fs_(fs) {}
383
384   // CommandRunner impl
385   virtual bool CanRunMore();
386   virtual bool StartCommand(Edge* edge);
387   virtual bool WaitForCommand(Result* result);
388   virtual vector<Edge*> GetActiveEdges();
389   virtual void Abort();
390
391   vector<string> commands_ran_;
392   Edge* last_command_;
393   VirtualFileSystem* fs_;
394 };
395
396 struct BuildTest : public StateTestWithBuiltinRules {
397   BuildTest() : config_(MakeConfig()), command_runner_(&fs_),
398                 builder_(&state_, config_, NULL, NULL, &fs_),
399                 status_(config_) {
400   }
401
402   virtual void SetUp() {
403     StateTestWithBuiltinRules::SetUp();
404
405     builder_.command_runner_.reset(&command_runner_);
406     AssertParse(&state_,
407 "build cat1: cat in1\n"
408 "build cat2: cat in1 in2\n"
409 "build cat12: cat cat1 cat2\n");
410
411     fs_.Create("in1", "");
412     fs_.Create("in2", "");
413   }
414
415   ~BuildTest() {
416     builder_.command_runner_.release();
417   }
418
419   // Mark a path dirty.
420   void Dirty(const string& path);
421
422   BuildConfig MakeConfig() {
423     BuildConfig config;
424     config.verbosity = BuildConfig::QUIET;
425     return config;
426   }
427
428   BuildConfig config_;
429   FakeCommandRunner command_runner_;
430   VirtualFileSystem fs_;
431   Builder builder_;
432
433   BuildStatus status_;
434 };
435
436 bool FakeCommandRunner::CanRunMore() {
437   // Only run one at a time.
438   return last_command_ == NULL;
439 }
440
441 bool FakeCommandRunner::StartCommand(Edge* edge) {
442   assert(!last_command_);
443   commands_ran_.push_back(edge->EvaluateCommand());
444   if (edge->rule().name() == "cat"  ||
445       edge->rule().name() == "cat_rsp" ||
446       edge->rule().name() == "cc" ||
447       edge->rule().name() == "touch" ||
448       edge->rule().name() == "touch-interrupt") {
449     for (vector<Node*>::iterator out = edge->outputs_.begin();
450          out != edge->outputs_.end(); ++out) {
451       fs_->Create((*out)->path(), "");
452     }
453   } else if (edge->rule().name() == "true" ||
454              edge->rule().name() == "fail" ||
455              edge->rule().name() == "interrupt") {
456     // Don't do anything.
457   } else {
458     printf("unknown command\n");
459     return false;
460   }
461
462   last_command_ = edge;
463   return true;
464 }
465
466 bool FakeCommandRunner::WaitForCommand(Result* result) {
467   if (!last_command_)
468     return false;
469
470   Edge* edge = last_command_;
471   result->edge = edge;
472
473   if (edge->rule().name() == "interrupt" ||
474       edge->rule().name() == "touch-interrupt") {
475     result->status = ExitInterrupted;
476     return true;
477   }
478
479   if (edge->rule().name() == "fail")
480     result->status = ExitFailure;
481   else
482     result->status = ExitSuccess;
483   last_command_ = NULL;
484   return true;
485 }
486
487 vector<Edge*> FakeCommandRunner::GetActiveEdges() {
488   vector<Edge*> edges;
489   if (last_command_)
490     edges.push_back(last_command_);
491   return edges;
492 }
493
494 void FakeCommandRunner::Abort() {
495   last_command_ = NULL;
496 }
497
498 void BuildTest::Dirty(const string& path) {
499   Node* node = GetNode(path);
500   node->MarkDirty();
501
502   // If it's an input file, mark that we've already stat()ed it and
503   // it's missing.
504   if (!node->in_edge())
505     node->MarkMissing();
506 }
507
508 TEST_F(BuildTest, NoWork) {
509   string err;
510   EXPECT_TRUE(builder_.AlreadyUpToDate());
511 }
512
513 TEST_F(BuildTest, OneStep) {
514   // Given a dirty target with one ready input,
515   // we should rebuild the target.
516   Dirty("cat1");
517   string err;
518   EXPECT_TRUE(builder_.AddTarget("cat1", &err));
519   ASSERT_EQ("", err);
520   EXPECT_TRUE(builder_.Build(&err));
521   ASSERT_EQ("", err);
522
523   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
524   EXPECT_EQ("cat in1 > cat1", command_runner_.commands_ran_[0]);
525 }
526
527 TEST_F(BuildTest, OneStep2) {
528   // Given a target with one dirty input,
529   // we should rebuild the target.
530   Dirty("cat1");
531   string err;
532   EXPECT_TRUE(builder_.AddTarget("cat1", &err));
533   ASSERT_EQ("", err);
534   EXPECT_TRUE(builder_.Build(&err));
535   EXPECT_EQ("", err);
536
537   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
538   EXPECT_EQ("cat in1 > cat1", command_runner_.commands_ran_[0]);
539 }
540
541 TEST_F(BuildTest, TwoStep) {
542   string err;
543   EXPECT_TRUE(builder_.AddTarget("cat12", &err));
544   ASSERT_EQ("", err);
545   EXPECT_TRUE(builder_.Build(&err));
546   EXPECT_EQ("", err);
547   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
548   // Depending on how the pointers work out, we could've ran
549   // the first two commands in either order.
550   EXPECT_TRUE((command_runner_.commands_ran_[0] == "cat in1 > cat1" &&
551                command_runner_.commands_ran_[1] == "cat in1 in2 > cat2") ||
552               (command_runner_.commands_ran_[1] == "cat in1 > cat1" &&
553                command_runner_.commands_ran_[0] == "cat in1 in2 > cat2"));
554
555   EXPECT_EQ("cat cat1 cat2 > cat12", command_runner_.commands_ran_[2]);
556
557   fs_.Tick();
558
559   // Modifying in2 requires rebuilding one intermediate file
560   // and the final file.
561   fs_.Create("in2", "");
562   state_.Reset();
563   EXPECT_TRUE(builder_.AddTarget("cat12", &err));
564   ASSERT_EQ("", err);
565   EXPECT_TRUE(builder_.Build(&err));
566   ASSERT_EQ("", err);
567   ASSERT_EQ(5u, command_runner_.commands_ran_.size());
568   EXPECT_EQ("cat in1 in2 > cat2", command_runner_.commands_ran_[3]);
569   EXPECT_EQ("cat cat1 cat2 > cat12", command_runner_.commands_ran_[4]);
570 }
571
572 TEST_F(BuildTest, TwoOutputs) {
573   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
574 "rule touch\n"
575 "  command = touch $out\n"
576 "build out1 out2: touch in.txt\n"));
577
578   fs_.Create("in.txt", "");
579
580   string err;
581   EXPECT_TRUE(builder_.AddTarget("out1", &err));
582   ASSERT_EQ("", err);
583   EXPECT_TRUE(builder_.Build(&err));
584   EXPECT_EQ("", err);
585   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
586   EXPECT_EQ("touch out1 out2", command_runner_.commands_ran_[0]);
587 }
588
589 // Test case from
590 //   https://github.com/martine/ninja/issues/148
591 TEST_F(BuildTest, MultiOutIn) {
592   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
593 "rule touch\n"
594 "  command = touch $out\n"
595 "build in1 otherfile: touch in\n"
596 "build out: touch in | in1\n"));
597
598   fs_.Create("in", "");
599   fs_.Tick();
600   fs_.Create("in1", "");
601
602   string err;
603   EXPECT_TRUE(builder_.AddTarget("out", &err));
604   ASSERT_EQ("", err);
605   EXPECT_TRUE(builder_.Build(&err));
606   EXPECT_EQ("", err);
607 }
608
609 TEST_F(BuildTest, Chain) {
610   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
611 "build c2: cat c1\n"
612 "build c3: cat c2\n"
613 "build c4: cat c3\n"
614 "build c5: cat c4\n"));
615
616   fs_.Create("c1", "");
617
618   string err;
619   EXPECT_TRUE(builder_.AddTarget("c5", &err));
620   ASSERT_EQ("", err);
621   EXPECT_TRUE(builder_.Build(&err));
622   EXPECT_EQ("", err);
623   ASSERT_EQ(4u, command_runner_.commands_ran_.size());
624
625   err.clear();
626   command_runner_.commands_ran_.clear();
627   state_.Reset();
628   EXPECT_TRUE(builder_.AddTarget("c5", &err));
629   ASSERT_EQ("", err);
630   EXPECT_TRUE(builder_.AlreadyUpToDate());
631
632   fs_.Tick();
633
634   fs_.Create("c3", "");
635   err.clear();
636   command_runner_.commands_ran_.clear();
637   state_.Reset();
638   EXPECT_TRUE(builder_.AddTarget("c5", &err));
639   ASSERT_EQ("", err);
640   EXPECT_FALSE(builder_.AlreadyUpToDate());
641   EXPECT_TRUE(builder_.Build(&err));
642   ASSERT_EQ(2u, command_runner_.commands_ran_.size());  // 3->4, 4->5
643 }
644
645 TEST_F(BuildTest, MissingInput) {
646   // Input is referenced by build file, but no rule for it.
647   string err;
648   Dirty("in1");
649   EXPECT_FALSE(builder_.AddTarget("cat1", &err));
650   EXPECT_EQ("'in1', needed by 'cat1', missing and no known rule to make it",
651             err);
652 }
653
654 TEST_F(BuildTest, MissingTarget) {
655   // Target is not referenced by build file.
656   string err;
657   EXPECT_FALSE(builder_.AddTarget("meow", &err));
658   EXPECT_EQ("unknown target: 'meow'", err);
659 }
660
661 TEST_F(BuildTest, MakeDirs) {
662   string err;
663
664 #ifdef _WIN32
665   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
666                                       "build subdir\\dir2\\file: cat in1\n"));
667   EXPECT_TRUE(builder_.AddTarget("subdir\\dir2\\file", &err));
668 #else
669   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
670                                       "build subdir/dir2/file: cat in1\n"));
671   EXPECT_TRUE(builder_.AddTarget("subdir/dir2/file", &err));
672 #endif
673
674   EXPECT_EQ("", err);
675   EXPECT_TRUE(builder_.Build(&err));
676   ASSERT_EQ("", err);
677   ASSERT_EQ(2u, fs_.directories_made_.size());
678   EXPECT_EQ("subdir", fs_.directories_made_[0]);
679 #ifdef _WIN32
680   EXPECT_EQ("subdir\\dir2", fs_.directories_made_[1]);
681 #else
682   EXPECT_EQ("subdir/dir2", fs_.directories_made_[1]);
683 #endif
684 }
685
686 TEST_F(BuildTest, DepFileMissing) {
687   string err;
688   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
689 "rule cc\n  command = cc $in\n  depfile = $out.d\n"
690 "build foo.o: cc foo.c\n"));
691   fs_.Create("foo.c", "");
692
693   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
694   ASSERT_EQ("", err);
695   ASSERT_EQ(1u, fs_.files_read_.size());
696   EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
697 }
698
699 TEST_F(BuildTest, DepFileOK) {
700   string err;
701   int orig_edges = state_.edges_.size();
702   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
703 "rule cc\n  command = cc $in\n  depfile = $out.d\n"
704 "build foo.o: cc foo.c\n"));
705   Edge* edge = state_.edges_.back();
706
707   fs_.Create("foo.c", "");
708   GetNode("bar.h")->MarkDirty();  // Mark bar.h as missing.
709   fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
710   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
711   ASSERT_EQ("", err);
712   ASSERT_EQ(1u, fs_.files_read_.size());
713   EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
714
715   // Expect three new edges: one generating foo.o, and two more from
716   // loading the depfile.
717   ASSERT_EQ(orig_edges + 3, (int)state_.edges_.size());
718   // Expect our edge to now have three inputs: foo.c and two headers.
719   ASSERT_EQ(3u, edge->inputs_.size());
720
721   // Expect the command line we generate to only use the original input.
722   ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
723 }
724
725 TEST_F(BuildTest, DepFileParseError) {
726   string err;
727   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
728 "rule cc\n  command = cc $in\n  depfile = $out.d\n"
729 "build foo.o: cc foo.c\n"));
730   fs_.Create("foo.c", "");
731   fs_.Create("foo.o.d", "randomtext\n");
732   EXPECT_FALSE(builder_.AddTarget("foo.o", &err));
733   EXPECT_EQ("expected depfile 'foo.o.d' to mention 'foo.o', got 'randomtext'",
734             err);
735 }
736
737 TEST_F(BuildTest, OrderOnlyDeps) {
738   string err;
739   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
740 "rule cc\n  command = cc $in\n  depfile = $out.d\n"
741 "build foo.o: cc foo.c || otherfile\n"));
742   Edge* edge = state_.edges_.back();
743
744   fs_.Create("foo.c", "");
745   fs_.Create("otherfile", "");
746   fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
747   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
748   ASSERT_EQ("", err);
749
750   // One explicit, two implicit, one order only.
751   ASSERT_EQ(4u, edge->inputs_.size());
752   EXPECT_EQ(2, edge->implicit_deps_);
753   EXPECT_EQ(1, edge->order_only_deps_);
754   // Verify the inputs are in the order we expect
755   // (explicit then implicit then orderonly).
756   EXPECT_EQ("foo.c", edge->inputs_[0]->path());
757   EXPECT_EQ("blah.h", edge->inputs_[1]->path());
758   EXPECT_EQ("bar.h", edge->inputs_[2]->path());
759   EXPECT_EQ("otherfile", edge->inputs_[3]->path());
760
761   // Expect the command line we generate to only use the original input.
762   ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
763
764   // explicit dep dirty, expect a rebuild.
765   EXPECT_TRUE(builder_.Build(&err));
766   ASSERT_EQ("", err);
767   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
768
769   fs_.Tick();
770
771   // Recreate the depfile, as it should have been deleted by the build.
772   fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
773
774   // implicit dep dirty, expect a rebuild.
775   fs_.Create("blah.h", "");
776   fs_.Create("bar.h", "");
777   command_runner_.commands_ran_.clear();
778   state_.Reset();
779   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
780   EXPECT_TRUE(builder_.Build(&err));
781   ASSERT_EQ("", err);
782   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
783
784   fs_.Tick();
785
786   // Recreate the depfile, as it should have been deleted by the build.
787   fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
788
789   // order only dep dirty, no rebuild.
790   fs_.Create("otherfile", "");
791   command_runner_.commands_ran_.clear();
792   state_.Reset();
793   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
794   EXPECT_EQ("", err);
795   EXPECT_TRUE(builder_.AlreadyUpToDate());
796
797   // implicit dep missing, expect rebuild.
798   fs_.RemoveFile("bar.h");
799   command_runner_.commands_ran_.clear();
800   state_.Reset();
801   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
802   EXPECT_TRUE(builder_.Build(&err));
803   ASSERT_EQ("", err);
804   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
805 }
806
807 TEST_F(BuildTest, RebuildOrderOnlyDeps) {
808   string err;
809   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
810 "rule cc\n  command = cc $in\n"
811 "rule true\n  command = true\n"
812 "build oo.h: cc oo.h.in\n"
813 "build foo.o: cc foo.c || oo.h\n"));
814
815   fs_.Create("foo.c", "");
816   fs_.Create("oo.h.in", "");
817
818   // foo.o and order-only dep dirty, build both.
819   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
820   EXPECT_TRUE(builder_.Build(&err));
821   ASSERT_EQ("", err);
822   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
823
824   // all clean, no rebuild.
825   command_runner_.commands_ran_.clear();
826   state_.Reset();
827   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
828   EXPECT_EQ("", err);
829   EXPECT_TRUE(builder_.AlreadyUpToDate());
830
831   // order-only dep missing, build it only.
832   fs_.RemoveFile("oo.h");
833   command_runner_.commands_ran_.clear();
834   state_.Reset();
835   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
836   EXPECT_TRUE(builder_.Build(&err));
837   ASSERT_EQ("", err);
838   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
839   ASSERT_EQ("cc oo.h.in", command_runner_.commands_ran_[0]);
840
841   fs_.Tick();
842
843   // order-only dep dirty, build it only.
844   fs_.Create("oo.h.in", "");
845   command_runner_.commands_ran_.clear();
846   state_.Reset();
847   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
848   EXPECT_TRUE(builder_.Build(&err));
849   ASSERT_EQ("", err);
850   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
851   ASSERT_EQ("cc oo.h.in", command_runner_.commands_ran_[0]);
852 }
853
854 TEST_F(BuildTest, Phony) {
855   string err;
856   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
857 "build out: cat bar.cc\n"
858 "build all: phony out\n"));
859   fs_.Create("bar.cc", "");
860
861   EXPECT_TRUE(builder_.AddTarget("all", &err));
862   ASSERT_EQ("", err);
863
864   // Only one command to run, because phony runs no command.
865   EXPECT_FALSE(builder_.AlreadyUpToDate());
866   EXPECT_TRUE(builder_.Build(&err));
867   ASSERT_EQ("", err);
868   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
869 }
870
871 TEST_F(BuildTest, PhonyNoWork) {
872   string err;
873   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
874 "build out: cat bar.cc\n"
875 "build all: phony out\n"));
876   fs_.Create("bar.cc", "");
877   fs_.Create("out", "");
878
879   EXPECT_TRUE(builder_.AddTarget("all", &err));
880   ASSERT_EQ("", err);
881   EXPECT_TRUE(builder_.AlreadyUpToDate());
882 }
883
884 TEST_F(BuildTest, Fail) {
885   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
886 "rule fail\n"
887 "  command = fail\n"
888 "build out1: fail\n"));
889
890   string err;
891   EXPECT_TRUE(builder_.AddTarget("out1", &err));
892   ASSERT_EQ("", err);
893
894   EXPECT_FALSE(builder_.Build(&err));
895   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
896   ASSERT_EQ("subcommand failed", err);
897 }
898
899 TEST_F(BuildTest, SwallowFailures) {
900   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
901 "rule fail\n"
902 "  command = fail\n"
903 "build out1: fail\n"
904 "build out2: fail\n"
905 "build out3: fail\n"
906 "build all: phony out1 out2 out3\n"));
907
908   // Swallow two failures, die on the third.
909   config_.failures_allowed = 3;
910
911   string err;
912   EXPECT_TRUE(builder_.AddTarget("all", &err));
913   ASSERT_EQ("", err);
914
915   EXPECT_FALSE(builder_.Build(&err));
916   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
917   ASSERT_EQ("subcommands failed", err);
918 }
919
920 TEST_F(BuildTest, SwallowFailuresLimit) {
921   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
922 "rule fail\n"
923 "  command = fail\n"
924 "build out1: fail\n"
925 "build out2: fail\n"
926 "build out3: fail\n"
927 "build final: cat out1 out2 out3\n"));
928
929   // Swallow ten failures; we should stop before building final.
930   config_.failures_allowed = 11;
931
932   string err;
933   EXPECT_TRUE(builder_.AddTarget("final", &err));
934   ASSERT_EQ("", err);
935
936   EXPECT_FALSE(builder_.Build(&err));
937   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
938   ASSERT_EQ("cannot make progress due to previous errors", err);
939 }
940
941 struct BuildWithLogTest : public BuildTest {
942   BuildWithLogTest() {
943     builder_.SetBuildLog(&build_log_);
944   }
945
946   BuildLog build_log_;
947 };
948
949 TEST_F(BuildWithLogTest, NotInLogButOnDisk) {
950   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
951 "rule cc\n"
952 "  command = cc\n"
953 "build out1: cc in\n"));
954
955   // Create input/output that would be considered up to date when
956   // not considering the command line hash.
957   fs_.Create("in", "");
958   fs_.Create("out1", "");
959   string err;
960
961   // Because it's not in the log, it should not be up-to-date until
962   // we build again.
963   EXPECT_TRUE(builder_.AddTarget("out1", &err));
964   EXPECT_FALSE(builder_.AlreadyUpToDate());
965
966   command_runner_.commands_ran_.clear();
967   state_.Reset();
968
969   EXPECT_TRUE(builder_.AddTarget("out1", &err));
970   EXPECT_TRUE(builder_.Build(&err));
971   EXPECT_TRUE(builder_.AlreadyUpToDate());
972 }
973
974 TEST_F(BuildWithLogTest, RestatTest) {
975   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
976 "rule true\n"
977 "  command = true\n"
978 "  restat = 1\n"
979 "rule cc\n"
980 "  command = cc\n"
981 "  restat = 1\n"
982 "build out1: cc in\n"
983 "build out2: true out1\n"
984 "build out3: cat out2\n"));
985
986   fs_.Create("out1", "");
987   fs_.Create("out2", "");
988   fs_.Create("out3", "");
989
990   fs_.Tick();
991
992   fs_.Create("in", "");
993
994   // Do a pre-build so that there's commands in the log for the outputs,
995   // otherwise, the lack of an entry in the build log will cause out3 to rebuild
996   // regardless of restat.
997   string err;
998   EXPECT_TRUE(builder_.AddTarget("out3", &err));
999   ASSERT_EQ("", err);
1000   EXPECT_TRUE(builder_.Build(&err));
1001   ASSERT_EQ("", err);
1002   command_runner_.commands_ran_.clear();
1003   state_.Reset();
1004
1005   fs_.Tick();
1006
1007   fs_.Create("in", "");
1008   // "cc" touches out1, so we should build out2.  But because "true" does not
1009   // touch out2, we should cancel the build of out3.
1010   EXPECT_TRUE(builder_.AddTarget("out3", &err));
1011   ASSERT_EQ("", err);
1012   EXPECT_TRUE(builder_.Build(&err));
1013   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1014
1015   // If we run again, it should be a no-op, because the build log has recorded
1016   // that we've already built out2 with an input timestamp of 2 (from out1).
1017   command_runner_.commands_ran_.clear();
1018   state_.Reset();
1019   EXPECT_TRUE(builder_.AddTarget("out3", &err));
1020   ASSERT_EQ("", err);
1021   EXPECT_TRUE(builder_.AlreadyUpToDate());
1022
1023   fs_.Tick();
1024
1025   fs_.Create("in", "");
1026
1027   // The build log entry should not, however, prevent us from rebuilding out2
1028   // if out1 changes.
1029   command_runner_.commands_ran_.clear();
1030   state_.Reset();
1031   EXPECT_TRUE(builder_.AddTarget("out3", &err));
1032   ASSERT_EQ("", err);
1033   EXPECT_TRUE(builder_.Build(&err));
1034   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1035 }
1036
1037 TEST_F(BuildWithLogTest, RestatMissingFile) {
1038   // If a restat rule doesn't create its output, and the output didn't
1039   // exist before the rule was run, consider that behavior equivalent
1040   // to a rule that doesn't modify its existent output file.
1041
1042   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1043 "rule true\n"
1044 "  command = true\n"
1045 "  restat = 1\n"
1046 "rule cc\n"
1047 "  command = cc\n"
1048 "build out1: true in\n"
1049 "build out2: cc out1\n"));
1050
1051   fs_.Create("in", "");
1052   fs_.Create("out2", "");
1053
1054   // Do a pre-build so that there's commands in the log for the outputs,
1055   // otherwise, the lack of an entry in the build log will cause out2 to rebuild
1056   // regardless of restat.
1057   string err;
1058   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1059   ASSERT_EQ("", err);
1060   EXPECT_TRUE(builder_.Build(&err));
1061   ASSERT_EQ("", err);
1062   command_runner_.commands_ran_.clear();
1063   state_.Reset();
1064
1065   fs_.Tick();
1066   fs_.Create("in", "");
1067   fs_.Create("out2", "");
1068
1069   // Run a build, expect only the first command to run.
1070   // It doesn't touch its output (due to being the "true" command), so
1071   // we shouldn't run the dependent build.
1072   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1073   ASSERT_EQ("", err);
1074   EXPECT_TRUE(builder_.Build(&err));
1075   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1076 }
1077
1078 // Test scenario, in which an input file is removed, but output isn't changed
1079 // https://github.com/martine/ninja/issues/295
1080 TEST_F(BuildWithLogTest, RestatMissingInput) {
1081   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1082     "rule true\n"
1083     "  command = true\n"
1084     "  depfile = $out.d\n"
1085     "  restat = 1\n"
1086     "rule cc\n"
1087     "  command = cc\n"
1088     "build out1: true in\n"
1089     "build out2: cc out1\n"));
1090
1091   // Create all necessary files
1092   fs_.Create("in", "");
1093
1094   // The implicit dependencies and the depfile itself
1095   // are newer than the output
1096   TimeStamp restat_mtime = fs_.Tick();
1097   fs_.Create("out1.d", "out1: will.be.deleted restat.file\n");
1098   fs_.Create("will.be.deleted", "");
1099   fs_.Create("restat.file", "");
1100
1101   // Run the build, out1 and out2 get built
1102   string err;
1103   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1104   ASSERT_EQ("", err);
1105   EXPECT_TRUE(builder_.Build(&err));
1106   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1107
1108   // See that an entry in the logfile is created, capturing
1109   // the right mtime
1110   BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out1");
1111   ASSERT_TRUE(NULL != log_entry);
1112   ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
1113
1114   // Now remove a file, referenced from depfile, so that target becomes
1115   // dirty, but the output does not change
1116   fs_.RemoveFile("will.be.deleted");
1117
1118   // Trigger the build again - only out1 gets built
1119   command_runner_.commands_ran_.clear();
1120   state_.Reset();
1121   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1122   ASSERT_EQ("", err);
1123   EXPECT_TRUE(builder_.Build(&err));
1124   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1125
1126   // Check that the logfile entry remains correctly set
1127   log_entry = build_log_.LookupByOutput("out1");
1128   ASSERT_TRUE(NULL != log_entry);
1129   ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
1130 }
1131
1132 struct BuildDryRun : public BuildWithLogTest {
1133   BuildDryRun() {
1134     config_.dry_run = true;
1135   }
1136 };
1137
1138 TEST_F(BuildDryRun, AllCommandsShown) {
1139   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1140 "rule true\n"
1141 "  command = true\n"
1142 "  restat = 1\n"
1143 "rule cc\n"
1144 "  command = cc\n"
1145 "  restat = 1\n"
1146 "build out1: cc in\n"
1147 "build out2: true out1\n"
1148 "build out3: cat out2\n"));
1149
1150   fs_.Create("out1", "");
1151   fs_.Create("out2", "");
1152   fs_.Create("out3", "");
1153
1154   fs_.Tick();
1155
1156   fs_.Create("in", "");
1157
1158   // "cc" touches out1, so we should build out2.  But because "true" does not
1159   // touch out2, we should cancel the build of out3.
1160   string err;
1161   EXPECT_TRUE(builder_.AddTarget("out3", &err));
1162   ASSERT_EQ("", err);
1163   EXPECT_TRUE(builder_.Build(&err));
1164   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1165 }
1166
1167 // Test that RSP files are created when & where appropriate and deleted after
1168 // successful execution.
1169 TEST_F(BuildTest, RspFileSuccess)
1170 {
1171   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1172     "rule cat_rsp\n"
1173     "  command = cat $rspfile > $out\n"
1174     "  rspfile = $rspfile\n"
1175     "  rspfile_content = $long_command\n"
1176     "build out1: cat in\n"
1177     "build out2: cat_rsp in\n"
1178     "  rspfile = out2.rsp\n"
1179     "  long_command = Some very long command\n"));
1180
1181   fs_.Create("out1", "");
1182   fs_.Create("out2", "");
1183   fs_.Create("out3", "");
1184
1185   fs_.Tick();
1186
1187   fs_.Create("in", "");
1188
1189   string err;
1190   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1191   ASSERT_EQ("", err);
1192   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1193   ASSERT_EQ("", err);
1194
1195   size_t files_created = fs_.files_created_.size();
1196   size_t files_removed = fs_.files_removed_.size();
1197
1198   EXPECT_TRUE(builder_.Build(&err));
1199   ASSERT_EQ(2u, command_runner_.commands_ran_.size()); // cat + cat_rsp
1200
1201   // The RSP file was created
1202   ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1203   ASSERT_EQ(1u, fs_.files_created_.count("out2.rsp"));
1204
1205   // The RSP file was removed
1206   ASSERT_EQ(files_removed + 1, fs_.files_removed_.size());
1207   ASSERT_EQ(1u, fs_.files_removed_.count("out2.rsp"));
1208 }
1209
1210 // Test that RSP file is created but not removed for commands, which fail
1211 TEST_F(BuildTest, RspFileFailure) {
1212   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1213     "rule fail\n"
1214     "  command = fail\n"
1215     "  rspfile = $rspfile\n"
1216     "  rspfile_content = $long_command\n"
1217     "build out: fail in\n"
1218     "  rspfile = out.rsp\n"
1219     "  long_command = Another very long command\n"));
1220
1221   fs_.Create("out", "");
1222   fs_.Tick();
1223   fs_.Create("in", "");
1224
1225   string err;
1226   EXPECT_TRUE(builder_.AddTarget("out", &err));
1227   ASSERT_EQ("", err);
1228
1229   size_t files_created = fs_.files_created_.size();
1230   size_t files_removed = fs_.files_removed_.size();
1231
1232   EXPECT_FALSE(builder_.Build(&err));
1233   ASSERT_EQ("subcommand failed", err);
1234   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1235
1236   // The RSP file was created
1237   ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1238   ASSERT_EQ(1u, fs_.files_created_.count("out.rsp"));
1239
1240   // The RSP file was NOT removed
1241   ASSERT_EQ(files_removed, fs_.files_removed_.size());
1242   ASSERT_EQ(0u, fs_.files_removed_.count("out.rsp"));
1243
1244   // The RSP file contains what it should
1245   ASSERT_EQ("Another very long command", fs_.files_["out.rsp"].contents);
1246 }
1247
1248 // Test that contens of the RSP file behaves like a regular part of
1249 // command line, i.e. triggers a rebuild if changed
1250 TEST_F(BuildWithLogTest, RspFileCmdLineChange) {
1251   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1252     "rule cat_rsp\n"
1253     "  command = cat $rspfile > $out\n"
1254     "  rspfile = $rspfile\n"
1255     "  rspfile_content = $long_command\n"
1256     "build out: cat_rsp in\n"
1257     "  rspfile = out.rsp\n"
1258     "  long_command = Original very long command\n"));
1259
1260   fs_.Create("out", "");
1261   fs_.Tick();
1262   fs_.Create("in", "");
1263
1264   string err;
1265   EXPECT_TRUE(builder_.AddTarget("out", &err));
1266   ASSERT_EQ("", err);
1267
1268   // 1. Build for the 1st time (-> populate log)
1269   EXPECT_TRUE(builder_.Build(&err));
1270   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1271
1272   // 2. Build again (no change)
1273   command_runner_.commands_ran_.clear();
1274   state_.Reset();
1275   EXPECT_TRUE(builder_.AddTarget("out", &err));
1276   EXPECT_EQ("", err);
1277   ASSERT_TRUE(builder_.AlreadyUpToDate());
1278
1279   // 3. Alter the entry in the logfile
1280   // (to simulate a change in the command line between 2 builds)
1281   BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out");
1282   ASSERT_TRUE(NULL != log_entry);
1283   ASSERT_NO_FATAL_FAILURE(AssertHash(
1284         "cat out.rsp > out;rspfile=Original very long command",
1285         log_entry->command_hash));
1286   log_entry->command_hash++;  // Change the command hash to something else.
1287   // Now expect the target to be rebuilt
1288   command_runner_.commands_ran_.clear();
1289   state_.Reset();
1290   EXPECT_TRUE(builder_.AddTarget("out", &err));
1291   EXPECT_EQ("", err);
1292   EXPECT_TRUE(builder_.Build(&err));
1293   EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1294 }
1295
1296 TEST_F(BuildTest, InterruptCleanup) {
1297   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1298 "rule interrupt\n"
1299 "  command = interrupt\n"
1300 "rule touch-interrupt\n"
1301 "  command = touch-interrupt\n"
1302 "build out1: interrupt in1\n"
1303 "build out2: touch-interrupt in2\n"));
1304
1305   fs_.Create("out1", "");
1306   fs_.Create("out2", "");
1307   fs_.Tick();
1308   fs_.Create("in1", "");
1309   fs_.Create("in2", "");
1310
1311   // An untouched output of an interrupted command should be retained.
1312   string err;
1313   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1314   EXPECT_EQ("", err);
1315   EXPECT_FALSE(builder_.Build(&err));
1316   EXPECT_EQ("interrupted by user", err);
1317   builder_.Cleanup();
1318   EXPECT_GT(fs_.Stat("out1"), 0);
1319   err = "";
1320
1321   // A touched output of an interrupted command should be deleted.
1322   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1323   EXPECT_EQ("", err);
1324   EXPECT_FALSE(builder_.Build(&err));
1325   EXPECT_EQ("interrupted by user", err);
1326   builder_.Cleanup();
1327   EXPECT_EQ(0, fs_.Stat("out2"));
1328 }
1329
1330 TEST_F(BuildTest, PhonyWithNoInputs) {
1331   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1332 "build nonexistent: phony\n"
1333 "build out1: cat || nonexistent\n"
1334 "build out2: cat nonexistent\n"));
1335   fs_.Create("out1", "");
1336   fs_.Create("out2", "");
1337
1338   // out1 should be up to date even though its input is dirty, because its
1339   // order-only dependency has nothing to do.
1340   string err;
1341   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1342   ASSERT_EQ("", err);
1343   EXPECT_TRUE(builder_.AlreadyUpToDate());
1344
1345   // out2 should still be out of date though, because its input is dirty.
1346   err.clear();
1347   command_runner_.commands_ran_.clear();
1348   state_.Reset();
1349   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1350   ASSERT_EQ("", err);
1351   EXPECT_TRUE(builder_.Build(&err));
1352   EXPECT_EQ("", err);
1353   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1354 }
1355
1356 TEST_F(BuildTest, DepsGccWithEmptyDeps) {
1357   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1358 "rule cc\n"
1359 "  command = cc\n"
1360 "  deps = gcc\n"
1361 "build out: cc\n"));
1362   Dirty("out");
1363
1364   string err;
1365   EXPECT_TRUE(builder_.AddTarget("out", &err));
1366   ASSERT_EQ("", err);
1367   EXPECT_FALSE(builder_.AlreadyUpToDate());
1368
1369   EXPECT_FALSE(builder_.Build(&err));
1370   ASSERT_EQ("subcommand failed", err);
1371   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1372 }
1373
1374 TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
1375   EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
1376             status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]"));
1377 }
1378
1379 TEST_F(BuildTest, FailedDepsParse) {
1380   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1381 "build bad_deps.o: cat in1\n"
1382 "  deps = gcc\n"
1383 "  depfile = in1.d\n"));
1384
1385   string err;
1386   EXPECT_TRUE(builder_.AddTarget("bad_deps.o", &err));
1387   ASSERT_EQ("", err);
1388
1389   // These deps will fail to parse, as they should only have one
1390   // path to the left of the colon.
1391   fs_.Create("in1.d", "AAA BBB");
1392
1393   EXPECT_FALSE(builder_.Build(&err));
1394   EXPECT_EQ("subcommand failed", err);
1395 }
1396
1397 /// Tests of builds involving deps logs necessarily must span
1398 /// multiple builds.  We reuse methods on BuildTest but not the
1399 /// builder_ it sets up, because we want pristine objects for
1400 /// each build.
1401 struct BuildWithDepsLogTest : public BuildTest {
1402   BuildWithDepsLogTest() {}
1403
1404   virtual void SetUp() {
1405     BuildTest::SetUp();
1406
1407     temp_dir_.CreateAndEnter("BuildWithDepsLogTest");
1408   }
1409
1410   virtual void TearDown() {
1411     temp_dir_.Cleanup();
1412   }
1413
1414   ScopedTempDir temp_dir_;
1415
1416   /// Shadow parent class builder_ so we don't accidentally use it.
1417   void* builder_;
1418 };
1419
1420 /// Run a straightforwad build where the deps log is used.
1421 TEST_F(BuildWithDepsLogTest, Straightforward) {
1422   string err;
1423   // Note: in1 was created by the superclass SetUp().
1424   const char* manifest =
1425       "build out: cat in1\n"
1426       "  deps = gcc\n"
1427       "  depfile = in1.d\n";
1428   {
1429     State state;
1430     ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1431     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1432
1433     // Run the build once, everything should be ok.
1434     DepsLog deps_log;
1435     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1436     ASSERT_EQ("", err);
1437
1438     Builder builder(&state, config_, NULL, &deps_log, &fs_);
1439     builder.command_runner_.reset(&command_runner_);
1440     EXPECT_TRUE(builder.AddTarget("out", &err));
1441     ASSERT_EQ("", err);
1442     fs_.Create("in1.d", "out: in2");
1443     EXPECT_TRUE(builder.Build(&err));
1444     EXPECT_EQ("", err);
1445
1446     // The deps file should have been removed.
1447     EXPECT_EQ(0, fs_.Stat("in1.d"));
1448     // Recreate it for the next step.
1449     fs_.Create("in1.d", "out: in2");
1450     deps_log.Close();
1451     builder.command_runner_.release();
1452   }
1453
1454   {
1455     State state;
1456     ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1457     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1458
1459     // Touch the file only mentioned in the deps.
1460     fs_.Tick();
1461     fs_.Create("in2", "");
1462
1463     // Run the build again.
1464     DepsLog deps_log;
1465     ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1466     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1467
1468     Builder builder(&state, config_, NULL, &deps_log, &fs_);
1469     builder.command_runner_.reset(&command_runner_);
1470     command_runner_.commands_ran_.clear();
1471     EXPECT_TRUE(builder.AddTarget("out", &err));
1472     ASSERT_EQ("", err);
1473     EXPECT_TRUE(builder.Build(&err));
1474     EXPECT_EQ("", err);
1475
1476     // We should have rebuilt the output due to in2 being
1477     // out of date.
1478     EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1479
1480     builder.command_runner_.release();
1481   }
1482 }
1483
1484 /// Verify that obsolete deps still cause a rebuild.
1485 TEST_F(BuildWithDepsLogTest, ObsoleteDeps) {
1486   string err;
1487   // Note: in1 was created by the superclass SetUp().
1488   const char* manifest =
1489       "build out: cat in1\n"
1490       "  deps = gcc\n"
1491       "  depfile = in1.d\n";
1492   {
1493     // Create the obsolete deps, then run a build to incorporate them.
1494     // The idea is that the inputs/outputs are newer than the logged
1495     // deps.
1496     fs_.Create("in1.d", "out: ");
1497     fs_.Tick();
1498
1499     fs_.Create("in1", "");
1500
1501     State state;
1502     ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1503     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1504
1505     // Run the build once, everything should be ok.
1506     DepsLog deps_log;
1507     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1508     ASSERT_EQ("", err);
1509
1510     Builder builder(&state, config_, NULL, &deps_log, &fs_);
1511     builder.command_runner_.reset(&command_runner_);
1512     EXPECT_TRUE(builder.AddTarget("out", &err));
1513     ASSERT_EQ("", err);
1514     EXPECT_TRUE(builder.Build(&err));
1515     EXPECT_EQ("", err);
1516
1517     fs_.Create("out", "");
1518     // The deps file should have been removed.
1519     EXPECT_EQ(0, fs_.Stat("in1.d"));
1520     deps_log.Close();
1521     builder.command_runner_.release();
1522   }
1523
1524   // Now we should be in a situation where in1/out2 both have recent
1525   // timestamps but the deps are old.  Verify we rebuild.
1526   fs_.Tick();
1527
1528   {
1529     State state;
1530     ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1531     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1532
1533     DepsLog deps_log;
1534     ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1535     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1536
1537     Builder builder(&state, config_, NULL, &deps_log, &fs_);
1538     builder.command_runner_.reset(&command_runner_);
1539     command_runner_.commands_ran_.clear();
1540     EXPECT_TRUE(builder.AddTarget("out", &err));
1541     ASSERT_EQ("", err);
1542
1543     // Recreate the deps here just to prove the old recorded deps are
1544     // the problem.
1545     fs_.Create("in1.d", "out: ");
1546
1547     EXPECT_TRUE(builder.Build(&err));
1548     EXPECT_EQ("", err);
1549
1550     // We should have rebuilt the output due to the deps being
1551     // out of date.
1552     EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1553
1554     builder.command_runner_.release();
1555   }
1556 }