• 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/", "d"},
55     {"//chrome/", "//", false, "", "", "", ""},
56     // Toolchain parsing.
57     {"//chrome/", "//chrome:bar(//t:n)", true, "//chrome/", "bar", "//t/", "n"},
58     {"//chrome/", "//chrome:bar(//t)", true, "//chrome/", "bar", "//t/", "t"},
59     {"//chrome/", "//chrome:bar(//t:)", true, "//chrome/", "bar", "//t/", "t"},
60     {"//chrome/", "//chrome:bar()", true, "//chrome/", "bar", "//t/", "d"},
61     {"//chrome/", "//chrome:bar(foo)", true, "//chrome/", "bar",
62      "//chrome/foo/", "foo"},
63     {"//chrome/", "//chrome:bar(:foo)", true, "//chrome/", "bar", "//chrome/",
64      "foo"},
65     // TODO(brettw) it might be nice to make this an error:
66     //{"//chrome/", "//chrome:bar())", false, "", "", "", "" },
67     {"//chrome/", "//chrome:bar(//t:bar(tc))", false, "", "", "", ""},
68     {"//chrome/", "//chrome:bar(()", false, "", "", "", ""},
69     {"//chrome/", "(t:b)", false, "", "", "", ""},
70     {"//chrome/", ":bar(//t/b)", true, "//chrome/", "bar", "//t/b/", "b"},
71     {"//chrome/", ":bar(/t/b)", true, "//chrome/", "bar", "/t/b/", "b"},
72     {"//chrome/", ":bar(t/b)", true, "//chrome/", "bar", "//chrome/t/b/", "b"},
73   };
74 
75   Label default_toolchain(SourceDir("//t/"), "d");
76 
77   for (size_t i = 0; i < std::size(cases); i++) {
78     const ParseDepStringCase& cur = cases[i];
79 
80     std::string location, name;
81     Err err;
82     Value v(nullptr, Value::STRING);
83     v.string_value() = cur.str;
84     Label result = Label::Resolve(SourceDir(cur.cur_dir), std::string_view(),
85                                   default_toolchain, v, &err);
86     EXPECT_EQ(cur.success, !err.has_error()) << i << " " << cur.str;
87     if (!err.has_error() && cur.success) {
88       EXPECT_EQ(cur.expected_dir, result.dir().value()) << i << " " << cur.str;
89       EXPECT_EQ(cur.expected_name, result.name()) << i << " " << cur.str;
90       EXPECT_EQ(cur.expected_toolchain_dir, result.toolchain_dir().value())
91           << i << " " << cur.str;
92       EXPECT_EQ(cur.expected_toolchain_name, result.toolchain_name())
93           << i << " " << cur.str;
94     }
95   }
96 }
97 
98 // Tests the case where the path resolves to something above "//". It should get
99 // converted to an absolute path "/foo/bar".
TEST(Label,ResolveAboveRootBuildDir)100 TEST(Label, ResolveAboveRootBuildDir) {
101   Label default_toolchain(SourceDir("//t/"), "d");
102 
103   std::string location, name;
104   Err err;
105 
106   SourceDir cur_dir("//cur/");
107   std::string source_root("/foo/bar/baz");
108 
109   // No source root given, should not go above the root build dir.
110   Label result = Label::Resolve(cur_dir, std::string_view(), default_toolchain,
111                                 Value(nullptr, "../../..:target"), &err);
112   EXPECT_FALSE(err.has_error());
113   EXPECT_EQ("//", result.dir().value()) << result.dir().value();
114   EXPECT_EQ("target", result.name());
115 
116   // Source root provided, it should go into that.
117   result = Label::Resolve(cur_dir, source_root, default_toolchain,
118                           Value(nullptr, "../../..:target"), &err);
119   EXPECT_FALSE(err.has_error());
120   EXPECT_EQ("/foo/", result.dir().value()) << result.dir().value();
121   EXPECT_EQ("target", result.name());
122 
123   // It shouldn't go up higher than the system root.
124   result = Label::Resolve(cur_dir, source_root, default_toolchain,
125                           Value(nullptr, "../../../../..:target"), &err);
126   EXPECT_FALSE(err.has_error());
127   EXPECT_EQ("/", result.dir().value()) << result.dir().value();
128   EXPECT_EQ("target", result.name());
129 
130   // Test an absolute label that goes above the source root. This currently
131   // stops at the source root. It should arguably keep going and produce "/foo/"
132   // but this test just makes sure the current behavior isn't regressed by
133   // accident.
134   result = Label::Resolve(cur_dir, source_root, default_toolchain,
135                           Value(nullptr, "//../.."), &err);
136   EXPECT_FALSE(err.has_error()) << err.message();
137   EXPECT_EQ("/foo/", result.dir().value()) << result.dir().value();
138   EXPECT_EQ("foo", result.name());
139 }
140