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