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