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, VarInOutQuoteSpaces) {
143 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
144 "build a$ b: cat nospace with$ space nospace2\n"));
146 Edge* edge = GetNode("a b")->in_edge();
147 EXPECT_EQ("cat nospace \"with space\" nospace2 > \"a b\"",
148 edge->EvaluateCommand());
151 // Regression test for https://github.com/martine/ninja/issues/380
152 TEST_F(GraphTest, DepfileWithCanonicalizablePath) {
153 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
155 " depfile = $out.d\n"
156 " command = cat $in > $out\n"
157 "build ./out.o: catdep ./foo.cc\n"));
158 fs_.Create("foo.cc", "");
159 fs_.Create("out.o.d", "out.o: bar/../foo.cc\n");
160 fs_.Create("out.o", "");
162 Edge* edge = GetNode("out.o")->in_edge();
164 EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
167 EXPECT_FALSE(GetNode("out.o")->dirty());
170 // Regression test for https://github.com/martine/ninja/issues/404
171 TEST_F(GraphTest, DepfileRemoved) {
172 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
174 " depfile = $out.d\n"
175 " command = cat $in > $out\n"
176 "build ./out.o: catdep ./foo.cc\n"));
177 fs_.Create("foo.h", "");
178 fs_.Create("foo.cc", "");
180 fs_.Create("out.o.d", "out.o: foo.h\n");
181 fs_.Create("out.o", "");
183 Edge* edge = GetNode("out.o")->in_edge();
185 EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
187 EXPECT_FALSE(GetNode("out.o")->dirty());
190 fs_.RemoveFile("out.o.d");
191 EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
193 EXPECT_TRUE(GetNode("out.o")->dirty());
196 // Check that rule-level variables are in scope for eval.
197 TEST_F(GraphTest, RuleVariablesInScope) {
198 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
201 " command = depfile is $depfile\n"
202 "build out: r in\n"));
203 Edge* edge = GetNode("out")->in_edge();
204 EXPECT_EQ("depfile is x", edge->EvaluateCommand());
207 // Check that build statements can override rule builtins like depfile.
208 TEST_F(GraphTest, DepfileOverride) {
209 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
212 " command = unused\n"
215 Edge* edge = GetNode("out")->in_edge();
216 EXPECT_EQ("y", edge->GetBinding("depfile"));
219 // Check that overridden values show up in expansion of rule-level bindings.
220 TEST_F(GraphTest, DepfileOverrideParent) {
221 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
224 " command = depfile is $depfile\n"
227 Edge* edge = GetNode("out")->in_edge();
228 EXPECT_EQ("depfile is y", edge->GetBinding("command"));
231 // Verify that building a nested phony rule prints "no work to do"
232 TEST_F(GraphTest, NestedPhonyPrintsDone) {
235 "build n2: phony n1\n"
238 Edge* edge = GetNode("n2")->in_edge();
239 EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
243 EXPECT_TRUE(plan_.AddTarget(GetNode("n2"), &err));
246 EXPECT_EQ(0, plan_.command_edge_count());
247 ASSERT_FALSE(plan_.more_to_do());