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.
19 struct GraphTest : public StateTestWithBuiltinRules {
20 VirtualFileSystem fs_;
23 TEST_F(GraphTest, MissingImplicit) {
24 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
25 "build out: cat in | implicit\n"));
26 fs_.Create("in", 1, "");
27 fs_.Create("out", 1, "");
29 Edge* edge = GetNode("out")->in_edge();
31 EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
34 // A missing implicit dep *should* make the output dirty.
35 // (In fact, a build will fail.)
36 // This is a change from prior semantics of ninja.
37 EXPECT_TRUE(GetNode("out")->dirty());
40 TEST_F(GraphTest, ModifiedImplicit) {
41 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
42 "build out: cat in | implicit\n"));
43 fs_.Create("in", 1, "");
44 fs_.Create("out", 1, "");
45 fs_.Create("implicit", 2, "");
47 Edge* edge = GetNode("out")->in_edge();
49 EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
52 // A modified implicit dep should make the output dirty.
53 EXPECT_TRUE(GetNode("out")->dirty());
56 TEST_F(GraphTest, FunkyMakefilePath) {
57 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
60 " command = cat $in > $out\n"
61 "build out.o: catdep foo.cc\n"));
62 fs_.Create("implicit.h", 2, "");
63 fs_.Create("foo.cc", 1, "");
64 fs_.Create("out.o.d", 1, "out.o: ./foo/../implicit.h\n");
65 fs_.Create("out.o", 1, "");
67 Edge* edge = GetNode("out.o")->in_edge();
69 EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
72 // implicit.h has changed, though our depfile refers to it with a
73 // non-canonical path; we should still find it.
74 EXPECT_TRUE(GetNode("out.o")->dirty());
77 TEST_F(GraphTest, ExplicitImplicit) {
78 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
81 " command = cat $in > $out\n"
82 "build implicit.h: cat data\n"
83 "build out.o: catdep foo.cc || implicit.h\n"));
84 fs_.Create("data", 2, "");
85 fs_.Create("implicit.h", 1, "");
86 fs_.Create("foo.cc", 1, "");
87 fs_.Create("out.o.d", 1, "out.o: implicit.h\n");
88 fs_.Create("out.o", 1, "");
90 Edge* edge = GetNode("out.o")->in_edge();
92 EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
95 // We have both an implicit and an explicit dep on implicit.h.
96 // The implicit dep should "win" (in the sense that it should cause
97 // the output to be dirty).
98 EXPECT_TRUE(GetNode("out.o")->dirty());
101 TEST_F(GraphTest, PathWithCurrentDirectory) {
102 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
104 " depfile = $out.d\n"
105 " command = cat $in > $out\n"
106 "build ./out.o: catdep ./foo.cc\n"));
107 fs_.Create("foo.cc", 1, "");
108 fs_.Create("out.o.d", 1, "out.o: foo.cc\n");
109 fs_.Create("out.o", 1, "");
111 Edge* edge = GetNode("out.o")->in_edge();
113 EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
116 EXPECT_FALSE(GetNode("out.o")->dirty());
119 TEST_F(GraphTest, RootNodes) {
120 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
121 "build out1: cat in1\n"
122 "build mid1: cat in1\n"
123 "build out2: cat mid1\n"
124 "build out3 out4: cat mid1\n"));
127 vector<Node*> root_nodes = state_.RootNodes(&err);
128 EXPECT_EQ(4u, root_nodes.size());
129 for (size_t i = 0; i < root_nodes.size(); ++i) {
130 string name = root_nodes[i]->path();
131 EXPECT_EQ("out", name.substr(0, 3));
135 TEST_F(GraphTest, VarInOutQuoteSpaces) {
136 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
137 "build a$ b: cat nospace with$ space nospace2\n"));
139 Edge* edge = GetNode("a b")->in_edge();
140 EXPECT_EQ("cat nospace \"with space\" nospace2 > \"a b\"",
141 edge->EvaluateCommand());
144 // Regression test for https://github.com/martine/ninja/issues/380
145 TEST_F(GraphTest, DepfileWithCanonicalizablePath) {
146 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
148 " depfile = $out.d\n"
149 " command = cat $in > $out\n"
150 "build ./out.o: catdep ./foo.cc\n"));
151 fs_.Create("foo.cc", 1, "");
152 fs_.Create("out.o.d", 1, "out.o: bar/../foo.cc\n");
153 fs_.Create("out.o", 1, "");
155 Edge* edge = GetNode("out.o")->in_edge();
157 EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
160 EXPECT_FALSE(GetNode("out.o")->dirty());
163 // Regression test for https://github.com/martine/ninja/issues/404
164 TEST_F(GraphTest, DepfileRemoved) {
165 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
167 " depfile = $out.d\n"
168 " command = cat $in > $out\n"
169 "build ./out.o: catdep ./foo.cc\n"));
170 fs_.Create("foo.h", 1, "");
171 fs_.Create("foo.cc", 1, "");
172 fs_.Create("out.o.d", 2, "out.o: foo.h\n");
173 fs_.Create("out.o", 2, "");
175 Edge* edge = GetNode("out.o")->in_edge();
177 EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
179 EXPECT_FALSE(GetNode("out.o")->dirty());
182 fs_.RemoveFile("out.o.d");
183 EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
185 EXPECT_TRUE(GetNode("out.o")->dirty());