1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 struct GraphTest : public StateTestWithBuiltinRules {
21 GraphTest() : scan_(&state_, NULL, NULL, &fs_) {}
23 VirtualFileSystem fs_;
27 TEST_F(GraphTest, MissingImplicit) {
28 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
29 "build out: cat in | implicit\n"));
31 fs_.Create("out", "");
33 Edge* edge = GetNode("out")->in_edge();
35 EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
38 // A missing implicit dep *should* make the output dirty.
39 // (In fact, a build will fail.)
40 // This is a change from prior semantics of ninja.
41 EXPECT_TRUE(GetNode("out")->dirty());
44 TEST_F(GraphTest, ModifiedImplicit) {
45 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
46 "build out: cat in | implicit\n"));
48 fs_.Create("out", "");
50 fs_.Create("implicit", "");
52 Edge* edge = GetNode("out")->in_edge();
54 EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
57 // A modified implicit dep should make the output dirty.
58 EXPECT_TRUE(GetNode("out")->dirty());
61 TEST_F(GraphTest, FunkyMakefilePath) {
62 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
65 " command = cat $in > $out\n"
66 "build out.o: catdep foo.cc\n"));
67 fs_.Create("foo.cc", "");
68 fs_.Create("out.o.d", "out.o: ./foo/../implicit.h\n");
69 fs_.Create("out.o", "");
71 fs_.Create("implicit.h", "");
73 Edge* edge = GetNode("out.o")->in_edge();
75 EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
78 // implicit.h has changed, though our depfile refers to it with a
79 // non-canonical path; we should still find it.
80 EXPECT_TRUE(GetNode("out.o")->dirty());
83 TEST_F(GraphTest, ExplicitImplicit) {
84 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
87 " command = cat $in > $out\n"
88 "build implicit.h: cat data\n"
89 "build out.o: catdep foo.cc || implicit.h\n"));
90 fs_.Create("implicit.h", "");
91 fs_.Create("foo.cc", "");
92 fs_.Create("out.o.d", "out.o: implicit.h\n");
93 fs_.Create("out.o", "");
95 fs_.Create("data", "");
97 Edge* edge = GetNode("out.o")->in_edge();
99 EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
102 // We have both an implicit and an explicit dep on implicit.h.
103 // The implicit dep should "win" (in the sense that it should cause
104 // the output to be dirty).
105 EXPECT_TRUE(GetNode("out.o")->dirty());
108 TEST_F(GraphTest, PathWithCurrentDirectory) {
109 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
111 " depfile = $out.d\n"
112 " command = cat $in > $out\n"
113 "build ./out.o: catdep ./foo.cc\n"));
114 fs_.Create("foo.cc", "");
115 fs_.Create("out.o.d", "out.o: foo.cc\n");
116 fs_.Create("out.o", "");
118 Edge* edge = GetNode("out.o")->in_edge();
120 EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
123 EXPECT_FALSE(GetNode("out.o")->dirty());
126 TEST_F(GraphTest, RootNodes) {
127 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
128 "build out1: cat in1\n"
129 "build mid1: cat in1\n"
130 "build out2: cat mid1\n"
131 "build out3 out4: cat mid1\n"));
134 vector<Node*> root_nodes = state_.RootNodes(&err);
135 EXPECT_EQ(4u, root_nodes.size());
136 for (size_t i = 0; i < root_nodes.size(); ++i) {
137 string name = root_nodes[i]->path();
138 EXPECT_EQ("out", name.substr(0, 3));
142 TEST_F(GraphTest, VarInOutPathEscaping) {
143 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
144 "build a$ b: cat no'space with$ space$$ no\"space2\n"));
146 Edge* edge = GetNode("a b")->in_edge();
148 EXPECT_EQ("cat no'space \"with space$\" \"no\\\"space2\" > \"a b\"",
149 edge->EvaluateCommand());
151 EXPECT_EQ("cat 'no'\\''space' 'with space$' 'no\"space2' > 'a b'",
152 edge->EvaluateCommand());
156 // Regression test for https://github.com/martine/ninja/issues/380
157 TEST_F(GraphTest, DepfileWithCanonicalizablePath) {
158 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
160 " depfile = $out.d\n"
161 " command = cat $in > $out\n"
162 "build ./out.o: catdep ./foo.cc\n"));
163 fs_.Create("foo.cc", "");
164 fs_.Create("out.o.d", "out.o: bar/../foo.cc\n");
165 fs_.Create("out.o", "");
167 Edge* edge = GetNode("out.o")->in_edge();
169 EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
172 EXPECT_FALSE(GetNode("out.o")->dirty());
175 // Regression test for https://github.com/martine/ninja/issues/404
176 TEST_F(GraphTest, DepfileRemoved) {
177 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
179 " depfile = $out.d\n"
180 " command = cat $in > $out\n"
181 "build ./out.o: catdep ./foo.cc\n"));
182 fs_.Create("foo.h", "");
183 fs_.Create("foo.cc", "");
185 fs_.Create("out.o.d", "out.o: foo.h\n");
186 fs_.Create("out.o", "");
188 Edge* edge = GetNode("out.o")->in_edge();
190 EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
192 EXPECT_FALSE(GetNode("out.o")->dirty());
195 fs_.RemoveFile("out.o.d");
196 EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
198 EXPECT_TRUE(GetNode("out.o")->dirty());
201 // Check that rule-level variables are in scope for eval.
202 TEST_F(GraphTest, RuleVariablesInScope) {
203 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
206 " command = depfile is $depfile\n"
207 "build out: r in\n"));
208 Edge* edge = GetNode("out")->in_edge();
209 EXPECT_EQ("depfile is x", edge->EvaluateCommand());
212 // Check that build statements can override rule builtins like depfile.
213 TEST_F(GraphTest, DepfileOverride) {
214 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
217 " command = unused\n"
220 Edge* edge = GetNode("out")->in_edge();
221 EXPECT_EQ("y", edge->GetBinding("depfile"));
224 // Check that overridden values show up in expansion of rule-level bindings.
225 TEST_F(GraphTest, DepfileOverrideParent) {
226 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
229 " command = depfile is $depfile\n"
232 Edge* edge = GetNode("out")->in_edge();
233 EXPECT_EQ("depfile is y", edge->GetBinding("command"));
236 // Verify that building a nested phony rule prints "no work to do"
237 TEST_F(GraphTest, NestedPhonyPrintsDone) {
240 "build n2: phony n1\n"
243 Edge* edge = GetNode("n2")->in_edge();
244 EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
248 EXPECT_TRUE(plan_.AddTarget(GetNode("n2"), &err));
251 EXPECT_EQ(0, plan_.command_edge_count());
252 ASSERT_FALSE(plan_.more_to_do());
256 TEST_F(GraphTest, Decanonicalize) {
257 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
258 "build out\\out1: cat src\\in1\n"
259 "build out\\out2/out3\\out4: cat mid1\n"
260 "build out3 out4\\foo: cat mid1\n"));
263 vector<Node*> root_nodes = state_.RootNodes(&err);
264 EXPECT_EQ(4u, root_nodes.size());
265 EXPECT_EQ(root_nodes[0]->path(), "out/out1");
266 EXPECT_EQ(root_nodes[1]->path(), "out/out2/out3/out4");
267 EXPECT_EQ(root_nodes[2]->path(), "out3");
268 EXPECT_EQ(root_nodes[3]->path(), "out4/foo");
269 EXPECT_EQ(root_nodes[0]->PathDecanonicalized(), "out\\out1");
270 EXPECT_EQ(root_nodes[1]->PathDecanonicalized(), "out\\out2/out3\\out4");
271 EXPECT_EQ(root_nodes[2]->PathDecanonicalized(), "out3");
272 EXPECT_EQ(root_nodes[3]->PathDecanonicalized(), "out4\\foo");