Merge pull request #401 from syntheticpp/win-network-path
[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, NotInLogButOnDisk) {
727   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
728 "rule cc\n"
729 "  command = cc\n"
730 "build out1: cc in\n"));
731
732   // Create input/output that would be considered up to date when
733   // not considering the command line hash.
734   fs_.Create("in", now_, "");
735   fs_.Create("out1", now_, "");
736   string err;
737
738   // Because it's not in the log, it should not be up-to-date until
739   // we build again.
740   EXPECT_TRUE(builder_.AddTarget("out1", &err));
741   EXPECT_FALSE(builder_.AlreadyUpToDate());
742
743   commands_ran_.clear();
744   state_.Reset();
745
746   EXPECT_TRUE(builder_.AddTarget("out1", &err));
747   EXPECT_TRUE(builder_.Build(&err));
748   EXPECT_TRUE(builder_.AlreadyUpToDate());
749 }
750
751 TEST_F(BuildWithLogTest, RestatTest) {
752   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
753 "rule true\n"
754 "  command = true\n"
755 "  restat = 1\n"
756 "rule cc\n"
757 "  command = cc\n"
758 "  restat = 1\n"
759 "build out1: cc in\n"
760 "build out2: true out1\n"
761 "build out3: cat out2\n"));
762
763   fs_.Create("out1", now_, "");
764   fs_.Create("out2", now_, "");
765   fs_.Create("out3", now_, "");
766
767   now_++;
768
769   fs_.Create("in", now_, "");
770
771   // Do a pre-build so that there's commands in the log for the outputs,
772   // otherwise, the lack of an entry in the build log will cause out3 to rebuild
773   // regardless of restat.
774   string err;
775   EXPECT_TRUE(builder_.AddTarget("out3", &err));
776   ASSERT_EQ("", err);
777   EXPECT_TRUE(builder_.Build(&err));
778   ASSERT_EQ("", err);
779   commands_ran_.clear();
780   state_.Reset();
781
782   now_++;
783
784   fs_.Create("in", now_, "");
785   // "cc" touches out1, so we should build out2.  But because "true" does not
786   // touch out2, we should cancel the build of out3.
787   EXPECT_TRUE(builder_.AddTarget("out3", &err));
788   ASSERT_EQ("", err);
789   EXPECT_TRUE(builder_.Build(&err));
790   ASSERT_EQ(2u, commands_ran_.size());
791
792   // If we run again, it should be a no-op, because the build log has recorded
793   // that we've already built out2 with an input timestamp of 2 (from out1).
794   commands_ran_.clear();
795   state_.Reset();
796   EXPECT_TRUE(builder_.AddTarget("out3", &err));
797   ASSERT_EQ("", err);
798   EXPECT_TRUE(builder_.AlreadyUpToDate());
799
800   now_++;
801
802   fs_.Create("in", now_, "");
803
804   // The build log entry should not, however, prevent us from rebuilding out2
805   // if out1 changes.
806   commands_ran_.clear();
807   state_.Reset();
808   EXPECT_TRUE(builder_.AddTarget("out3", &err));
809   ASSERT_EQ("", err);
810   EXPECT_TRUE(builder_.Build(&err));
811   ASSERT_EQ(2u, commands_ran_.size());
812 }
813
814 TEST_F(BuildWithLogTest, RestatMissingFile) {
815   // If a restat rule doesn't create its output, and the output didn't
816   // exist before the rule was run, consider that behavior equivalent
817   // to a rule that doesn't modify its existent output file.
818
819   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
820 "rule true\n"
821 "  command = true\n"
822 "  restat = 1\n"
823 "rule cc\n"
824 "  command = cc\n"
825 "build out1: true in\n"
826 "build out2: cc out1\n"));
827
828   fs_.Create("in", now_, "");
829   fs_.Create("out2", now_, "");
830
831   // Do a pre-build so that there's commands in the log for the outputs,
832   // otherwise, the lack of an entry in the build log will cause out2 to rebuild
833   // regardless of restat.
834   string err;
835   EXPECT_TRUE(builder_.AddTarget("out2", &err));
836   ASSERT_EQ("", err);
837   EXPECT_TRUE(builder_.Build(&err));
838   ASSERT_EQ("", err);
839   commands_ran_.clear();
840   state_.Reset();
841
842   now_++;
843   fs_.Create("in", now_, "");
844   fs_.Create("out2", now_, "");
845
846   // Run a build, expect only the first command to run.
847   // It doesn't touch its output (due to being the "true" command), so
848   // we shouldn't run the dependent build.
849   EXPECT_TRUE(builder_.AddTarget("out2", &err));
850   ASSERT_EQ("", err);
851   EXPECT_TRUE(builder_.Build(&err));
852   ASSERT_EQ(1u, commands_ran_.size());
853 }
854
855 // Test scenario, in which an input file is removed, but output isn't changed
856 // https://github.com/martine/ninja/issues/295
857 TEST_F(BuildWithLogTest, RestatMissingInput) {
858   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
859     "rule true\n"
860     "  command = true\n"
861     "  depfile = $out.d\n"
862     "  restat = 1\n"
863     "rule cc\n"
864     "  command = cc\n"
865     "build out1: true in\n"
866     "build out2: cc out1\n"));
867
868   // Create all necessary files
869   fs_.Create("in", now_, "");
870
871   // The implicit dependencies and the depfile itself 
872   // are newer than the output
873   TimeStamp restat_mtime = ++now_;
874   fs_.Create("out1.d", now_, "out1: will.be.deleted restat.file\n");
875   fs_.Create("will.be.deleted", now_, "");
876   fs_.Create("restat.file", now_, "");
877
878   // Run the build, out1 and out2 get built
879   string err;
880   EXPECT_TRUE(builder_.AddTarget("out2", &err));
881   ASSERT_EQ("", err);
882   EXPECT_TRUE(builder_.Build(&err));
883   ASSERT_EQ(2u, commands_ran_.size());
884
885   // See that an entry in the logfile is created, capturing
886   // the right mtime
887   BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out1");
888   ASSERT_TRUE(NULL != log_entry);
889   ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
890
891   // Now remove a file, referenced from depfile, so that target becomes 
892   // dirty, but the output does not change
893   fs_.RemoveFile("will.be.deleted");
894   
895   // Trigger the build again - only out1 gets built
896   commands_ran_.clear();
897   state_.Reset();
898   EXPECT_TRUE(builder_.AddTarget("out2", &err));
899   ASSERT_EQ("", err);
900   EXPECT_TRUE(builder_.Build(&err));
901   ASSERT_EQ(1u, commands_ran_.size());
902
903   // Check that the logfile entry remains correctly set
904   log_entry = build_log_.LookupByOutput("out1");
905   ASSERT_TRUE(NULL != log_entry);
906   ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
907 }
908
909 struct BuildDryRun : public BuildWithLogTest {
910   BuildDryRun() {
911     config_.dry_run = true;
912   }
913 };
914
915 TEST_F(BuildDryRun, AllCommandsShown) {
916   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
917 "rule true\n"
918 "  command = true\n"
919 "  restat = 1\n"
920 "rule cc\n"
921 "  command = cc\n"
922 "  restat = 1\n"
923 "build out1: cc in\n"
924 "build out2: true out1\n"
925 "build out3: cat out2\n"));
926
927   fs_.Create("out1", now_, "");
928   fs_.Create("out2", now_, "");
929   fs_.Create("out3", now_, "");
930
931   now_++;
932
933   fs_.Create("in", now_, "");
934
935   // "cc" touches out1, so we should build out2.  But because "true" does not
936   // touch out2, we should cancel the build of out3.
937   string err;
938   EXPECT_TRUE(builder_.AddTarget("out3", &err));
939   ASSERT_EQ("", err);
940   EXPECT_TRUE(builder_.Build(&err));
941   ASSERT_EQ(3u, commands_ran_.size());
942 }
943
944 // Test that RSP files are created when & where appropriate and deleted after
945 // succesful execution.
946 TEST_F(BuildTest, RspFileSuccess)
947 {
948   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
949     "rule cat_rsp\n"
950     "  command = cat $rspfile > $out\n"
951     "  rspfile = $rspfile\n"
952     "  rspfile_content = $long_command\n"
953     "build out1: cat in\n"
954     "build out2: cat_rsp in\n"
955     "  rspfile = out2.rsp\n"
956     "  long_command = Some very long command\n"));
957
958   fs_.Create("out1", now_, "");
959   fs_.Create("out2", now_, "");
960   fs_.Create("out3", now_, "");
961
962   now_++;
963
964   fs_.Create("in", now_, "");
965
966   string err;
967   EXPECT_TRUE(builder_.AddTarget("out1", &err));
968   ASSERT_EQ("", err);
969   EXPECT_TRUE(builder_.AddTarget("out2", &err));
970   ASSERT_EQ("", err);
971
972   size_t files_created = fs_.files_created_.size();
973   size_t files_removed = fs_.files_removed_.size();
974
975   EXPECT_TRUE(builder_.Build(&err));
976   ASSERT_EQ(2u, commands_ran_.size()); // cat + cat_rsp
977
978   // The RSP file was created
979   ASSERT_EQ(files_created + 1, fs_.files_created_.size());
980   ASSERT_EQ(1u, fs_.files_created_.count("out2.rsp"));
981
982   // The RSP file was removed
983   ASSERT_EQ(files_removed + 1, fs_.files_removed_.size());
984   ASSERT_EQ(1u, fs_.files_removed_.count("out2.rsp"));
985 }
986
987 // Test that RSP file is created but not removed for commands, which fail
988 TEST_F(BuildTest, RspFileFailure) {
989   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
990     "rule fail\n"
991     "  command = fail\n"
992     "  rspfile = $rspfile\n"
993     "  rspfile_content = $long_command\n"
994     "build out: fail in\n"
995     "  rspfile = out.rsp\n"
996     "  long_command = Another very long command\n"));
997
998   fs_.Create("out", now_, "");
999   now_++;
1000   fs_.Create("in", now_, "");
1001
1002   string err;
1003   EXPECT_TRUE(builder_.AddTarget("out", &err));
1004   ASSERT_EQ("", err);
1005
1006   size_t files_created = fs_.files_created_.size();
1007   size_t files_removed = fs_.files_removed_.size();
1008
1009   EXPECT_FALSE(builder_.Build(&err));
1010   ASSERT_EQ("subcommand failed", err);
1011   ASSERT_EQ(1u, commands_ran_.size());
1012
1013   // The RSP file was created
1014   ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1015   ASSERT_EQ(1u, fs_.files_created_.count("out.rsp"));
1016
1017   // The RSP file was NOT removed
1018   ASSERT_EQ(files_removed, fs_.files_removed_.size());
1019   ASSERT_EQ(0u, fs_.files_removed_.count("out.rsp"));
1020
1021   // The RSP file contains what it should
1022   ASSERT_EQ("Another very long command", fs_.files_["out.rsp"].contents);
1023 }
1024
1025 // Test that contens of the RSP file behaves like a regular part of
1026 // command line, i.e. triggers a rebuild if changed
1027 TEST_F(BuildWithLogTest, RspFileCmdLineChange) {
1028   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1029     "rule cat_rsp\n"
1030     "  command = cat $rspfile > $out\n"
1031     "  rspfile = $rspfile\n"
1032     "  rspfile_content = $long_command\n"
1033     "build out: cat_rsp in\n"
1034     "  rspfile = out.rsp\n"
1035     "  long_command = Original very long command\n"));
1036
1037   fs_.Create("out", now_, "");
1038   now_++;
1039   fs_.Create("in", now_, "");
1040
1041   string err;
1042   EXPECT_TRUE(builder_.AddTarget("out", &err));
1043   ASSERT_EQ("", err);
1044
1045   // 1. Build for the 1st time (-> populate log)
1046   EXPECT_TRUE(builder_.Build(&err));
1047   ASSERT_EQ(1u, commands_ran_.size());
1048
1049   // 2. Build again (no change)
1050   commands_ran_.clear();
1051   state_.Reset();
1052   EXPECT_TRUE(builder_.AddTarget("out", &err));
1053   EXPECT_EQ("", err);
1054   ASSERT_TRUE(builder_.AlreadyUpToDate());
1055
1056   // 3. Alter the entry in the logfile
1057   // (to simulate a change in the command line between 2 builds)
1058   BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out");
1059   ASSERT_TRUE(NULL != log_entry);
1060   ASSERT_NO_FATAL_FAILURE(AssertHash(
1061         "cat out.rsp > out;rspfile=Original very long command",
1062         log_entry->command_hash));
1063   log_entry->command_hash++;  // Change the command hash to something else.
1064   // Now expect the target to be rebuilt
1065   commands_ran_.clear();
1066   state_.Reset();
1067   EXPECT_TRUE(builder_.AddTarget("out", &err));
1068   EXPECT_EQ("", err);
1069   EXPECT_TRUE(builder_.Build(&err));
1070   EXPECT_EQ(1u, commands_ran_.size());
1071 }
1072
1073 TEST_F(BuildTest, InterruptCleanup) {
1074   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1075 "rule interrupt\n"
1076 "  command = interrupt\n"
1077 "rule touch-interrupt\n"
1078 "  command = touch-interrupt\n"
1079 "build out1: interrupt in1\n"
1080 "build out2: touch-interrupt in2\n"));
1081
1082   fs_.Create("out1", now_, "");
1083   fs_.Create("out2", now_, "");
1084   now_++;
1085   fs_.Create("in1", now_, "");
1086   fs_.Create("in2", now_, "");
1087
1088   // An untouched output of an interrupted command should be retained.
1089   string err;
1090   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1091   EXPECT_EQ("", err);
1092   EXPECT_FALSE(builder_.Build(&err));
1093   EXPECT_EQ("interrupted by user", err);
1094   builder_.Cleanup();
1095   EXPECT_EQ(now_-1, fs_.Stat("out1"));
1096   err = "";
1097
1098   // A touched output of an interrupted command should be deleted.
1099   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1100   EXPECT_EQ("", err);
1101   EXPECT_FALSE(builder_.Build(&err));
1102   EXPECT_EQ("interrupted by user", err);
1103   builder_.Cleanup();
1104   EXPECT_EQ(0, fs_.Stat("out2"));
1105 }
1106
1107 TEST_F(BuildTest, PhonyWithNoInputs) {
1108   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1109 "build nonexistent: phony\n"
1110 "build out1: cat || nonexistent\n"
1111 "build out2: cat nonexistent\n"));
1112   fs_.Create("out1", now_, "");
1113   fs_.Create("out2", now_, "");
1114
1115   // out1 should be up to date even though its input is dirty, because its
1116   // order-only dependency has nothing to do.
1117   string err;
1118   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1119   ASSERT_EQ("", err);
1120   EXPECT_TRUE(builder_.AlreadyUpToDate());
1121
1122   // out2 should still be out of date though, because its input is dirty.
1123   err.clear();
1124   commands_ran_.clear();
1125   state_.Reset();
1126   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1127   ASSERT_EQ("", err);
1128   EXPECT_TRUE(builder_.Build(&err));
1129   EXPECT_EQ("", err);
1130   ASSERT_EQ(1u, commands_ran_.size());
1131 }
1132
1133 TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
1134   EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
1135             status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]"));
1136 }