Ninja
graph_test.cc
Go to the documentation of this file.
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 "graph.h"
16 #include "build.h"
17 
18 #include "test.h"
19 
21  GraphTest() : scan_(&state_, NULL, NULL, &fs_, NULL) {}
22 
25 };
26 
27 TEST_F(GraphTest, MissingImplicit) {
29 "build out: cat in | implicit\n"));
30  fs_.Create("in", "");
31  fs_.Create("out", "");
32 
33  string err;
34  EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out"), &err));
35  ASSERT_EQ("", err);
36 
37  // A missing implicit dep *should* make the output dirty.
38  // (In fact, a build will fail.)
39  // This is a change from prior semantics of ninja.
40  EXPECT_TRUE(GetNode("out")->dirty());
41 }
42 
43 TEST_F(GraphTest, ModifiedImplicit) {
45 "build out: cat in | implicit\n"));
46  fs_.Create("in", "");
47  fs_.Create("out", "");
48  fs_.Tick();
49  fs_.Create("implicit", "");
50 
51  string err;
52  EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out"), &err));
53  ASSERT_EQ("", err);
54 
55  // A modified implicit dep should make the output dirty.
56  EXPECT_TRUE(GetNode("out")->dirty());
57 }
58 
59 TEST_F(GraphTest, FunkyMakefilePath) {
61 "rule catdep\n"
62 " depfile = $out.d\n"
63 " command = cat $in > $out\n"
64 "build out.o: catdep foo.cc\n"));
65  fs_.Create("foo.cc", "");
66  fs_.Create("out.o.d", "out.o: ./foo/../implicit.h\n");
67  fs_.Create("out.o", "");
68  fs_.Tick();
69  fs_.Create("implicit.h", "");
70 
71  string err;
72  EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
73  ASSERT_EQ("", err);
74 
75  // implicit.h has changed, though our depfile refers to it with a
76  // non-canonical path; we should still find it.
77  EXPECT_TRUE(GetNode("out.o")->dirty());
78 }
79 
80 TEST_F(GraphTest, ExplicitImplicit) {
82 "rule catdep\n"
83 " depfile = $out.d\n"
84 " command = cat $in > $out\n"
85 "build implicit.h: cat data\n"
86 "build out.o: catdep foo.cc || implicit.h\n"));
87  fs_.Create("implicit.h", "");
88  fs_.Create("foo.cc", "");
89  fs_.Create("out.o.d", "out.o: implicit.h\n");
90  fs_.Create("out.o", "");
91  fs_.Tick();
92  fs_.Create("data", "");
93 
94  string err;
95  EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
96  ASSERT_EQ("", err);
97 
98  // We have both an implicit and an explicit dep on implicit.h.
99  // The implicit dep should "win" (in the sense that it should cause
100  // the output to be dirty).
101  EXPECT_TRUE(GetNode("out.o")->dirty());
102 }
103 
104 TEST_F(GraphTest, ImplicitOutputParse) {
106 "build out | out.imp: cat in\n"));
107 
108  Edge* edge = GetNode("out")->in_edge();
109  EXPECT_EQ(2, edge->outputs_.size());
110  EXPECT_EQ("out", edge->outputs_[0]->path());
111  EXPECT_EQ("out.imp", edge->outputs_[1]->path());
112  EXPECT_EQ(1, edge->implicit_outs_);
113  EXPECT_EQ(edge, GetNode("out.imp")->in_edge());
114 }
115 
116 TEST_F(GraphTest, ImplicitOutputMissing) {
118 "build out | out.imp: cat in\n"));
119  fs_.Create("in", "");
120  fs_.Create("out", "");
121 
122  string err;
123  EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out"), &err));
124  ASSERT_EQ("", err);
125 
126  EXPECT_TRUE(GetNode("out")->dirty());
127  EXPECT_TRUE(GetNode("out.imp")->dirty());
128 }
129 
130 TEST_F(GraphTest, ImplicitOutputOutOfDate) {
132 "build out | out.imp: cat in\n"));
133  fs_.Create("out.imp", "");
134  fs_.Tick();
135  fs_.Create("in", "");
136  fs_.Create("out", "");
137 
138  string err;
139  EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out"), &err));
140  ASSERT_EQ("", err);
141 
142  EXPECT_TRUE(GetNode("out")->dirty());
143  EXPECT_TRUE(GetNode("out.imp")->dirty());
144 }
145 
146 TEST_F(GraphTest, ImplicitOutputOnlyParse) {
148 "build | out.imp: cat in\n"));
149 
150  Edge* edge = GetNode("out.imp")->in_edge();
151  EXPECT_EQ(1, edge->outputs_.size());
152  EXPECT_EQ("out.imp", edge->outputs_[0]->path());
153  EXPECT_EQ(1, edge->implicit_outs_);
154  EXPECT_EQ(edge, GetNode("out.imp")->in_edge());
155 }
156 
157 TEST_F(GraphTest, ImplicitOutputOnlyMissing) {
159 "build | out.imp: cat in\n"));
160  fs_.Create("in", "");
161 
162  string err;
163  EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.imp"), &err));
164  ASSERT_EQ("", err);
165 
166  EXPECT_TRUE(GetNode("out.imp")->dirty());
167 }
168 
169 TEST_F(GraphTest, ImplicitOutputOnlyOutOfDate) {
171 "build | out.imp: cat in\n"));
172  fs_.Create("out.imp", "");
173  fs_.Tick();
174  fs_.Create("in", "");
175 
176  string err;
177  EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.imp"), &err));
178  ASSERT_EQ("", err);
179 
180  EXPECT_TRUE(GetNode("out.imp")->dirty());
181 }
182 
183 TEST_F(GraphTest, PathWithCurrentDirectory) {
185 "rule catdep\n"
186 " depfile = $out.d\n"
187 " command = cat $in > $out\n"
188 "build ./out.o: catdep ./foo.cc\n"));
189  fs_.Create("foo.cc", "");
190  fs_.Create("out.o.d", "out.o: foo.cc\n");
191  fs_.Create("out.o", "");
192 
193  string err;
194  EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
195  ASSERT_EQ("", err);
196 
197  EXPECT_FALSE(GetNode("out.o")->dirty());
198 }
199 
200 TEST_F(GraphTest, RootNodes) {
202 "build out1: cat in1\n"
203 "build mid1: cat in1\n"
204 "build out2: cat mid1\n"
205 "build out3 out4: cat mid1\n"));
206 
207  string err;
208  vector<Node*> root_nodes = state_.RootNodes(&err);
209  EXPECT_EQ(4u, root_nodes.size());
210  for (size_t i = 0; i < root_nodes.size(); ++i) {
211  string name = root_nodes[i]->path();
212  EXPECT_EQ("out", name.substr(0, 3));
213  }
214 }
215 
216 TEST_F(GraphTest, VarInOutPathEscaping) {
218 "build a$ b: cat no'space with$ space$$ no\"space2\n"));
219 
220  Edge* edge = GetNode("a b")->in_edge();
221 #if _WIN32
222  EXPECT_EQ("cat no'space \"with space$\" \"no\\\"space2\" > \"a b\"",
223  edge->EvaluateCommand());
224 #else
225  EXPECT_EQ("cat 'no'\\''space' 'with space$' 'no\"space2' > 'a b'",
226  edge->EvaluateCommand());
227 #endif
228 }
229 
230 // Regression test for https://github.com/ninja-build/ninja/issues/380
231 TEST_F(GraphTest, DepfileWithCanonicalizablePath) {
233 "rule catdep\n"
234 " depfile = $out.d\n"
235 " command = cat $in > $out\n"
236 "build ./out.o: catdep ./foo.cc\n"));
237  fs_.Create("foo.cc", "");
238  fs_.Create("out.o.d", "out.o: bar/../foo.cc\n");
239  fs_.Create("out.o", "");
240 
241  string err;
242  EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
243  ASSERT_EQ("", err);
244 
245  EXPECT_FALSE(GetNode("out.o")->dirty());
246 }
247 
248 // Regression test for https://github.com/ninja-build/ninja/issues/404
249 TEST_F(GraphTest, DepfileRemoved) {
251 "rule catdep\n"
252 " depfile = $out.d\n"
253 " command = cat $in > $out\n"
254 "build ./out.o: catdep ./foo.cc\n"));
255  fs_.Create("foo.h", "");
256  fs_.Create("foo.cc", "");
257  fs_.Tick();
258  fs_.Create("out.o.d", "out.o: foo.h\n");
259  fs_.Create("out.o", "");
260 
261  string err;
262  EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
263  ASSERT_EQ("", err);
264  EXPECT_FALSE(GetNode("out.o")->dirty());
265 
266  state_.Reset();
267  fs_.RemoveFile("out.o.d");
268  EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
269  ASSERT_EQ("", err);
270  EXPECT_TRUE(GetNode("out.o")->dirty());
271 }
272 
273 // Check that rule-level variables are in scope for eval.
274 TEST_F(GraphTest, RuleVariablesInScope) {
276 "rule r\n"
277 " depfile = x\n"
278 " command = depfile is $depfile\n"
279 "build out: r in\n"));
280  Edge* edge = GetNode("out")->in_edge();
281  EXPECT_EQ("depfile is x", edge->EvaluateCommand());
282 }
283 
284 // Check that build statements can override rule builtins like depfile.
285 TEST_F(GraphTest, DepfileOverride) {
287 "rule r\n"
288 " depfile = x\n"
289 " command = unused\n"
290 "build out: r in\n"
291 " depfile = y\n"));
292  Edge* edge = GetNode("out")->in_edge();
293  EXPECT_EQ("y", edge->GetBinding("depfile"));
294 }
295 
296 // Check that overridden values show up in expansion of rule-level bindings.
297 TEST_F(GraphTest, DepfileOverrideParent) {
299 "rule r\n"
300 " depfile = x\n"
301 " command = depfile is $depfile\n"
302 "build out: r in\n"
303 " depfile = y\n"));
304  Edge* edge = GetNode("out")->in_edge();
305  EXPECT_EQ("depfile is y", edge->GetBinding("command"));
306 }
307 
308 // Verify that building a nested phony rule prints "no work to do"
309 TEST_F(GraphTest, NestedPhonyPrintsDone) {
310  AssertParse(&state_,
311 "build n1: phony \n"
312 "build n2: phony n1\n"
313  );
314  string err;
315  EXPECT_TRUE(scan_.RecomputeDirty(GetNode("n2"), &err));
316  ASSERT_EQ("", err);
317 
318  Plan plan_;
319  EXPECT_TRUE(plan_.AddTarget(GetNode("n2"), &err));
320  ASSERT_EQ("", err);
321 
322  EXPECT_EQ(0, plan_.command_edge_count());
323  ASSERT_FALSE(plan_.more_to_do());
324 }
325 
326 TEST_F(GraphTest, PhonySelfReferenceError) {
327  ManifestParserOptions parser_opts;
329  AssertParse(&state_,
330 "build a: phony a\n",
331  parser_opts);
332 
333  string err;
334  EXPECT_FALSE(scan_.RecomputeDirty(GetNode("a"), &err));
335  ASSERT_EQ("dependency cycle: a -> a [-w phonycycle=err]", err);
336 }
337 
338 TEST_F(GraphTest, DependencyCycle) {
339  AssertParse(&state_,
340 "build out: cat mid\n"
341 "build mid: cat in\n"
342 "build in: cat pre\n"
343 "build pre: cat out\n");
344 
345  string err;
346  EXPECT_FALSE(scan_.RecomputeDirty(GetNode("out"), &err));
347  ASSERT_EQ("dependency cycle: out -> mid -> in -> pre -> out", err);
348 }
349 
350 TEST_F(GraphTest, CycleInEdgesButNotInNodes1) {
351  string err;
352  AssertParse(&state_,
353 "build a b: cat a\n");
354  EXPECT_FALSE(scan_.RecomputeDirty(GetNode("b"), &err));
355  ASSERT_EQ("dependency cycle: a -> a", err);
356 }
357 
358 TEST_F(GraphTest, CycleInEdgesButNotInNodes2) {
359  string err;
361 "build b a: cat a\n"));
362  EXPECT_FALSE(scan_.RecomputeDirty(GetNode("b"), &err));
363  ASSERT_EQ("dependency cycle: a -> a", err);
364 }
365 
366 TEST_F(GraphTest, CycleInEdgesButNotInNodes3) {
367  string err;
369 "build a b: cat c\n"
370 "build c: cat a\n"));
371  EXPECT_FALSE(scan_.RecomputeDirty(GetNode("b"), &err));
372  ASSERT_EQ("dependency cycle: a -> c -> a", err);
373 }
374 
375 TEST_F(GraphTest, CycleInEdgesButNotInNodes4) {
376  string err;
378 "build d: cat c\n"
379 "build c: cat b\n"
380 "build b: cat a\n"
381 "build a e: cat d\n"
382 "build f: cat e\n"));
383  EXPECT_FALSE(scan_.RecomputeDirty(GetNode("f"), &err));
384  ASSERT_EQ("dependency cycle: a -> d -> c -> b -> a", err);
385 }
386 
387 // Verify that cycles in graphs with multiple outputs are handled correctly
388 // in RecomputeDirty() and don't cause deps to be loaded multiple times.
389 TEST_F(GraphTest, CycleWithLengthZeroFromDepfile) {
390  AssertParse(&state_,
391 "rule deprule\n"
392 " depfile = dep.d\n"
393 " command = unused\n"
394 "build a b: deprule\n"
395  );
396  fs_.Create("dep.d", "a: b\n");
397 
398  string err;
399  EXPECT_FALSE(scan_.RecomputeDirty(GetNode("a"), &err));
400  ASSERT_EQ("dependency cycle: b -> b", err);
401 
402  // Despite the depfile causing edge to be a cycle (it has outputs a and b,
403  // but the depfile also adds b as an input), the deps should have been loaded
404  // only once:
405  Edge* edge = GetNode("a")->in_edge();
406  EXPECT_EQ(1, edge->inputs_.size());
407  EXPECT_EQ("b", edge->inputs_[0]->path());
408 }
409 
410 // Like CycleWithLengthZeroFromDepfile but with a higher cycle length.
411 TEST_F(GraphTest, CycleWithLengthOneFromDepfile) {
412  AssertParse(&state_,
413 "rule deprule\n"
414 " depfile = dep.d\n"
415 " command = unused\n"
416 "rule r\n"
417 " command = unused\n"
418 "build a b: deprule\n"
419 "build c: r b\n"
420  );
421  fs_.Create("dep.d", "a: c\n");
422 
423  string err;
424  EXPECT_FALSE(scan_.RecomputeDirty(GetNode("a"), &err));
425  ASSERT_EQ("dependency cycle: b -> c -> b", err);
426 
427  // Despite the depfile causing edge to be a cycle (|edge| has outputs a and b,
428  // but c's in_edge has b as input but the depfile also adds |edge| as
429  // output)), the deps should have been loaded only once:
430  Edge* edge = GetNode("a")->in_edge();
431  EXPECT_EQ(1, edge->inputs_.size());
432  EXPECT_EQ("c", edge->inputs_[0]->path());
433 }
434 
435 // Like CycleWithLengthOneFromDepfile but building a node one hop away from
436 // the cycle.
437 TEST_F(GraphTest, CycleWithLengthOneFromDepfileOneHopAway) {
438  AssertParse(&state_,
439 "rule deprule\n"
440 " depfile = dep.d\n"
441 " command = unused\n"
442 "rule r\n"
443 " command = unused\n"
444 "build a b: deprule\n"
445 "build c: r b\n"
446 "build d: r a\n"
447  );
448  fs_.Create("dep.d", "a: c\n");
449 
450  string err;
451  EXPECT_FALSE(scan_.RecomputeDirty(GetNode("d"), &err));
452  ASSERT_EQ("dependency cycle: b -> c -> b", err);
453 
454  // Despite the depfile causing edge to be a cycle (|edge| has outputs a and b,
455  // but c's in_edge has b as input but the depfile also adds |edge| as
456  // output)), the deps should have been loaded only once:
457  Edge* edge = GetNode("a")->in_edge();
458  EXPECT_EQ(1, edge->inputs_.size());
459  EXPECT_EQ("c", edge->inputs_[0]->path());
460 }
461 
462 #ifdef _WIN32
463 TEST_F(GraphTest, Decanonicalize) {
465 "build out\\out1: cat src\\in1\n"
466 "build out\\out2/out3\\out4: cat mid1\n"
467 "build out3 out4\\foo: cat mid1\n"));
468 
469  string err;
470  vector<Node*> root_nodes = state_.RootNodes(&err);
471  EXPECT_EQ(4u, root_nodes.size());
472  EXPECT_EQ(root_nodes[0]->path(), "out/out1");
473  EXPECT_EQ(root_nodes[1]->path(), "out/out2/out3/out4");
474  EXPECT_EQ(root_nodes[2]->path(), "out3");
475  EXPECT_EQ(root_nodes[3]->path(), "out4/foo");
476  EXPECT_EQ(root_nodes[0]->PathDecanonicalized(), "out\\out1");
477  EXPECT_EQ(root_nodes[1]->PathDecanonicalized(), "out\\out2/out3\\out4");
478  EXPECT_EQ(root_nodes[2]->PathDecanonicalized(), "out3");
479  EXPECT_EQ(root_nodes[3]->PathDecanonicalized(), "out4\\foo");
480 }
481 #endif
An implementation of DiskInterface that uses an in-memory representation of disk state.
Definition: test.h:134
void AssertParse(State *state, const char *input, ManifestParserOptions opts)
Definition: test.cc:98
Plan stores the state of a build plan: what we intend to build, which steps we&#39;re ready to execute...
Definition: build.h:42
PhonyCycleAction phony_cycle_action_
#define EXPECT_TRUE(a)
Definition: test.h:76
bool AddTarget(Node *node, string *err)
Add a target to our plan (including all its dependencies).
Definition: build.cc:299
#define EXPECT_FALSE(a)
Definition: test.h:78
int command_edge_count() const
Number of edges with commands to run.
Definition: build.h:73
An edge in the dependency graph; links between Nodes using Rules.
Definition: graph.h:131
string EvaluateCommand(bool incl_rsp_file=false)
Expand all variables in a command and return it as a string.
Definition: graph.cc:362
A base test fixture that includes a State object with a builtin "cat" rule.
Definition: test.h:113
VirtualFileSystem fs_
Definition: graph_test.cc:23
vector< Node * > inputs_
Definition: graph.h:163
#define EXPECT_EQ(a, b)
Definition: test.h:64
DependencyScan scan_
Definition: graph_test.cc:24
#define ASSERT_FALSE(a)
Definition: test.h:95
int implicit_outs_
Definition: graph.h:198
#define ASSERT_EQ(a, b)
Definition: test.h:81
string GetBinding(const string &key)
Returns the shell-escaped value of |key|.
Definition: graph.cc:372
DependencyScan manages the process of scanning the files in a graph and updating the dirty/outputs_re...
Definition: graph.h:254
#define ASSERT_NO_FATAL_FAILURE(a)
Definition: test.h:97
bool more_to_do() const
Returns true if there&#39;s more work to be done.
Definition: build.h:55
TEST_F(GraphTest, MissingImplicit)
Definition: graph_test.cc:27
vector< Node * > outputs_
Definition: graph.h:164