canonicalize paths loaded from depfiles
[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 TEST(CanonicalizePath, PathSamples) {
20   string path = "foo.h";
21   string err;
22   EXPECT_TRUE(CanonicalizePath(&path, &err));
23   EXPECT_EQ("", err);
24   EXPECT_EQ("foo.h", path);
25
26   path = "./foo.h"; err = "";
27   EXPECT_TRUE(CanonicalizePath(&path, &err));
28   EXPECT_EQ("", err);
29   EXPECT_EQ("foo.h", path);
30
31   path = "./foo/./bar.h"; err = "";
32   EXPECT_TRUE(CanonicalizePath(&path, &err));
33   EXPECT_EQ("", err);
34   EXPECT_EQ("foo/bar.h", path);
35
36   path = "./x/foo/../bar.h"; err = "";
37   EXPECT_TRUE(CanonicalizePath(&path, &err));
38   EXPECT_EQ("", err);
39   EXPECT_EQ("x/bar.h", path);
40
41   path = "./x/foo/../../bar.h"; err = "";
42   EXPECT_TRUE(CanonicalizePath(&path, &err));
43   EXPECT_EQ("", err);
44   EXPECT_EQ("bar.h", path);
45
46   path = "./x/../foo/../../bar.h"; err = "";
47   EXPECT_FALSE(CanonicalizePath(&path, &err));
48   EXPECT_EQ("can't canonicalize path './x/../foo/../../bar.h' that reaches "
49             "above its directory", err);
50 }
51
52 struct GraphTest : public StateTestWithBuiltinRules {
53   VirtualFileSystem fs_;
54 };
55
56 TEST_F(GraphTest, MissingImplicit) {
57   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
58 "build out: cat in | implicit\n"));
59   fs_.Create("in", 1, "");
60   fs_.Create("out", 1, "");
61
62   Edge* edge = GetNode("out")->in_edge_;
63   string err;
64   EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
65   ASSERT_EQ("", err);
66
67   // A missing implicit dep does not make the output dirty.
68   EXPECT_FALSE(GetNode("out")->dirty_);
69 }
70
71 TEST_F(GraphTest, FunkyMakefilePath) {
72   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
73 "rule catdep\n"
74 "  depfile = $out.d\n"
75 "  command = cat $in > $out\n"
76 "build out.o: catdep foo.cc\n"));
77   fs_.Create("implicit.h", 2, "");
78   fs_.Create("foo.cc", 1, "");
79   fs_.Create("out.o.d", 1, "out.o: ./foo/../implicit.h\n");
80   fs_.Create("out.o", 1, "");
81
82   Edge* edge = GetNode("out.o")->in_edge_;
83   string err;
84   EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
85   ASSERT_EQ("", err);
86
87   // implicit.h has changed, though our depfile refers to it with a
88   // non-canonical path; we should still find it.
89   EXPECT_TRUE(GetNode("out.o")->dirty_);
90 }