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