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 "gn/functions.h"
6 #include "gn/parse_tree.h"
7 #include "gn/test_with_scope.h"
8 #include "util/build_config.h"
9 #include "util/test/test.h"
10
11 namespace {
12
RebaseOne(Scope * scope,const char * input,const char * to_dir,const char * from_dir)13 std::string RebaseOne(Scope* scope,
14 const char* input,
15 const char* to_dir,
16 const char* from_dir) {
17 std::vector<Value> args;
18 args.push_back(Value(nullptr, input));
19 args.push_back(Value(nullptr, to_dir));
20 args.push_back(Value(nullptr, from_dir));
21
22 Err err;
23 FunctionCallNode function;
24 Value result = functions::RunRebasePath(scope, &function, args, &err);
25 bool is_string = result.type() == Value::STRING;
26 EXPECT_TRUE(is_string);
27
28 return result.string_value();
29 }
30
31 } // namespace
32
TEST(RebasePath,Strings)33 TEST(RebasePath, Strings) {
34 TestWithScope setup;
35 Scope* scope = setup.scope();
36 scope->set_source_dir(SourceDir("//tools/gn/"));
37
38 // Build-file relative paths.
39 EXPECT_EQ("../../tools/gn", RebaseOne(scope, ".", "//out/Debug", "."));
40 EXPECT_EQ("../../tools/gn/", RebaseOne(scope, "./", "//out/Debug", "."));
41 EXPECT_EQ("../../tools/gn/foo", RebaseOne(scope, "foo", "//out/Debug", "."));
42 EXPECT_EQ("../..", RebaseOne(scope, "../..", "//out/Debug", "."));
43 EXPECT_EQ("../../", RebaseOne(scope, "../../", "//out/Debug", "."));
44
45 // Without a source root defined, we cannot move out of the source tree.
46 EXPECT_EQ("../..", RebaseOne(scope, "../../..", "//out/Debug", "."));
47
48 // Source-absolute input paths.
49 EXPECT_EQ("./", RebaseOne(scope, "//", "//", "//"));
50 EXPECT_EQ("foo", RebaseOne(scope, "//foo", "//", "//"));
51 EXPECT_EQ("foo/", RebaseOne(scope, "//foo/", "//", "//"));
52 EXPECT_EQ("../../foo/bar", RebaseOne(scope, "//foo/bar", "//out/Debug", "."));
53 EXPECT_EQ("./", RebaseOne(scope, "//foo/", "//foo/", "//"));
54 EXPECT_EQ(".", RebaseOne(scope, "//foo", "//foo", "//"));
55
56 // Test slash conversion.
57 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo/bar", ".", "."));
58 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo\\bar", ".", "."));
59
60 // Test system path output.
61 #if defined(OS_WIN)
62 setup.build_settings()->SetRootPath(base::FilePath(u"C:/path/to/src"));
63 EXPECT_EQ("C:/path/to/src", RebaseOne(scope, ".", "", "//"));
64 EXPECT_EQ("C:/path/to/src/", RebaseOne(scope, "//", "", "//"));
65 EXPECT_EQ("C:/path/to/src/foo", RebaseOne(scope, "foo", "", "//"));
66 EXPECT_EQ("C:/path/to/src/foo/", RebaseOne(scope, "foo/", "", "//"));
67 EXPECT_EQ("C:/path/to/src/tools/gn/foo", RebaseOne(scope, "foo", "", "."));
68 EXPECT_EQ("C:/path/to/other/tools",
69 RebaseOne(scope, "//../other/tools", "", "//"));
70 EXPECT_EQ("C:/path/to/src/foo/bar",
71 RebaseOne(scope, "//../src/foo/bar", "", "//"));
72 EXPECT_EQ("C:/path/to", RebaseOne(scope, "//..", "", "//"));
73 EXPECT_EQ("C:/path", RebaseOne(scope, "../../../..", "", "."));
74 EXPECT_EQ("C:/path/to/external/dir/",
75 RebaseOne(scope, "//../external/dir/", "", "//"));
76
77 #else
78 setup.build_settings()->SetRootPath(base::FilePath("/path/to/src"));
79 EXPECT_EQ("/path/to/src", RebaseOne(scope, ".", "", "//"));
80 EXPECT_EQ("/path/to/src/", RebaseOne(scope, "//", "", "//"));
81 EXPECT_EQ("/path/to/src/foo", RebaseOne(scope, "foo", "", "//"));
82 EXPECT_EQ("/path/to/src/foo/", RebaseOne(scope, "foo/", "", "//"));
83 EXPECT_EQ("/path/to/src/tools/gn/foo", RebaseOne(scope, "foo", "", "."));
84 EXPECT_EQ("/path/to/other/tools",
85 RebaseOne(scope, "//../other/tools", "", "//"));
86 EXPECT_EQ("/path/to/src/foo/bar",
87 RebaseOne(scope, "//../src/foo/bar", "", "//"));
88 EXPECT_EQ("/path/to", RebaseOne(scope, "//..", "", "//"));
89 EXPECT_EQ("/path", RebaseOne(scope, "../../../..", "", "."));
90 EXPECT_EQ("/path/to/external/dir/",
91 RebaseOne(scope, "//../external/dir/", "", "//"));
92 #endif
93 }
94
TEST(RebasePath,StringsSystemPaths)95 TEST(RebasePath, StringsSystemPaths) {
96 TestWithScope setup;
97 Scope* scope = setup.scope();
98
99 #if defined(OS_WIN)
100 setup.build_settings()->SetBuildDir(SourceDir("C:/ssd/out/Debug"));
101 setup.build_settings()->SetRootPath(base::FilePath(u"C:/hdd/src"));
102
103 // Test system absolute to-dir.
104 EXPECT_EQ("../../ssd/out/Debug",
105 RebaseOne(scope, ".", "//", "C:/ssd/out/Debug"));
106 EXPECT_EQ("../../ssd/out/Debug/",
107 RebaseOne(scope, "./", "//", "C:/ssd/out/Debug"));
108 EXPECT_EQ("../../ssd/out/Debug/foo",
109 RebaseOne(scope, "foo", "//", "C:/ssd/out/Debug"));
110 EXPECT_EQ("../../ssd/out/Debug/foo/",
111 RebaseOne(scope, "foo/", "//", "C:/ssd/out/Debug"));
112
113 // Test system absolute from-dir.
114 EXPECT_EQ("../../../hdd/src",
115 RebaseOne(scope, ".", "C:/ssd/out/Debug", "//"));
116 EXPECT_EQ("../../../hdd/src/",
117 RebaseOne(scope, "./", "C:/ssd/out/Debug", "//"));
118 EXPECT_EQ("../../../hdd/src/foo",
119 RebaseOne(scope, "foo", "C:/ssd/out/Debug", "//"));
120 EXPECT_EQ("../../../hdd/src/foo/",
121 RebaseOne(scope, "foo/", "C:/ssd/out/Debug", "//"));
122 #else
123 setup.build_settings()->SetBuildDir(SourceDir("/ssd/out/Debug"));
124 setup.build_settings()->SetRootPath(base::FilePath("/ssd/out/Debug"));
125 EXPECT_EQ("../Debug-suffix/a", RebaseOne(scope, "/ssd/out/Debug-suffix/a",
126 "/ssd/out/Debug", "/ssd/out/Debug"));
127 setup.build_settings()->SetRootPath(base::FilePath("/hdd/src"));
128
129 // Test system absolute to-dir.
130 EXPECT_EQ("../../ssd/out/Debug",
131 RebaseOne(scope, ".", "//", "/ssd/out/Debug"));
132 EXPECT_EQ("../../ssd/out/Debug/",
133 RebaseOne(scope, "./", "//", "/ssd/out/Debug"));
134 EXPECT_EQ("../../ssd/out/Debug/foo",
135 RebaseOne(scope, "foo", "//", "/ssd/out/Debug"));
136 EXPECT_EQ("../../ssd/out/Debug/foo/",
137 RebaseOne(scope, "foo/", "//", "/ssd/out/Debug"));
138
139 // Test system absolute from-dir.
140 EXPECT_EQ("../../../hdd/src", RebaseOne(scope, ".", "/ssd/out/Debug", "//"));
141 EXPECT_EQ("../../../hdd/src/",
142 RebaseOne(scope, "./", "/ssd/out/Debug", "//"));
143 EXPECT_EQ("../../../hdd/src/foo",
144 RebaseOne(scope, "foo", "/ssd/out/Debug", "//"));
145 EXPECT_EQ("../../../hdd/src/foo/",
146 RebaseOne(scope, "foo/", "/ssd/out/Debug", "//"));
147 #endif
148 }
149
150 // Test list input.
TEST(RebasePath,List)151 TEST(RebasePath, List) {
152 TestWithScope setup;
153 setup.scope()->set_source_dir(SourceDir("//gn/"));
154
155 std::vector<Value> args;
156 args.push_back(Value(nullptr, Value::LIST));
157 args[0].list_value().push_back(Value(nullptr, "foo.txt"));
158 args[0].list_value().push_back(Value(nullptr, "bar.txt"));
159 args.push_back(Value(nullptr, "//out/Debug/"));
160 args.push_back(Value(nullptr, "."));
161
162 Err err;
163 FunctionCallNode function;
164 Value ret = functions::RunRebasePath(setup.scope(), &function, args, &err);
165 EXPECT_FALSE(err.has_error());
166
167 ASSERT_EQ(Value::LIST, ret.type());
168 ASSERT_EQ(2u, ret.list_value().size());
169
170 EXPECT_EQ("../../gn/foo.txt", ret.list_value()[0].string_value());
171 EXPECT_EQ("../../gn/bar.txt", ret.list_value()[1].string_value());
172 }
173
TEST(RebasePath,Errors)174 TEST(RebasePath, Errors) {
175 TestWithScope setup;
176
177 // No arg input should issue an error.
178 Err err;
179 std::vector<Value> args;
180 FunctionCallNode function;
181 Value ret = functions::RunRebasePath(setup.scope(), &function, args, &err);
182 EXPECT_TRUE(err.has_error());
183 }
184