Merge pull request #175 from nornagon/escape-in-out
[platform/upstream/ninja.git] / src / graph_test.cc
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
17 #include "test.h"
18
19 struct GraphTest : public StateTestWithBuiltinRules {
20   VirtualFileSystem fs_;
21 };
22
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, "");
28
29   Edge* edge = GetNode("out")->in_edge();
30   string err;
31   EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
32   ASSERT_EQ("", err);
33
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());
38 }
39
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, "");
46
47   Edge* edge = GetNode("out")->in_edge();
48   string err;
49   EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
50   ASSERT_EQ("", err);
51
52   // A modified implicit dep should make the output dirty.
53   EXPECT_TRUE(GetNode("out")->dirty());
54 }
55
56 TEST_F(GraphTest, FunkyMakefilePath) {
57   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
58 "rule catdep\n"
59 "  depfile = $out.d\n"
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, "");
66
67   Edge* edge = GetNode("out.o")->in_edge();
68   string err;
69   EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
70   ASSERT_EQ("", err);
71
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());
75 }
76
77 TEST_F(GraphTest, ExplicitImplicit) {
78   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
79 "rule catdep\n"
80 "  depfile = $out.d\n"
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, "");
89
90   Edge* edge = GetNode("out.o")->in_edge();
91   string err;
92   EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
93   ASSERT_EQ("", err);
94
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());
99 }
100
101 TEST_F(GraphTest, PathWithCurrentDirectory) {
102   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
103 "rule catdep\n"
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, "");
110
111   Edge* edge = GetNode("out.o")->in_edge();
112   string err;
113   EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
114   ASSERT_EQ("", err);
115
116   EXPECT_FALSE(GetNode("out.o")->dirty());
117 }
118
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"));
125
126   string err;
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));
132   }
133 }
134
135 TEST_F(GraphTest, VarInOutQuoteSpaces) {
136   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
137 "build a$ b: cat nospace with$ space nospace2\n"));
138
139   Edge* edge = GetNode("a b")->in_edge();
140   EXPECT_EQ("cat nospace \"with space\" nospace2 > \"a b\"",
141       edge->EvaluateCommand());
142 }