forked from ninja-build/ninja
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_test.cc
4383 lines (3703 loc) · 132 KB
/
build_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "build.h"
#include <assert.h>
#include <climits>
#include <stdint.h>
#include "build_log.h"
#include "deps_log.h"
#include "graph.h"
#include "status_printer.h"
#include "test.h"
using namespace std;
struct CompareEdgesByOutput {
static bool cmp(const Edge* a, const Edge* b) {
return a->outputs_[0]->path() < b->outputs_[0]->path();
}
};
/// Fixture for tests involving Plan.
// Though Plan doesn't use State, it's useful to have one around
// to create Nodes and Edges.
struct PlanTest : public StateTestWithBuiltinRules {
Plan plan_;
/// Because FindWork does not return Edges in any sort of predictable order,
// provide a means to get available Edges in order and in a format which is
// easy to write tests around.
void FindWorkSorted(deque<Edge*>* ret, int count) {
for (int i = 0; i < count; ++i) {
ASSERT_TRUE(plan_.more_to_do());
Edge* edge = plan_.FindWork();
ASSERT_TRUE(edge);
ret->push_back(edge);
}
ASSERT_FALSE(plan_.FindWork());
sort(ret->begin(), ret->end(), CompareEdgesByOutput::cmp);
}
void PrepareForTarget(const char* node, BuildLog *log=NULL) {
string err;
EXPECT_TRUE(plan_.AddTarget(GetNode(node), &err));
ASSERT_EQ("", err);
plan_.PrepareQueue();
ASSERT_TRUE(plan_.more_to_do());
}
void TestPoolWithDepthOne(const char *test_case);
};
TEST_F(PlanTest, Basic) {
ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
"build out: cat mid\n"
"build mid: cat in\n"));
GetNode("mid")->MarkDirty();
GetNode("out")->MarkDirty();
PrepareForTarget("out");
Edge* edge = plan_.FindWork();
ASSERT_TRUE(edge);
ASSERT_EQ("in", edge->inputs_[0]->path());
ASSERT_EQ("mid", edge->outputs_[0]->path());
ASSERT_FALSE(plan_.FindWork());
string err;
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_TRUE(edge);
ASSERT_EQ("mid", edge->inputs_[0]->path());
ASSERT_EQ("out", edge->outputs_[0]->path());
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
ASSERT_FALSE(plan_.more_to_do());
edge = plan_.FindWork();
ASSERT_EQ(0, edge);
}
// Test that two outputs from one rule can be handled as inputs to the next.
TEST_F(PlanTest, DoubleOutputDirect) {
ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
"build out: cat mid1 mid2\n"
"build mid1 mid2: cat in\n"));
GetNode("mid1")->MarkDirty();
GetNode("mid2")->MarkDirty();
GetNode("out")->MarkDirty();
PrepareForTarget("out");
Edge* edge;
edge = plan_.FindWork();
ASSERT_TRUE(edge); // cat in
string err;
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_TRUE(edge); // cat mid1 mid2
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_FALSE(edge); // done
}
// Test that two outputs from one rule can eventually be routed to another.
TEST_F(PlanTest, DoubleOutputIndirect) {
ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
"build out: cat b1 b2\n"
"build b1: cat a1\n"
"build b2: cat a2\n"
"build a1 a2: cat in\n"));
GetNode("a1")->MarkDirty();
GetNode("a2")->MarkDirty();
GetNode("b1")->MarkDirty();
GetNode("b2")->MarkDirty();
GetNode("out")->MarkDirty();
PrepareForTarget("out");
Edge* edge;
edge = plan_.FindWork();
ASSERT_TRUE(edge); // cat in
string err;
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_TRUE(edge); // cat a1
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_TRUE(edge); // cat a2
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_TRUE(edge); // cat b1 b2
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_FALSE(edge); // done
}
// Test that two edges from one output can both execute.
TEST_F(PlanTest, DoubleDependent) {
ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
"build out: cat a1 a2\n"
"build a1: cat mid\n"
"build a2: cat mid\n"
"build mid: cat in\n"));
GetNode("mid")->MarkDirty();
GetNode("a1")->MarkDirty();
GetNode("a2")->MarkDirty();
GetNode("out")->MarkDirty();
PrepareForTarget("out");
Edge* edge;
edge = plan_.FindWork();
ASSERT_TRUE(edge); // cat in
string err;
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_TRUE(edge); // cat mid
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_TRUE(edge); // cat mid
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_TRUE(edge); // cat a1 a2
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_FALSE(edge); // done
}
void PlanTest::TestPoolWithDepthOne(const char* test_case) {
ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, test_case));
GetNode("out1")->MarkDirty();
GetNode("out2")->MarkDirty();
string err;
EXPECT_TRUE(plan_.AddTarget(GetNode("out1"), &err));
ASSERT_EQ("", err);
EXPECT_TRUE(plan_.AddTarget(GetNode("out2"), &err));
ASSERT_EQ("", err);
plan_.PrepareQueue();
ASSERT_TRUE(plan_.more_to_do());
Edge* edge = plan_.FindWork();
ASSERT_TRUE(edge);
ASSERT_EQ("in", edge->inputs_[0]->path());
ASSERT_EQ("out1", edge->outputs_[0]->path());
// This will be false since poolcat is serialized
ASSERT_FALSE(plan_.FindWork());
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_TRUE(edge);
ASSERT_EQ("in", edge->inputs_[0]->path());
ASSERT_EQ("out2", edge->outputs_[0]->path());
ASSERT_FALSE(plan_.FindWork());
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
ASSERT_FALSE(plan_.more_to_do());
edge = plan_.FindWork();
ASSERT_EQ(0, edge);
}
TEST_F(PlanTest, PoolWithDepthOne) {
TestPoolWithDepthOne(
"pool foobar\n"
" depth = 1\n"
"rule poolcat\n"
" command = cat $in > $out\n"
" pool = foobar\n"
"build out1: poolcat in\n"
"build out2: poolcat in\n");
}
TEST_F(PlanTest, ConsolePool) {
TestPoolWithDepthOne(
"rule poolcat\n"
" command = cat $in > $out\n"
" pool = console\n"
"build out1: poolcat in\n"
"build out2: poolcat in\n");
}
TEST_F(PlanTest, PoolsWithDepthTwo) {
ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
"pool foobar\n"
" depth = 2\n"
"pool bazbin\n"
" depth = 2\n"
"rule foocat\n"
" command = cat $in > $out\n"
" pool = foobar\n"
"rule bazcat\n"
" command = cat $in > $out\n"
" pool = bazbin\n"
"build out1: foocat in\n"
"build out2: foocat in\n"
"build out3: foocat in\n"
"build outb1: bazcat in\n"
"build outb2: bazcat in\n"
"build outb3: bazcat in\n"
" pool =\n"
"build allTheThings: cat out1 out2 out3 outb1 outb2 outb3\n"
));
// Mark all the out* nodes dirty
for (int i = 0; i < 3; ++i) {
GetNode("out" + string(1, '1' + static_cast<char>(i)))->MarkDirty();
GetNode("outb" + string(1, '1' + static_cast<char>(i)))->MarkDirty();
}
GetNode("allTheThings")->MarkDirty();
PrepareForTarget("allTheThings");
deque<Edge*> edges;
FindWorkSorted(&edges, 5);
for (int i = 0; i < 4; ++i) {
Edge *edge = edges[i];
ASSERT_EQ("in", edge->inputs_[0]->path());
string base_name(i < 2 ? "out" : "outb");
ASSERT_EQ(base_name + string(1, '1' + (i % 2)), edge->outputs_[0]->path());
}
// outb3 is exempt because it has an empty pool
Edge* edge = edges[4];
ASSERT_TRUE(edge);
ASSERT_EQ("in", edge->inputs_[0]->path());
ASSERT_EQ("outb3", edge->outputs_[0]->path());
// finish out1
string err;
plan_.EdgeFinished(edges.front(), Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edges.pop_front();
// out3 should be available
Edge* out3 = plan_.FindWork();
ASSERT_TRUE(out3);
ASSERT_EQ("in", out3->inputs_[0]->path());
ASSERT_EQ("out3", out3->outputs_[0]->path());
ASSERT_FALSE(plan_.FindWork());
plan_.EdgeFinished(out3, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
ASSERT_FALSE(plan_.FindWork());
for (deque<Edge*>::iterator it = edges.begin(); it != edges.end(); ++it) {
plan_.EdgeFinished(*it, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
}
Edge* last = plan_.FindWork();
ASSERT_TRUE(last);
ASSERT_EQ("allTheThings", last->outputs_[0]->path());
plan_.EdgeFinished(last, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
ASSERT_FALSE(plan_.more_to_do());
ASSERT_FALSE(plan_.FindWork());
}
TEST_F(PlanTest, PoolWithRedundantEdges) {
ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
"pool compile\n"
" depth = 1\n"
"rule gen_foo\n"
" command = touch foo.cpp\n"
"rule gen_bar\n"
" command = touch bar.cpp\n"
"rule echo\n"
" command = echo $out > $out\n"
"build foo.cpp.obj: echo foo.cpp || foo.cpp\n"
" pool = compile\n"
"build bar.cpp.obj: echo bar.cpp || bar.cpp\n"
" pool = compile\n"
"build libfoo.a: echo foo.cpp.obj bar.cpp.obj\n"
"build foo.cpp: gen_foo\n"
"build bar.cpp: gen_bar\n"
"build all: phony libfoo.a\n"));
GetNode("foo.cpp")->MarkDirty();
GetNode("foo.cpp.obj")->MarkDirty();
GetNode("bar.cpp")->MarkDirty();
GetNode("bar.cpp.obj")->MarkDirty();
GetNode("libfoo.a")->MarkDirty();
GetNode("all")->MarkDirty();
PrepareForTarget("all");
Edge* edge = NULL;
deque<Edge*> initial_edges;
FindWorkSorted(&initial_edges, 2);
edge = initial_edges[1]; // Foo first
ASSERT_EQ("foo.cpp", edge->outputs_[0]->path());
string err;
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_TRUE(edge);
ASSERT_FALSE(plan_.FindWork());
ASSERT_EQ("foo.cpp", edge->inputs_[0]->path());
ASSERT_EQ("foo.cpp", edge->inputs_[1]->path());
ASSERT_EQ("foo.cpp.obj", edge->outputs_[0]->path());
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = initial_edges[0]; // Now for bar
ASSERT_EQ("bar.cpp", edge->outputs_[0]->path());
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_TRUE(edge);
ASSERT_FALSE(plan_.FindWork());
ASSERT_EQ("bar.cpp", edge->inputs_[0]->path());
ASSERT_EQ("bar.cpp", edge->inputs_[1]->path());
ASSERT_EQ("bar.cpp.obj", edge->outputs_[0]->path());
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_TRUE(edge);
ASSERT_FALSE(plan_.FindWork());
ASSERT_EQ("foo.cpp.obj", edge->inputs_[0]->path());
ASSERT_EQ("bar.cpp.obj", edge->inputs_[1]->path());
ASSERT_EQ("libfoo.a", edge->outputs_[0]->path());
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_TRUE(edge);
ASSERT_FALSE(plan_.FindWork());
ASSERT_EQ("libfoo.a", edge->inputs_[0]->path());
ASSERT_EQ("all", edge->outputs_[0]->path());
plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_FALSE(edge);
ASSERT_FALSE(plan_.more_to_do());
}
TEST_F(PlanTest, PoolWithFailingEdge) {
ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
"pool foobar\n"
" depth = 1\n"
"rule poolcat\n"
" command = cat $in > $out\n"
" pool = foobar\n"
"build out1: poolcat in\n"
"build out2: poolcat in\n"));
GetNode("out1")->MarkDirty();
GetNode("out2")->MarkDirty();
string err;
EXPECT_TRUE(plan_.AddTarget(GetNode("out1"), &err));
ASSERT_EQ("", err);
EXPECT_TRUE(plan_.AddTarget(GetNode("out2"), &err));
ASSERT_EQ("", err);
plan_.PrepareQueue();
ASSERT_TRUE(plan_.more_to_do());
Edge* edge = plan_.FindWork();
ASSERT_TRUE(edge);
ASSERT_EQ("in", edge->inputs_[0]->path());
ASSERT_EQ("out1", edge->outputs_[0]->path());
// This will be false since poolcat is serialized
ASSERT_FALSE(plan_.FindWork());
plan_.EdgeFinished(edge, Plan::kEdgeFailed, &err);
ASSERT_EQ("", err);
edge = plan_.FindWork();
ASSERT_TRUE(edge);
ASSERT_EQ("in", edge->inputs_[0]->path());
ASSERT_EQ("out2", edge->outputs_[0]->path());
ASSERT_FALSE(plan_.FindWork());
plan_.EdgeFinished(edge, Plan::kEdgeFailed, &err);
ASSERT_EQ("", err);
ASSERT_TRUE(plan_.more_to_do()); // Jobs have failed
edge = plan_.FindWork();
ASSERT_EQ(0, edge);
}
TEST_F(PlanTest, PriorityWithoutBuildLog) {
// Without a build log, the critical time is equivalent to graph
// depth. Test with the following graph:
// a2
// |
// a1 b1
// | | |
// a0 b0 c0
// \ | /
// out
ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
"rule r\n"
" command = unused\n"
"build out: r a0 b0 c0\n"
"build a0: r a1\n"
"build a1: r a2\n"
"build b0: r b1\n"
"build c0: r b1\n"
));
GetNode("a1")->MarkDirty();
GetNode("a0")->MarkDirty();
GetNode("b0")->MarkDirty();
GetNode("c0")->MarkDirty();
GetNode("out")->MarkDirty();
BuildLog log;
PrepareForTarget("out", &log);
EXPECT_EQ(GetNode("out")->in_edge()->critical_path_weight(), 1);
EXPECT_EQ(GetNode("a0")->in_edge()->critical_path_weight(), 2);
EXPECT_EQ(GetNode("b0")->in_edge()->critical_path_weight(), 2);
EXPECT_EQ(GetNode("c0")->in_edge()->critical_path_weight(), 2);
EXPECT_EQ(GetNode("a1")->in_edge()->critical_path_weight(), 3);
const int n_edges = 5;
const char *expected_order[n_edges] = {
"a1", "a0", "b0", "c0", "out"};
for (int i = 0; i < n_edges; ++i) {
Edge* edge = plan_.FindWork();
ASSERT_TRUE(edge != nullptr);
EXPECT_EQ(expected_order[i], edge->outputs_[0]->path());
std::string err;
ASSERT_TRUE(plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err));
EXPECT_EQ(err, "");
}
EXPECT_FALSE(plan_.FindWork());
}
/// Fake implementation of CommandRunner, useful for tests.
struct FakeCommandRunner : public CommandRunner {
explicit FakeCommandRunner(VirtualFileSystem* fs) :
max_active_edges_(1), fs_(fs) {}
// CommandRunner impl
virtual size_t CanRunMore() const;
virtual bool StartCommand(Edge* edge);
virtual bool WaitForCommand(Result* result);
virtual vector<Edge*> GetActiveEdges();
virtual void Abort();
vector<string> commands_ran_;
vector<Edge*> active_edges_;
size_t max_active_edges_;
VirtualFileSystem* fs_;
};
struct BuildTest : public StateTestWithBuiltinRules, public BuildLogUser {
BuildTest() : config_(MakeConfig()), command_runner_(&fs_), status_(config_),
builder_(&state_, config_, NULL, NULL, &fs_, &status_, 0) {
}
explicit BuildTest(DepsLog* log)
: config_(MakeConfig()), command_runner_(&fs_), status_(config_),
builder_(&state_, config_, NULL, log, &fs_, &status_, 0) {}
virtual void SetUp() {
StateTestWithBuiltinRules::SetUp();
builder_.command_runner_.reset(&command_runner_);
AssertParse(&state_,
"build cat1: cat in1\n"
"build cat2: cat in1 in2\n"
"build cat12: cat cat1 cat2\n");
fs_.Create("in1", "");
fs_.Create("in2", "");
}
~BuildTest() {
builder_.command_runner_.release();
}
virtual bool IsPathDead(StringPiece s) const { return false; }
/// Rebuild target in the 'working tree' (fs_).
/// State of command_runner_ and logs contents (if specified) ARE MODIFIED.
/// Handy to check for NOOP builds, and higher-level rebuild tests.
void RebuildTarget(const string& target, const char* manifest,
const char* log_path = NULL, const char* deps_path = NULL,
State* state = NULL);
// Mark a path dirty.
void Dirty(const string& path);
BuildConfig MakeConfig() {
BuildConfig config;
config.verbosity = BuildConfig::QUIET;
return config;
}
BuildConfig config_;
FakeCommandRunner command_runner_;
VirtualFileSystem fs_;
StatusPrinter status_;
Builder builder_;
};
void BuildTest::RebuildTarget(const string& target, const char* manifest,
const char* log_path, const char* deps_path,
State* state) {
State local_state, *pstate = &local_state;
if (state)
pstate = state;
ASSERT_NO_FATAL_FAILURE(AddCatRule(pstate));
AssertParse(pstate, manifest);
string err;
BuildLog build_log, *pbuild_log = NULL;
if (log_path) {
ASSERT_TRUE(build_log.Load(log_path, &err));
ASSERT_TRUE(build_log.OpenForWrite(log_path, *this, &err));
ASSERT_EQ("", err);
pbuild_log = &build_log;
}
DepsLog deps_log, *pdeps_log = NULL;
if (deps_path) {
ASSERT_TRUE(deps_log.Load(deps_path, pstate, &err));
ASSERT_TRUE(deps_log.OpenForWrite(deps_path, &err));
ASSERT_EQ("", err);
pdeps_log = &deps_log;
}
Builder builder(pstate, config_, pbuild_log, pdeps_log, &fs_, &status_, 0);
EXPECT_TRUE(builder.AddTarget(target, &err));
command_runner_.commands_ran_.clear();
builder.command_runner_.reset(&command_runner_);
if (!builder.AlreadyUpToDate()) {
bool build_res = builder.Build(&err);
EXPECT_TRUE(build_res);
}
builder.command_runner_.release();
}
size_t FakeCommandRunner::CanRunMore() const {
if (active_edges_.size() < max_active_edges_)
return SIZE_MAX;
return 0;
}
bool FakeCommandRunner::StartCommand(Edge* edge) {
assert(active_edges_.size() < max_active_edges_);
assert(find(active_edges_.begin(), active_edges_.end(), edge)
== active_edges_.end());
commands_ran_.push_back(edge->EvaluateCommand());
if (edge->rule().name() == "cat" ||
edge->rule().name() == "cat_rsp" ||
edge->rule().name() == "cat_rsp_out" ||
edge->rule().name() == "cc" ||
edge->rule().name() == "cp_multi_msvc" ||
edge->rule().name() == "cp_multi_gcc" ||
edge->rule().name() == "touch" ||
edge->rule().name() == "touch-interrupt" ||
edge->rule().name() == "touch-fail-tick2") {
for (vector<Node*>::iterator out = edge->outputs_.begin();
out != edge->outputs_.end(); ++out) {
fs_->Create((*out)->path(), "");
}
} else if (edge->rule().name() == "true" ||
edge->rule().name() == "fail" ||
edge->rule().name() == "interrupt" ||
edge->rule().name() == "console") {
// Don't do anything.
} else if (edge->rule().name() == "cp") {
assert(!edge->inputs_.empty());
assert(edge->outputs_.size() == 1);
string content;
string err;
if (fs_->ReadFile(edge->inputs_[0]->path(), &content, &err) ==
DiskInterface::Okay)
fs_->WriteFile(edge->outputs_[0]->path(), content);
} else if (edge->rule().name() == "touch-implicit-dep-out") {
string dep = edge->GetBinding("test_dependency");
fs_->Tick();
fs_->Create(dep, "");
fs_->Tick();
for (vector<Node*>::iterator out = edge->outputs_.begin();
out != edge->outputs_.end(); ++out) {
fs_->Create((*out)->path(), "");
}
} else if (edge->rule().name() == "touch-out-implicit-dep") {
string dep = edge->GetBinding("test_dependency");
for (vector<Node*>::iterator out = edge->outputs_.begin();
out != edge->outputs_.end(); ++out) {
fs_->Create((*out)->path(), "");
}
fs_->Tick();
fs_->Create(dep, "");
} else if (edge->rule().name() == "generate-depfile") {
string dep = edge->GetBinding("test_dependency");
bool touch_dep = edge->GetBindingBool("touch_dependency");
string depfile = edge->GetUnescapedDepfile();
if (touch_dep) {
fs_->Tick();
fs_->Create(dep, "");
}
string contents;
for (vector<Node*>::iterator out = edge->outputs_.begin();
out != edge->outputs_.end(); ++out) {
contents += (*out)->path() + ": " + dep + "\n";
fs_->Create((*out)->path(), "");
}
fs_->Create(depfile, contents);
} else if (edge->rule().name() == "long-cc") {
string dep = edge->GetBinding("test_dependency");
string depfile = edge->GetUnescapedDepfile();
string contents;
for (vector<Node*>::iterator out = edge->outputs_.begin();
out != edge->outputs_.end(); ++out) {
fs_->Tick();
fs_->Tick();
fs_->Tick();
fs_->Create((*out)->path(), "");
contents += (*out)->path() + ": " + dep + "\n";
}
if (!dep.empty() && !depfile.empty())
fs_->Create(depfile, contents);
} else {
printf("unknown command\n");
return false;
}
active_edges_.push_back(edge);
// Allow tests to control the order by the name of the first output.
sort(active_edges_.begin(), active_edges_.end(),
CompareEdgesByOutput::cmp);
return true;
}
bool FakeCommandRunner::WaitForCommand(Result* result) {
if (active_edges_.empty())
return false;
// All active edges were already completed immediately when started,
// so we can pick any edge here. Pick the last edge. Tests can
// control the order of edges by the name of the first output.
vector<Edge*>::iterator edge_iter = active_edges_.end() - 1;
Edge* edge = *edge_iter;
result->edge = edge;
if (edge->rule().name() == "interrupt" ||
edge->rule().name() == "touch-interrupt") {
result->status = ExitInterrupted;
return true;
}
if (edge->rule().name() == "console") {
if (edge->use_console())
result->status = ExitSuccess;
else
result->status = ExitFailure;
active_edges_.erase(edge_iter);
return true;
}
if (edge->rule().name() == "cp_multi_msvc") {
const std::string prefix = edge->GetBinding("msvc_deps_prefix");
for (std::vector<Node*>::iterator in = edge->inputs_.begin();
in != edge->inputs_.end(); ++in) {
result->output += prefix + (*in)->path() + '\n';
}
}
if (edge->rule().name() == "fail" ||
(edge->rule().name() == "touch-fail-tick2" && fs_->now_ == 2))
result->status = ExitFailure;
else
result->status = ExitSuccess;
// This rule simulates an external process modifying files while the build command runs.
// See TestInputMtimeRaceCondition and TestInputMtimeRaceConditionWithDepFile.
// Note: only the first and third time the rule is run per test is the file modified, so
// the test can verify that subsequent runs without the race have no work to do.
if (edge->rule().name() == "long-cc") {
string dep = edge->GetBinding("test_dependency");
if (fs_->now_ == 4)
fs_->files_[dep].mtime = 3;
if (fs_->now_ == 10)
fs_->files_[dep].mtime = 9;
}
// Provide a way for test cases to verify when an edge finishes that
// some other edge is still active. This is useful for test cases
// covering behavior involving multiple active edges.
const string& verify_active_edge = edge->GetBinding("verify_active_edge");
if (!verify_active_edge.empty()) {
bool verify_active_edge_found = false;
for (vector<Edge*>::iterator i = active_edges_.begin();
i != active_edges_.end(); ++i) {
if (!(*i)->outputs_.empty() &&
(*i)->outputs_[0]->path() == verify_active_edge) {
verify_active_edge_found = true;
}
}
EXPECT_TRUE(verify_active_edge_found);
}
active_edges_.erase(edge_iter);
return true;
}
vector<Edge*> FakeCommandRunner::GetActiveEdges() {
return active_edges_;
}
void FakeCommandRunner::Abort() {
active_edges_.clear();
}
void BuildTest::Dirty(const string& path) {
Node* node = GetNode(path);
node->MarkDirty();
// If it's an input file, mark that we've already stat()ed it and
// it's missing.
if (!node->in_edge())
node->MarkMissing();
}
TEST_F(BuildTest, NoWork) {
string err;
EXPECT_TRUE(builder_.AlreadyUpToDate());
}
TEST_F(BuildTest, OneStep) {
// Given a dirty target with one ready input,
// we should rebuild the target.
Dirty("cat1");
string err;
EXPECT_TRUE(builder_.AddTarget("cat1", &err));
ASSERT_EQ("", err);
EXPECT_TRUE(builder_.Build(&err));
ASSERT_EQ("", err);
ASSERT_EQ(1u, command_runner_.commands_ran_.size());
EXPECT_EQ("cat in1 > cat1", command_runner_.commands_ran_[0]);
}
TEST_F(BuildTest, OneStep2) {
// Given a target with one dirty input,
// we should rebuild the target.
Dirty("cat1");
string err;
EXPECT_TRUE(builder_.AddTarget("cat1", &err));
ASSERT_EQ("", err);
EXPECT_TRUE(builder_.Build(&err));
EXPECT_EQ("", err);
ASSERT_EQ(1u, command_runner_.commands_ran_.size());
EXPECT_EQ("cat in1 > cat1", command_runner_.commands_ran_[0]);
}
TEST_F(BuildTest, TwoStep) {
string err;
EXPECT_TRUE(builder_.AddTarget("cat12", &err));
ASSERT_EQ("", err);
EXPECT_TRUE(builder_.Build(&err));
EXPECT_EQ("", err);
ASSERT_EQ(3u, command_runner_.commands_ran_.size());
// Depending on how the pointers work out, we could've ran
// the first two commands in either order.
EXPECT_TRUE((command_runner_.commands_ran_[0] == "cat in1 > cat1" &&
command_runner_.commands_ran_[1] == "cat in1 in2 > cat2") ||
(command_runner_.commands_ran_[1] == "cat in1 > cat1" &&
command_runner_.commands_ran_[0] == "cat in1 in2 > cat2"));
EXPECT_EQ("cat cat1 cat2 > cat12", command_runner_.commands_ran_[2]);
fs_.Tick();
// Modifying in2 requires rebuilding one intermediate file
// and the final file.
fs_.Create("in2", "");
state_.Reset();
EXPECT_TRUE(builder_.AddTarget("cat12", &err));
ASSERT_EQ("", err);
EXPECT_TRUE(builder_.Build(&err));
ASSERT_EQ("", err);
ASSERT_EQ(5u, command_runner_.commands_ran_.size());
EXPECT_EQ("cat in1 in2 > cat2", command_runner_.commands_ran_[3]);
EXPECT_EQ("cat cat1 cat2 > cat12", command_runner_.commands_ran_[4]);
}
TEST_F(BuildTest, TwoOutputs) {
ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
"rule touch\n"
" command = touch $out\n"
"build out1 out2: touch in.txt\n"));
fs_.Create("in.txt", "");
string err;
EXPECT_TRUE(builder_.AddTarget("out1", &err));
ASSERT_EQ("", err);
EXPECT_TRUE(builder_.Build(&err));
EXPECT_EQ("", err);
ASSERT_EQ(1u, command_runner_.commands_ran_.size());
EXPECT_EQ("touch out1 out2", command_runner_.commands_ran_[0]);
}
TEST_F(BuildTest, ImplicitOutput) {
ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
"rule touch\n"
" command = touch $out $out.imp\n"
"build out | out.imp: touch in.txt\n"));
fs_.Create("in.txt", "");
string err;
EXPECT_TRUE(builder_.AddTarget("out.imp", &err));
ASSERT_EQ("", err);
EXPECT_TRUE(builder_.Build(&err));
EXPECT_EQ("", err);
ASSERT_EQ(1u, command_runner_.commands_ran_.size());
EXPECT_EQ("touch out out.imp", command_runner_.commands_ran_[0]);
}
// Test case from
// https://github.com/ninja-build/ninja/issues/148
TEST_F(BuildTest, MultiOutIn) {
ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
"rule touch\n"
" command = touch $out\n"
"build in1 otherfile: touch in\n"
"build out: touch in | in1\n"));
fs_.Create("in", "");
fs_.Tick();
fs_.Create("in1", "");
string err;
EXPECT_TRUE(builder_.AddTarget("out", &err));
ASSERT_EQ("", err);
EXPECT_TRUE(builder_.Build(&err));
EXPECT_EQ("", err);
}
TEST_F(BuildTest, Chain) {
ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
"build c2: cat c1\n"
"build c3: cat c2\n"
"build c4: cat c3\n"
"build c5: cat c4\n"));
fs_.Create("c1", "");
string err;
EXPECT_TRUE(builder_.AddTarget("c5", &err));
ASSERT_EQ("", err);
EXPECT_TRUE(builder_.Build(&err));
EXPECT_EQ("", err);
ASSERT_EQ(4u, command_runner_.commands_ran_.size());
err.clear();
command_runner_.commands_ran_.clear();
state_.Reset();
EXPECT_TRUE(builder_.AddTarget("c5", &err));
ASSERT_EQ("", err);
EXPECT_TRUE(builder_.AlreadyUpToDate());
fs_.Tick();
fs_.Create("c3", "");
err.clear();
command_runner_.commands_ran_.clear();
state_.Reset();
EXPECT_TRUE(builder_.AddTarget("c5", &err));
ASSERT_EQ("", err);
EXPECT_FALSE(builder_.AlreadyUpToDate());
EXPECT_TRUE(builder_.Build(&err));
ASSERT_EQ(2u, command_runner_.commands_ran_.size()); // 3->4, 4->5
}
TEST_F(BuildTest, MissingInput) {
// Input is referenced by build file, but no rule for it.
string err;
Dirty("in1");
EXPECT_FALSE(builder_.AddTarget("cat1", &err));
EXPECT_EQ("'in1', needed by 'cat1', missing and no known rule to make it",
err);
}
TEST_F(BuildTest, MissingTarget) {
// Target is not referenced by build file.
string err;
EXPECT_FALSE(builder_.AddTarget("meow", &err));
EXPECT_EQ("unknown target: 'meow'", err);
}
TEST_F(BuildTest, MissingInputTarget) {
// Target is a missing input file
string err;
Dirty("in1");
EXPECT_FALSE(builder_.AddTarget("in1", &err));
EXPECT_EQ("'in1' missing and no known rule to make it", err);
}
TEST_F(BuildTest, MakeDirs) {
string err;
#ifdef _WIN32
ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
"build subdir\\dir2\\file: cat in1\n"));
#else
ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
"build subdir/dir2/file: cat in1\n"));
#endif