Merge pull request #441 from tfarina/missing-virtual
[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 "graph.h"
19 #include "test.h"
20
21 /// Fixture for tests involving Plan.
22 // Though Plan doesn't use State, it's useful to have one around
23 // to create Nodes and Edges.
24 struct PlanTest : public StateTestWithBuiltinRules {
25   Plan plan_;
26 };
27
28 TEST_F(PlanTest, Basic) {
29   AssertParse(&state_,
30 "build out: cat mid\n"
31 "build mid: cat in\n");
32   GetNode("mid")->MarkDirty();
33   GetNode("out")->MarkDirty();
34   string err;
35   EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
36   ASSERT_EQ("", err);
37   ASSERT_TRUE(plan_.more_to_do());
38
39   Edge* edge = plan_.FindWork();
40   ASSERT_TRUE(edge);
41   ASSERT_EQ("in",  edge->inputs_[0]->path());
42   ASSERT_EQ("mid", edge->outputs_[0]->path());
43
44   ASSERT_FALSE(plan_.FindWork());
45
46   plan_.EdgeFinished(edge);
47
48   edge = plan_.FindWork();
49   ASSERT_TRUE(edge);
50   ASSERT_EQ("mid", edge->inputs_[0]->path());
51   ASSERT_EQ("out", edge->outputs_[0]->path());
52
53   plan_.EdgeFinished(edge);
54
55   ASSERT_FALSE(plan_.more_to_do());
56   edge = plan_.FindWork();
57   ASSERT_EQ(0, edge);
58 }
59
60 // Test that two outputs from one rule can be handled as inputs to the next.
61 TEST_F(PlanTest, DoubleOutputDirect) {
62   AssertParse(&state_,
63 "build out: cat mid1 mid2\n"
64 "build mid1 mid2: cat in\n");
65   GetNode("mid1")->MarkDirty();
66   GetNode("mid2")->MarkDirty();
67   GetNode("out")->MarkDirty();
68
69   string err;
70   EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
71   ASSERT_EQ("", err);
72   ASSERT_TRUE(plan_.more_to_do());
73
74   Edge* edge;
75   edge = plan_.FindWork();
76   ASSERT_TRUE(edge);  // cat in
77   plan_.EdgeFinished(edge);
78
79   edge = plan_.FindWork();
80   ASSERT_TRUE(edge);  // cat mid1 mid2
81   plan_.EdgeFinished(edge);
82
83   edge = plan_.FindWork();
84   ASSERT_FALSE(edge);  // done
85 }
86
87 // Test that two outputs from one rule can eventually be routed to another.
88 TEST_F(PlanTest, DoubleOutputIndirect) {
89   AssertParse(&state_,
90 "build out: cat b1 b2\n"
91 "build b1: cat a1\n"
92 "build b2: cat a2\n"
93 "build a1 a2: cat in\n");
94   GetNode("a1")->MarkDirty();
95   GetNode("a2")->MarkDirty();
96   GetNode("b1")->MarkDirty();
97   GetNode("b2")->MarkDirty();
98   GetNode("out")->MarkDirty();
99   string err;
100   EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
101   ASSERT_EQ("", err);
102   ASSERT_TRUE(plan_.more_to_do());
103
104   Edge* edge;
105   edge = plan_.FindWork();
106   ASSERT_TRUE(edge);  // cat in
107   plan_.EdgeFinished(edge);
108
109   edge = plan_.FindWork();
110   ASSERT_TRUE(edge);  // cat a1
111   plan_.EdgeFinished(edge);
112
113   edge = plan_.FindWork();
114   ASSERT_TRUE(edge);  // cat a2
115   plan_.EdgeFinished(edge);
116
117   edge = plan_.FindWork();
118   ASSERT_TRUE(edge);  // cat b1 b2
119   plan_.EdgeFinished(edge);
120
121   edge = plan_.FindWork();
122   ASSERT_FALSE(edge);  // done
123 }
124
125 // Test that two edges from one output can both execute.
126 TEST_F(PlanTest, DoubleDependent) {
127   AssertParse(&state_,
128 "build out: cat a1 a2\n"
129 "build a1: cat mid\n"
130 "build a2: cat mid\n"
131 "build mid: cat in\n");
132   GetNode("mid")->MarkDirty();
133   GetNode("a1")->MarkDirty();
134   GetNode("a2")->MarkDirty();
135   GetNode("out")->MarkDirty();
136
137   string err;
138   EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
139   ASSERT_EQ("", err);
140   ASSERT_TRUE(plan_.more_to_do());
141
142   Edge* edge;
143   edge = plan_.FindWork();
144   ASSERT_TRUE(edge);  // cat in
145   plan_.EdgeFinished(edge);
146
147   edge = plan_.FindWork();
148   ASSERT_TRUE(edge);  // cat mid
149   plan_.EdgeFinished(edge);
150
151   edge = plan_.FindWork();
152   ASSERT_TRUE(edge);  // cat mid
153   plan_.EdgeFinished(edge);
154
155   edge = plan_.FindWork();
156   ASSERT_TRUE(edge);  // cat a1 a2
157   plan_.EdgeFinished(edge);
158
159   edge = plan_.FindWork();
160   ASSERT_FALSE(edge);  // done
161 }
162
163 TEST_F(PlanTest, DependencyCycle) {
164   AssertParse(&state_,
165 "build out: cat mid\n"
166 "build mid: cat in\n"
167 "build in: cat pre\n"
168 "build pre: cat out\n");
169   GetNode("out")->MarkDirty();
170   GetNode("mid")->MarkDirty();
171   GetNode("in")->MarkDirty();
172   GetNode("pre")->MarkDirty();
173
174   string err;
175   EXPECT_FALSE(plan_.AddTarget(GetNode("out"), &err));
176   ASSERT_EQ("dependency cycle: out -> mid -> in -> pre -> out", err);
177 }
178
179 struct BuildTest : public StateTestWithBuiltinRules,
180                    public CommandRunner {
181   BuildTest() : config_(MakeConfig()),
182                 builder_(&state_, config_, NULL, &fs_),
183                 now_(1), last_command_(NULL), status_(config_) {
184     builder_.command_runner_.reset(this);
185     AssertParse(&state_,
186 "build cat1: cat in1\n"
187 "build cat2: cat in1 in2\n"
188 "build cat12: cat cat1 cat2\n");
189
190     fs_.Create("in1", now_, "");
191     fs_.Create("in2", now_, "");
192   }
193
194   ~BuildTest() {
195     builder_.command_runner_.release();
196   }
197
198   // Mark a path dirty.
199   void Dirty(const string& path);
200
201   // CommandRunner impl
202   virtual bool CanRunMore();
203   virtual bool StartCommand(Edge* edge);
204   virtual Edge* WaitForCommand(ExitStatus* status, string* output);
205   virtual vector<Edge*> GetActiveEdges();
206   virtual void Abort();
207
208   BuildConfig MakeConfig() {
209     BuildConfig config;
210     config.verbosity = BuildConfig::QUIET;
211     return config;
212   }
213
214   BuildConfig config_;
215   VirtualFileSystem fs_;
216   Builder builder_;
217   int now_;
218
219   vector<string> commands_ran_;
220   Edge* last_command_;
221   BuildStatus status_;
222 };
223
224 void BuildTest::Dirty(const string& path) {
225   Node* node = GetNode(path);
226   node->MarkDirty();
227
228   // If it's an input file, mark that we've already stat()ed it and
229   // it's missing.
230   if (!node->in_edge())
231     node->MarkMissing();
232 }
233
234 bool BuildTest::CanRunMore() {
235   // Only run one at a time.
236   return last_command_ == NULL;
237 }
238
239 bool BuildTest::StartCommand(Edge* edge) {
240   assert(!last_command_);
241   commands_ran_.push_back(edge->EvaluateCommand());
242   if (edge->rule().name() == "cat"  ||
243       edge->rule().name() == "cat_rsp" ||
244       edge->rule().name() == "cc" ||
245       edge->rule().name() == "touch" ||
246       edge->rule().name() == "touch-interrupt") {
247     for (vector<Node*>::iterator out = edge->outputs_.begin();
248          out != edge->outputs_.end(); ++out) {
249       fs_.Create((*out)->path(), now_, "");
250     }
251   } else if (edge->rule().name() == "true" ||
252              edge->rule().name() == "fail" ||
253              edge->rule().name() == "interrupt") {
254     // Don't do anything.
255   } else {
256     printf("unknown command\n");
257     return false;
258   }
259
260   last_command_ = edge;
261   return true;
262 }
263
264 Edge* BuildTest::WaitForCommand(ExitStatus* status, string* /* output */) {
265   if (Edge* edge = last_command_) {
266     if (edge->rule().name() == "interrupt" ||
267         edge->rule().name() == "touch-interrupt") {
268       *status = ExitInterrupted;
269       return NULL;
270     }
271
272     if (edge->rule().name() == "fail")
273       *status = ExitFailure;
274     else
275       *status = ExitSuccess;
276     last_command_ = NULL;
277     return edge;
278   }
279   *status = ExitFailure;
280   return NULL;
281 }
282
283 vector<Edge*> BuildTest::GetActiveEdges() {
284   vector<Edge*> edges;
285   if (last_command_)
286     edges.push_back(last_command_);
287   return edges;
288 }
289
290 void BuildTest::Abort() {
291   last_command_ = NULL;
292 }
293
294 TEST_F(BuildTest, NoWork) {
295   string err;
296   EXPECT_TRUE(builder_.AlreadyUpToDate());
297 }
298
299 TEST_F(BuildTest, OneStep) {
300   // Given a dirty target with one ready input,
301   // we should rebuild the target.
302   Dirty("cat1");
303   string err;
304   EXPECT_TRUE(builder_.AddTarget("cat1", &err));
305   ASSERT_EQ("", err);
306   EXPECT_TRUE(builder_.Build(&err));
307   ASSERT_EQ("", err);
308
309   ASSERT_EQ(1u, commands_ran_.size());
310   EXPECT_EQ("cat in1 > cat1", commands_ran_[0]);
311 }
312
313 TEST_F(BuildTest, OneStep2) {
314   // Given a target with one dirty input,
315   // we should rebuild the target.
316   Dirty("cat1");
317   string err;
318   EXPECT_TRUE(builder_.AddTarget("cat1", &err));
319   ASSERT_EQ("", err);
320   EXPECT_TRUE(builder_.Build(&err));
321   EXPECT_EQ("", err);
322
323   ASSERT_EQ(1u, commands_ran_.size());
324   EXPECT_EQ("cat in1 > cat1", commands_ran_[0]);
325 }
326
327 TEST_F(BuildTest, TwoStep) {
328   string err;
329   EXPECT_TRUE(builder_.AddTarget("cat12", &err));
330   ASSERT_EQ("", err);
331   EXPECT_TRUE(builder_.Build(&err));
332   EXPECT_EQ("", err);
333   ASSERT_EQ(3u, commands_ran_.size());
334   // Depending on how the pointers work out, we could've ran
335   // the first two commands in either order.
336   EXPECT_TRUE((commands_ran_[0] == "cat in1 > cat1" &&
337                commands_ran_[1] == "cat in1 in2 > cat2") ||
338               (commands_ran_[1] == "cat in1 > cat1" &&
339                commands_ran_[0] == "cat in1 in2 > cat2"));
340
341   EXPECT_EQ("cat cat1 cat2 > cat12", commands_ran_[2]);
342
343   now_++;
344
345   // Modifying in2 requires rebuilding one intermediate file
346   // and the final file.
347   fs_.Create("in2", now_, "");
348   state_.Reset();
349   EXPECT_TRUE(builder_.AddTarget("cat12", &err));
350   ASSERT_EQ("", err);
351   EXPECT_TRUE(builder_.Build(&err));
352   ASSERT_EQ("", err);
353   ASSERT_EQ(5u, commands_ran_.size());
354   EXPECT_EQ("cat in1 in2 > cat2", commands_ran_[3]);
355   EXPECT_EQ("cat cat1 cat2 > cat12", commands_ran_[4]);
356 }
357
358 TEST_F(BuildTest, TwoOutputs) {
359   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
360 "rule touch\n"
361 "  command = touch $out\n"
362 "build out1 out2: touch in.txt\n"));
363
364   fs_.Create("in.txt", now_, "");
365
366   string err;
367   EXPECT_TRUE(builder_.AddTarget("out1", &err));
368   ASSERT_EQ("", err);
369   EXPECT_TRUE(builder_.Build(&err));
370   EXPECT_EQ("", err);
371   ASSERT_EQ(1u, commands_ran_.size());
372   EXPECT_EQ("touch out1 out2", commands_ran_[0]);
373 }
374
375 // Test case from
376 //   https://github.com/martine/ninja/issues/148
377 TEST_F(BuildTest, MultiOutIn) {
378   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
379 "rule touch\n"
380 "  command = touch $out\n"
381 "build in1 otherfile: touch in\n"
382 "build out: touch in | in1\n"));
383
384   fs_.Create("in", now_, "");
385   fs_.Create("in1", ++now_, "");
386
387   string err;
388   EXPECT_TRUE(builder_.AddTarget("out", &err));
389   ASSERT_EQ("", err);
390   EXPECT_TRUE(builder_.Build(&err));
391   EXPECT_EQ("", err);
392 }
393
394 TEST_F(BuildTest, Chain) {
395   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
396 "build c2: cat c1\n"
397 "build c3: cat c2\n"
398 "build c4: cat c3\n"
399 "build c5: cat c4\n"));
400
401   fs_.Create("c1", now_, "");
402
403   string err;
404   EXPECT_TRUE(builder_.AddTarget("c5", &err));
405   ASSERT_EQ("", err);
406   EXPECT_TRUE(builder_.Build(&err));
407   EXPECT_EQ("", err);
408   ASSERT_EQ(4u, commands_ran_.size());
409
410   err.clear();
411   commands_ran_.clear();
412   state_.Reset();
413   EXPECT_TRUE(builder_.AddTarget("c5", &err));
414   ASSERT_EQ("", err);
415   EXPECT_TRUE(builder_.AlreadyUpToDate());
416
417   now_++;
418
419   fs_.Create("c3", now_, "");
420   err.clear();
421   commands_ran_.clear();
422   state_.Reset();
423   EXPECT_TRUE(builder_.AddTarget("c5", &err));
424   ASSERT_EQ("", err);
425   EXPECT_FALSE(builder_.AlreadyUpToDate());
426   EXPECT_TRUE(builder_.Build(&err));
427   ASSERT_EQ(2u, commands_ran_.size());  // 3->4, 4->5
428 }
429
430 TEST_F(BuildTest, MissingInput) {
431   // Input is referenced by build file, but no rule for it.
432   string err;
433   Dirty("in1");
434   EXPECT_FALSE(builder_.AddTarget("cat1", &err));
435   EXPECT_EQ("'in1', needed by 'cat1', missing and no known rule to make it",
436             err);
437 }
438
439 TEST_F(BuildTest, MissingTarget) {
440   // Target is not referenced by build file.
441   string err;
442   EXPECT_FALSE(builder_.AddTarget("meow", &err));
443   EXPECT_EQ("unknown target: 'meow'", err);
444 }
445
446 TEST_F(BuildTest, MakeDirs) {
447   string err;
448
449 #ifdef _WIN32
450   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, "build subdir\\dir2\\file: cat in1\n"));
451   EXPECT_TRUE(builder_.AddTarget("subdir\\dir2\\file", &err));
452 #else
453   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, "build subdir/dir2/file: cat in1\n"));
454   EXPECT_TRUE(builder_.AddTarget("subdir/dir2/file", &err));
455 #endif
456
457   EXPECT_EQ("", err);
458   now_ = 0;  // Make all stat()s return file not found.
459   EXPECT_TRUE(builder_.Build(&err));
460   ASSERT_EQ("", err);
461   ASSERT_EQ(2u, fs_.directories_made_.size());
462   EXPECT_EQ("subdir", fs_.directories_made_[0]);
463 #ifdef _WIN32
464   EXPECT_EQ("subdir\\dir2", fs_.directories_made_[1]);
465 #else
466   EXPECT_EQ("subdir/dir2", fs_.directories_made_[1]);
467 #endif
468 }
469
470 TEST_F(BuildTest, DepFileMissing) {
471   string err;
472   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
473 "rule cc\n  command = cc $in\n  depfile = $out.d\n"
474 "build foo.o: cc foo.c\n"));
475   fs_.Create("foo.c", now_, "");
476
477   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
478   ASSERT_EQ("", err);
479   ASSERT_EQ(1u, fs_.files_read_.size());
480   EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
481 }
482
483 TEST_F(BuildTest, DepFileOK) {
484   string err;
485   int orig_edges = state_.edges_.size();
486   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
487 "rule cc\n  command = cc $in\n  depfile = $out.d\n"
488 "build foo.o: cc foo.c\n"));
489   Edge* edge = state_.edges_.back();
490
491   fs_.Create("foo.c", now_, "");
492   GetNode("bar.h")->MarkDirty();  // Mark bar.h as missing.
493   fs_.Create("foo.o.d", now_, "foo.o: blah.h bar.h\n");
494   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
495   ASSERT_EQ("", err);
496   ASSERT_EQ(1u, fs_.files_read_.size());
497   EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
498
499   // Expect three new edges: one generating foo.o, and two more from
500   // loading the depfile.
501   ASSERT_EQ(orig_edges + 3, (int)state_.edges_.size());
502   // Expect our edge to now have three inputs: foo.c and two headers.
503   ASSERT_EQ(3u, edge->inputs_.size());
504
505   // Expect the command line we generate to only use the original input.
506   ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
507 }
508
509 TEST_F(BuildTest, DepFileParseError) {
510   string err;
511   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
512 "rule cc\n  command = cc $in\n  depfile = $out.d\n"
513 "build foo.o: cc foo.c\n"));
514   fs_.Create("foo.c", now_, "");
515   fs_.Create("foo.o.d", now_, "randomtext\n");
516   EXPECT_FALSE(builder_.AddTarget("foo.o", &err));
517   EXPECT_EQ("expected depfile 'foo.o.d' to mention 'foo.o', got 'randomtext'",
518             err);
519 }
520
521 TEST_F(BuildTest, OrderOnlyDeps) {
522   string err;
523   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
524 "rule cc\n  command = cc $in\n  depfile = $out.d\n"
525 "build foo.o: cc foo.c || otherfile\n"));
526   Edge* edge = state_.edges_.back();
527
528   fs_.Create("foo.c", now_, "");
529   fs_.Create("otherfile", now_, "");
530   fs_.Create("foo.o.d", now_, "foo.o: blah.h bar.h\n");
531   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
532   ASSERT_EQ("", err);
533
534   // One explicit, two implicit, one order only.
535   ASSERT_EQ(4u, edge->inputs_.size());
536   EXPECT_EQ(2, edge->implicit_deps_);
537   EXPECT_EQ(1, edge->order_only_deps_);
538   // Verify the inputs are in the order we expect
539   // (explicit then implicit then orderonly).
540   EXPECT_EQ("foo.c", edge->inputs_[0]->path());
541   EXPECT_EQ("blah.h", edge->inputs_[1]->path());
542   EXPECT_EQ("bar.h", edge->inputs_[2]->path());
543   EXPECT_EQ("otherfile", edge->inputs_[3]->path());
544
545   // Expect the command line we generate to only use the original input.
546   ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
547
548   // explicit dep dirty, expect a rebuild.
549   EXPECT_TRUE(builder_.Build(&err));
550   ASSERT_EQ("", err);
551   ASSERT_EQ(1u, commands_ran_.size());
552
553   now_++;
554
555   // implicit dep dirty, expect a rebuild.
556   fs_.Create("blah.h", now_, "");
557   fs_.Create("bar.h", now_, "");
558   commands_ran_.clear();
559   state_.Reset();
560   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
561   EXPECT_TRUE(builder_.Build(&err));
562   ASSERT_EQ("", err);
563   ASSERT_EQ(1u, commands_ran_.size());
564
565   now_++;
566
567   // order only dep dirty, no rebuild.
568   fs_.Create("otherfile", now_, "");
569   commands_ran_.clear();
570   state_.Reset();
571   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
572   EXPECT_EQ("", err);
573   EXPECT_TRUE(builder_.AlreadyUpToDate());
574
575   // implicit dep missing, expect rebuild.
576   fs_.RemoveFile("bar.h");
577   commands_ran_.clear();
578   state_.Reset();
579   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
580   EXPECT_TRUE(builder_.Build(&err));
581   ASSERT_EQ("", err);
582   ASSERT_EQ(1u, commands_ran_.size());
583 }
584
585 TEST_F(BuildTest, RebuildOrderOnlyDeps) {
586   string err;
587   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
588 "rule cc\n  command = cc $in\n"
589 "rule true\n  command = true\n"
590 "build oo.h: cc oo.h.in\n"
591 "build foo.o: cc foo.c || oo.h\n"));
592
593   fs_.Create("foo.c", now_, "");
594   fs_.Create("oo.h.in", now_, "");
595
596   // foo.o and order-only dep dirty, build both.
597   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
598   EXPECT_TRUE(builder_.Build(&err));
599   ASSERT_EQ("", err);
600   ASSERT_EQ(2u, commands_ran_.size());
601
602   // all clean, no rebuild.
603   commands_ran_.clear();
604   state_.Reset();
605   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
606   EXPECT_EQ("", err);
607   EXPECT_TRUE(builder_.AlreadyUpToDate());
608
609   // order-only dep missing, build it only.
610   fs_.RemoveFile("oo.h");
611   commands_ran_.clear();
612   state_.Reset();
613   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
614   EXPECT_TRUE(builder_.Build(&err));
615   ASSERT_EQ("", err);
616   ASSERT_EQ(1u, commands_ran_.size());
617   ASSERT_EQ("cc oo.h.in", commands_ran_[0]);
618
619   now_++;
620
621   // order-only dep dirty, build it only.
622   fs_.Create("oo.h.in", now_, "");
623   commands_ran_.clear();
624   state_.Reset();
625   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
626   EXPECT_TRUE(builder_.Build(&err));
627   ASSERT_EQ("", err);
628   ASSERT_EQ(1u, commands_ran_.size());
629   ASSERT_EQ("cc oo.h.in", commands_ran_[0]);
630 }
631
632 TEST_F(BuildTest, Phony) {
633   string err;
634   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
635 "build out: cat bar.cc\n"
636 "build all: phony out\n"));
637   fs_.Create("bar.cc", now_, "");
638
639   EXPECT_TRUE(builder_.AddTarget("all", &err));
640   ASSERT_EQ("", err);
641
642   // Only one command to run, because phony runs no command.
643   EXPECT_FALSE(builder_.AlreadyUpToDate());
644   EXPECT_TRUE(builder_.Build(&err));
645   ASSERT_EQ("", err);
646   ASSERT_EQ(1u, commands_ran_.size());
647 }
648
649 TEST_F(BuildTest, PhonyNoWork) {
650   string err;
651   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
652 "build out: cat bar.cc\n"
653 "build all: phony out\n"));
654   fs_.Create("bar.cc", now_, "");
655   fs_.Create("out", now_, "");
656
657   EXPECT_TRUE(builder_.AddTarget("all", &err));
658   ASSERT_EQ("", err);
659   EXPECT_TRUE(builder_.AlreadyUpToDate());
660 }
661
662 TEST_F(BuildTest, Fail) {
663   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
664 "rule fail\n"
665 "  command = fail\n"
666 "build out1: fail\n"));
667
668   string err;
669   EXPECT_TRUE(builder_.AddTarget("out1", &err));
670   ASSERT_EQ("", err);
671
672   EXPECT_FALSE(builder_.Build(&err));
673   ASSERT_EQ(1u, commands_ran_.size());
674   ASSERT_EQ("subcommand failed", err);
675 }
676
677 TEST_F(BuildTest, SwallowFailures) {
678   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
679 "rule fail\n"
680 "  command = fail\n"
681 "build out1: fail\n"
682 "build out2: fail\n"
683 "build out3: fail\n"
684 "build all: phony out1 out2 out3\n"));
685
686   // Swallow two failures, die on the third.
687   config_.failures_allowed = 3;
688
689   string err;
690   EXPECT_TRUE(builder_.AddTarget("all", &err));
691   ASSERT_EQ("", err);
692
693   EXPECT_FALSE(builder_.Build(&err));
694   ASSERT_EQ(3u, commands_ran_.size());
695   ASSERT_EQ("subcommands failed", err);
696 }
697
698 TEST_F(BuildTest, SwallowFailuresLimit) {
699   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
700 "rule fail\n"
701 "  command = fail\n"
702 "build out1: fail\n"
703 "build out2: fail\n"
704 "build out3: fail\n"
705 "build final: cat out1 out2 out3\n"));
706
707   // Swallow ten failures; we should stop before building final.
708   config_.failures_allowed = 11;
709
710   string err;
711   EXPECT_TRUE(builder_.AddTarget("final", &err));
712   ASSERT_EQ("", err);
713
714   EXPECT_FALSE(builder_.Build(&err));
715   ASSERT_EQ(3u, commands_ran_.size());
716   ASSERT_EQ("cannot make progress due to previous errors", err);
717 }
718
719 struct BuildWithLogTest : public BuildTest {
720   BuildWithLogTest() {
721     builder_.SetBuildLog(&build_log_);
722   }
723
724   BuildLog build_log_;
725 };
726
727 TEST_F(BuildWithLogTest, NotInLogButOnDisk) {
728   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
729 "rule cc\n"
730 "  command = cc\n"
731 "build out1: cc in\n"));
732
733   // Create input/output that would be considered up to date when
734   // not considering the command line hash.
735   fs_.Create("in", now_, "");
736   fs_.Create("out1", now_, "");
737   string err;
738
739   // Because it's not in the log, it should not be up-to-date until
740   // we build again.
741   EXPECT_TRUE(builder_.AddTarget("out1", &err));
742   EXPECT_FALSE(builder_.AlreadyUpToDate());
743
744   commands_ran_.clear();
745   state_.Reset();
746
747   EXPECT_TRUE(builder_.AddTarget("out1", &err));
748   EXPECT_TRUE(builder_.Build(&err));
749   EXPECT_TRUE(builder_.AlreadyUpToDate());
750 }
751
752 TEST_F(BuildWithLogTest, RestatTest) {
753   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
754 "rule true\n"
755 "  command = true\n"
756 "  restat = 1\n"
757 "rule cc\n"
758 "  command = cc\n"
759 "  restat = 1\n"
760 "build out1: cc in\n"
761 "build out2: true out1\n"
762 "build out3: cat out2\n"));
763
764   fs_.Create("out1", now_, "");
765   fs_.Create("out2", now_, "");
766   fs_.Create("out3", now_, "");
767
768   now_++;
769
770   fs_.Create("in", now_, "");
771
772   // Do a pre-build so that there's commands in the log for the outputs,
773   // otherwise, the lack of an entry in the build log will cause out3 to rebuild
774   // regardless of restat.
775   string err;
776   EXPECT_TRUE(builder_.AddTarget("out3", &err));
777   ASSERT_EQ("", err);
778   EXPECT_TRUE(builder_.Build(&err));
779   ASSERT_EQ("", err);
780   commands_ran_.clear();
781   state_.Reset();
782
783   now_++;
784
785   fs_.Create("in", now_, "");
786   // "cc" touches out1, so we should build out2.  But because "true" does not
787   // touch out2, we should cancel the build of out3.
788   EXPECT_TRUE(builder_.AddTarget("out3", &err));
789   ASSERT_EQ("", err);
790   EXPECT_TRUE(builder_.Build(&err));
791   ASSERT_EQ(2u, commands_ran_.size());
792
793   // If we run again, it should be a no-op, because the build log has recorded
794   // that we've already built out2 with an input timestamp of 2 (from out1).
795   commands_ran_.clear();
796   state_.Reset();
797   EXPECT_TRUE(builder_.AddTarget("out3", &err));
798   ASSERT_EQ("", err);
799   EXPECT_TRUE(builder_.AlreadyUpToDate());
800
801   now_++;
802
803   fs_.Create("in", now_, "");
804
805   // The build log entry should not, however, prevent us from rebuilding out2
806   // if out1 changes.
807   commands_ran_.clear();
808   state_.Reset();
809   EXPECT_TRUE(builder_.AddTarget("out3", &err));
810   ASSERT_EQ("", err);
811   EXPECT_TRUE(builder_.Build(&err));
812   ASSERT_EQ(2u, commands_ran_.size());
813 }
814
815 TEST_F(BuildWithLogTest, RestatMissingFile) {
816   // If a restat rule doesn't create its output, and the output didn't
817   // exist before the rule was run, consider that behavior equivalent
818   // to a rule that doesn't modify its existent output file.
819
820   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
821 "rule true\n"
822 "  command = true\n"
823 "  restat = 1\n"
824 "rule cc\n"
825 "  command = cc\n"
826 "build out1: true in\n"
827 "build out2: cc out1\n"));
828
829   fs_.Create("in", now_, "");
830   fs_.Create("out2", now_, "");
831
832   // Do a pre-build so that there's commands in the log for the outputs,
833   // otherwise, the lack of an entry in the build log will cause out2 to rebuild
834   // regardless of restat.
835   string err;
836   EXPECT_TRUE(builder_.AddTarget("out2", &err));
837   ASSERT_EQ("", err);
838   EXPECT_TRUE(builder_.Build(&err));
839   ASSERT_EQ("", err);
840   commands_ran_.clear();
841   state_.Reset();
842
843   now_++;
844   fs_.Create("in", now_, "");
845   fs_.Create("out2", now_, "");
846
847   // Run a build, expect only the first command to run.
848   // It doesn't touch its output (due to being the "true" command), so
849   // we shouldn't run the dependent build.
850   EXPECT_TRUE(builder_.AddTarget("out2", &err));
851   ASSERT_EQ("", err);
852   EXPECT_TRUE(builder_.Build(&err));
853   ASSERT_EQ(1u, commands_ran_.size());
854 }
855
856 // Test scenario, in which an input file is removed, but output isn't changed
857 // https://github.com/martine/ninja/issues/295
858 TEST_F(BuildWithLogTest, RestatMissingInput) {
859   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
860     "rule true\n"
861     "  command = true\n"
862     "  depfile = $out.d\n"
863     "  restat = 1\n"
864     "rule cc\n"
865     "  command = cc\n"
866     "build out1: true in\n"
867     "build out2: cc out1\n"));
868
869   // Create all necessary files
870   fs_.Create("in", now_, "");
871
872   // The implicit dependencies and the depfile itself 
873   // are newer than the output
874   TimeStamp restat_mtime = ++now_;
875   fs_.Create("out1.d", now_, "out1: will.be.deleted restat.file\n");
876   fs_.Create("will.be.deleted", now_, "");
877   fs_.Create("restat.file", now_, "");
878
879   // Run the build, out1 and out2 get built
880   string err;
881   EXPECT_TRUE(builder_.AddTarget("out2", &err));
882   ASSERT_EQ("", err);
883   EXPECT_TRUE(builder_.Build(&err));
884   ASSERT_EQ(2u, commands_ran_.size());
885
886   // See that an entry in the logfile is created, capturing
887   // the right mtime
888   BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out1");
889   ASSERT_TRUE(NULL != log_entry);
890   ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
891
892   // Now remove a file, referenced from depfile, so that target becomes 
893   // dirty, but the output does not change
894   fs_.RemoveFile("will.be.deleted");
895   
896   // Trigger the build again - only out1 gets built
897   commands_ran_.clear();
898   state_.Reset();
899   EXPECT_TRUE(builder_.AddTarget("out2", &err));
900   ASSERT_EQ("", err);
901   EXPECT_TRUE(builder_.Build(&err));
902   ASSERT_EQ(1u, commands_ran_.size());
903
904   // Check that the logfile entry remains correctly set
905   log_entry = build_log_.LookupByOutput("out1");
906   ASSERT_TRUE(NULL != log_entry);
907   ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
908 }
909
910 struct BuildDryRun : public BuildWithLogTest {
911   BuildDryRun() {
912     config_.dry_run = true;
913   }
914 };
915
916 TEST_F(BuildDryRun, AllCommandsShown) {
917   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
918 "rule true\n"
919 "  command = true\n"
920 "  restat = 1\n"
921 "rule cc\n"
922 "  command = cc\n"
923 "  restat = 1\n"
924 "build out1: cc in\n"
925 "build out2: true out1\n"
926 "build out3: cat out2\n"));
927
928   fs_.Create("out1", now_, "");
929   fs_.Create("out2", now_, "");
930   fs_.Create("out3", now_, "");
931
932   now_++;
933
934   fs_.Create("in", now_, "");
935
936   // "cc" touches out1, so we should build out2.  But because "true" does not
937   // touch out2, we should cancel the build of out3.
938   string err;
939   EXPECT_TRUE(builder_.AddTarget("out3", &err));
940   ASSERT_EQ("", err);
941   EXPECT_TRUE(builder_.Build(&err));
942   ASSERT_EQ(3u, commands_ran_.size());
943 }
944
945 // Test that RSP files are created when & where appropriate and deleted after
946 // successful execution.
947 TEST_F(BuildTest, RspFileSuccess)
948 {
949   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
950     "rule cat_rsp\n"
951     "  command = cat $rspfile > $out\n"
952     "  rspfile = $rspfile\n"
953     "  rspfile_content = $long_command\n"
954     "build out1: cat in\n"
955     "build out2: cat_rsp in\n"
956     "  rspfile = out2.rsp\n"
957     "  long_command = Some very long command\n"));
958
959   fs_.Create("out1", now_, "");
960   fs_.Create("out2", now_, "");
961   fs_.Create("out3", now_, "");
962
963   now_++;
964
965   fs_.Create("in", now_, "");
966
967   string err;
968   EXPECT_TRUE(builder_.AddTarget("out1", &err));
969   ASSERT_EQ("", err);
970   EXPECT_TRUE(builder_.AddTarget("out2", &err));
971   ASSERT_EQ("", err);
972
973   size_t files_created = fs_.files_created_.size();
974   size_t files_removed = fs_.files_removed_.size();
975
976   EXPECT_TRUE(builder_.Build(&err));
977   ASSERT_EQ(2u, commands_ran_.size()); // cat + cat_rsp
978
979   // The RSP file was created
980   ASSERT_EQ(files_created + 1, fs_.files_created_.size());
981   ASSERT_EQ(1u, fs_.files_created_.count("out2.rsp"));
982
983   // The RSP file was removed
984   ASSERT_EQ(files_removed + 1, fs_.files_removed_.size());
985   ASSERT_EQ(1u, fs_.files_removed_.count("out2.rsp"));
986 }
987
988 // Test that RSP file is created but not removed for commands, which fail
989 TEST_F(BuildTest, RspFileFailure) {
990   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
991     "rule fail\n"
992     "  command = fail\n"
993     "  rspfile = $rspfile\n"
994     "  rspfile_content = $long_command\n"
995     "build out: fail in\n"
996     "  rspfile = out.rsp\n"
997     "  long_command = Another very long command\n"));
998
999   fs_.Create("out", now_, "");
1000   now_++;
1001   fs_.Create("in", now_, "");
1002
1003   string err;
1004   EXPECT_TRUE(builder_.AddTarget("out", &err));
1005   ASSERT_EQ("", err);
1006
1007   size_t files_created = fs_.files_created_.size();
1008   size_t files_removed = fs_.files_removed_.size();
1009
1010   EXPECT_FALSE(builder_.Build(&err));
1011   ASSERT_EQ("subcommand failed", err);
1012   ASSERT_EQ(1u, commands_ran_.size());
1013
1014   // The RSP file was created
1015   ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1016   ASSERT_EQ(1u, fs_.files_created_.count("out.rsp"));
1017
1018   // The RSP file was NOT removed
1019   ASSERT_EQ(files_removed, fs_.files_removed_.size());
1020   ASSERT_EQ(0u, fs_.files_removed_.count("out.rsp"));
1021
1022   // The RSP file contains what it should
1023   ASSERT_EQ("Another very long command", fs_.files_["out.rsp"].contents);
1024 }
1025
1026 // Test that contens of the RSP file behaves like a regular part of
1027 // command line, i.e. triggers a rebuild if changed
1028 TEST_F(BuildWithLogTest, RspFileCmdLineChange) {
1029   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1030     "rule cat_rsp\n"
1031     "  command = cat $rspfile > $out\n"
1032     "  rspfile = $rspfile\n"
1033     "  rspfile_content = $long_command\n"
1034     "build out: cat_rsp in\n"
1035     "  rspfile = out.rsp\n"
1036     "  long_command = Original very long command\n"));
1037
1038   fs_.Create("out", now_, "");
1039   now_++;
1040   fs_.Create("in", now_, "");
1041
1042   string err;
1043   EXPECT_TRUE(builder_.AddTarget("out", &err));
1044   ASSERT_EQ("", err);
1045
1046   // 1. Build for the 1st time (-> populate log)
1047   EXPECT_TRUE(builder_.Build(&err));
1048   ASSERT_EQ(1u, commands_ran_.size());
1049
1050   // 2. Build again (no change)
1051   commands_ran_.clear();
1052   state_.Reset();
1053   EXPECT_TRUE(builder_.AddTarget("out", &err));
1054   EXPECT_EQ("", err);
1055   ASSERT_TRUE(builder_.AlreadyUpToDate());
1056
1057   // 3. Alter the entry in the logfile
1058   // (to simulate a change in the command line between 2 builds)
1059   BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out");
1060   ASSERT_TRUE(NULL != log_entry);
1061   ASSERT_NO_FATAL_FAILURE(AssertHash(
1062         "cat out.rsp > out;rspfile=Original very long command",
1063         log_entry->command_hash));
1064   log_entry->command_hash++;  // Change the command hash to something else.
1065   // Now expect the target to be rebuilt
1066   commands_ran_.clear();
1067   state_.Reset();
1068   EXPECT_TRUE(builder_.AddTarget("out", &err));
1069   EXPECT_EQ("", err);
1070   EXPECT_TRUE(builder_.Build(&err));
1071   EXPECT_EQ(1u, commands_ran_.size());
1072 }
1073
1074 TEST_F(BuildTest, InterruptCleanup) {
1075   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1076 "rule interrupt\n"
1077 "  command = interrupt\n"
1078 "rule touch-interrupt\n"
1079 "  command = touch-interrupt\n"
1080 "build out1: interrupt in1\n"
1081 "build out2: touch-interrupt in2\n"));
1082
1083   fs_.Create("out1", now_, "");
1084   fs_.Create("out2", now_, "");
1085   now_++;
1086   fs_.Create("in1", now_, "");
1087   fs_.Create("in2", now_, "");
1088
1089   // An untouched output of an interrupted command should be retained.
1090   string err;
1091   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1092   EXPECT_EQ("", err);
1093   EXPECT_FALSE(builder_.Build(&err));
1094   EXPECT_EQ("interrupted by user", err);
1095   builder_.Cleanup();
1096   EXPECT_EQ(now_-1, fs_.Stat("out1"));
1097   err = "";
1098
1099   // A touched output of an interrupted command should be deleted.
1100   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1101   EXPECT_EQ("", err);
1102   EXPECT_FALSE(builder_.Build(&err));
1103   EXPECT_EQ("interrupted by user", err);
1104   builder_.Cleanup();
1105   EXPECT_EQ(0, fs_.Stat("out2"));
1106 }
1107
1108 TEST_F(BuildTest, PhonyWithNoInputs) {
1109   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1110 "build nonexistent: phony\n"
1111 "build out1: cat || nonexistent\n"
1112 "build out2: cat nonexistent\n"));
1113   fs_.Create("out1", now_, "");
1114   fs_.Create("out2", now_, "");
1115
1116   // out1 should be up to date even though its input is dirty, because its
1117   // order-only dependency has nothing to do.
1118   string err;
1119   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1120   ASSERT_EQ("", err);
1121   EXPECT_TRUE(builder_.AlreadyUpToDate());
1122
1123   // out2 should still be out of date though, because its input is dirty.
1124   err.clear();
1125   commands_ran_.clear();
1126   state_.Reset();
1127   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1128   ASSERT_EQ("", err);
1129   EXPECT_TRUE(builder_.Build(&err));
1130   EXPECT_EQ("", err);
1131   ASSERT_EQ(1u, commands_ran_.size());
1132 }
1133
1134 TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
1135   EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
1136             status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]"));
1137 }