windows: successfully link tests
[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 does not make the output dirty.
35   EXPECT_FALSE(GetNode("out")->dirty_);
36 }
37
38 TEST_F(GraphTest, FunkyMakefilePath) {
39   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
40 "rule catdep\n"
41 "  depfile = $out.d\n"
42 "  command = cat $in > $out\n"
43 "build out.o: catdep foo.cc\n"));
44   fs_.Create("implicit.h", 2, "");
45   fs_.Create("foo.cc", 1, "");
46   fs_.Create("out.o.d", 1, "out.o: ./foo/../implicit.h\n");
47   fs_.Create("out.o", 1, "");
48
49   Edge* edge = GetNode("out.o")->in_edge_;
50   string err;
51   EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
52   ASSERT_EQ("", err);
53
54   // implicit.h has changed, though our depfile refers to it with a
55   // non-canonical path; we should still find it.
56   EXPECT_TRUE(GetNode("out.o")->dirty_);
57 }
58
59 TEST_F(GraphTest, ExplicitImplicit) {
60   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
61 "rule catdep\n"
62 "  depfile = $out.d\n"
63 "  command = cat $in > $out\n"
64 "build implicit.h: cat data\n"
65 "build out.o: catdep foo.cc || implicit.h\n"));
66   fs_.Create("data", 2, "");
67   fs_.Create("implicit.h", 1, "");
68   fs_.Create("foo.cc", 1, "");
69   fs_.Create("out.o.d", 1, "out.o: implicit.h\n");
70   fs_.Create("out.o", 1, "");
71
72   Edge* edge = GetNode("out.o")->in_edge_;
73   string err;
74   EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
75   ASSERT_EQ("", err);
76
77   // We have both an implicit and an explicit dep on implicit.h.
78   // The implicit dep should "win" (in the sense that it should cause
79   // the output to be dirty).
80   EXPECT_TRUE(GetNode("out.o")->dirty_);
81 }
82
83 TEST_F(GraphTest, PathWithCurrentDirectory) {
84   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
85 "rule catdep\n"
86 "  depfile = $out.d\n"
87 "  command = cat $in > $out\n"
88 "build ./out.o: catdep ./foo.cc\n"));
89   fs_.Create("foo.cc", 1, "");
90   fs_.Create("out.o.d", 1, "out.o: foo.cc\n");
91   fs_.Create("out.o", 1, "");
92
93   Edge* edge = GetNode("out.o")->in_edge_;
94   string err;
95   EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
96   ASSERT_EQ("", err);
97
98   EXPECT_FALSE(GetNode("out.o")->dirty_);
99 }
100
101 TEST_F(GraphTest, RootNodes) {
102   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
103 "build out1: cat in1\n"
104 "build mid1: cat in1\n"
105 "build out2: cat mid1\n"
106 "build out3 out4: cat mid1\n"));
107
108   string err;
109   vector<Node*> root_nodes = state_.RootNodes(&err);
110   EXPECT_EQ(4u, root_nodes.size());
111   for (size_t i = 0; i < root_nodes.size(); ++i) {
112     string name = root_nodes[i]->file_->path_;
113     EXPECT_EQ("out", name.substr(0, 3));
114   }
115 }