• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <stddef.h>
6 
7 #include <iterator>
8 
9 #include "gn/err.h"
10 #include "gn/label.h"
11 #include "gn/value.h"
12 #include "util/build_config.h"
13 #include "util/test/test.h"
14 
15 namespace {
16 
17 struct ParseDepStringCase {
18   const char* cur_dir;
19   const char* str;
20   bool success;
21   const char* expected_dir;
22   const char* expected_name;
23   const char* expected_toolchain_dir;
24   const char* expected_toolchain_name;
25 };
26 
27 }  // namespace
28 
TEST(Label,Resolve)29 TEST(Label, Resolve) {
30   ParseDepStringCase cases[] = {
31       {"//chrome/", "", false, "", "", "", ""},
32       {"//chrome/", "/", false, "", "", "", ""},
33       {"//chrome/", ":", false, "", "", "", ""},
34       {"//chrome/", "/:", false, "", "", "", ""},
35       {"//chrome/", "blah", true, "//chrome/blah/", "blah", "//t/", "d"},
36       {"//chrome/", "blah:bar", true, "//chrome/blah/", "bar", "//t/", "d"},
37       // Absolute paths.
38       {"//chrome/", "/chrome:bar", true, "/chrome/", "bar", "//t/", "d"},
39       {"//chrome/", "/chrome/:bar", true, "/chrome/", "bar", "//t/", "d"},
40 #if defined(OS_WIN)
41       {"//chrome/", "/C:/chrome:bar", true, "/C:/chrome/", "bar", "//t/", "d"},
42       {"//chrome/", "/C:/chrome/:bar", true, "/C:/chrome/", "bar", "//t/", "d"},
43       {"//chrome/", "C:/chrome:bar", true, "/C:/chrome/", "bar", "//t/", "d"},
44 #endif
45       // Refers to root dir.
46       {"//chrome/", "//:bar", true, "//", "bar", "//t/", "d"},
47       // Implicit directory
48       {"//chrome/", ":bar", true, "//chrome/", "bar", "//t/", "d"},
49       {"//chrome/renderer/", ":bar", true, "//chrome/renderer/", "bar", "//t/",
50        "d"},
51       // Implicit names.
52       {"//chrome/", "//base", true, "//base/", "base", "//t/", "d"},
53       {"//chrome/", "//base/i18n", true, "//base/i18n/", "i18n", "//t/", "d"},
54       {"//chrome/", "//base/i18n:foo", true, "//base/i18n/", "foo", "//t/",
55        "d"},
56       {"//chrome/", "//", false, "", "", "", ""},
57       // Toolchain parsing.
58       {"//chrome/", "//chrome:bar(//t:n)", true, "//chrome/", "bar", "//t/",
59        "n"},
60       {"//chrome/", "//chrome:bar(//t)", true, "//chrome/", "bar", "//t/", "t"},
61       {"//chrome/", "//chrome:bar(//t:)", true, "//chrome/", "bar", "//t/",
62        "t"},
63       {"//chrome/", "//chrome:bar()", true, "//chrome/", "bar", "//t/", "d"},
64       {"//chrome/", "//chrome:bar(foo)", true, "//chrome/", "bar",
65        "//chrome/foo/", "foo"},
66       {"//chrome/", "//chrome:bar(:foo)", true, "//chrome/", "bar", "//chrome/",
67        "foo"},
68       // TODO(brettw) it might be nice to make this an error:
69       //{"//chrome/", "//chrome:bar())", false, "", "", "", "" },
70       {"//chrome/", "//chrome:bar(//t:bar(tc))", false, "", "", "", ""},
71       {"//chrome/", "//chrome:bar(()", false, "", "", "", ""},
72       {"//chrome/", "(t:b)", false, "", "", "", ""},
73       {"//chrome/", ":bar(//t/b)", true, "//chrome/", "bar", "//t/b/", "b"},
74       {"//chrome/", ":bar(/t/b)", true, "//chrome/", "bar", "/t/b/", "b"},
75       {"//chrome/", ":bar(t/b)", true, "//chrome/", "bar", "//chrome/t/b/",
76        "b"},
77   };
78 
79   Label default_toolchain(SourceDir("//t/"), "d");
80 
81   for (size_t i = 0; i < std::size(cases); i++) {
82     const ParseDepStringCase& cur = cases[i];
83 
84     std::string location, name;
85     Err err;
86     Value v(nullptr, Value::STRING);
87     v.string_value() = cur.str;
88     Label result = Label::Resolve(SourceDir(cur.cur_dir), std::string_view(),
89                                   default_toolchain, v, &err);
90     EXPECT_EQ(cur.success, !err.has_error()) << i << " " << cur.str;
91     if (!err.has_error() && cur.success) {
92       EXPECT_EQ(cur.expected_dir, result.dir().value()) << i << " " << cur.str;
93       EXPECT_EQ(cur.expected_name, result.name()) << i << " " << cur.str;
94       EXPECT_EQ(cur.expected_toolchain_dir, result.toolchain_dir().value())
95           << i << " " << cur.str;
96       EXPECT_EQ(cur.expected_toolchain_name, result.toolchain_name())
97           << i << " " << cur.str;
98     }
99   }
100 }
101 
102 // Tests the case where the path resolves to something above "//". It should get
103 // converted to an absolute path "/foo/bar".
TEST(Label,ResolveAboveRootBuildDir)104 TEST(Label, ResolveAboveRootBuildDir) {
105   Label default_toolchain(SourceDir("//t/"), "d");
106 
107   std::string location, name;
108   Err err;
109 
110   SourceDir cur_dir("//cur/");
111   std::string source_root("/foo/bar/baz");
112 
113   // No source root given, should not go above the root build dir.
114   Label result = Label::Resolve(cur_dir, std::string_view(), default_toolchain,
115                                 Value(nullptr, "../../..:target"), &err);
116   EXPECT_FALSE(err.has_error());
117   EXPECT_EQ("//", result.dir().value()) << result.dir().value();
118   EXPECT_EQ("target", result.name());
119 
120   // Source root provided, it should go into that.
121   result = Label::Resolve(cur_dir, source_root, default_toolchain,
122                           Value(nullptr, "../../..:target"), &err);
123   EXPECT_FALSE(err.has_error());
124   EXPECT_EQ("/foo/", result.dir().value()) << result.dir().value();
125   EXPECT_EQ("target", result.name());
126 
127   // It shouldn't go up higher than the system root.
128   result = Label::Resolve(cur_dir, source_root, default_toolchain,
129                           Value(nullptr, "../../../../..:target"), &err);
130   EXPECT_FALSE(err.has_error());
131   EXPECT_EQ("/", result.dir().value()) << result.dir().value();
132   EXPECT_EQ("target", result.name());
133 
134   // Test an absolute label that goes above the source root. This currently
135   // stops at the source root. It should arguably keep going and produce "/foo/"
136   // but this test just makes sure the current behavior isn't regressed by
137   // accident.
138   result = Label::Resolve(cur_dir, source_root, default_toolchain,
139                           Value(nullptr, "//../.."), &err);
140   EXPECT_FALSE(err.has_error()) << err.message();
141   EXPECT_EQ("/foo/", result.dir().value()) << result.dir().value();
142   EXPECT_EQ("foo", result.name());
143 }
144