blob: d89b56f616b4252cf0eb0085403746c3c7961756 [file] [log] [blame]
[email protected]ea3690c2013-09-23 17:59:221// 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 "build/build_config.h"
6#include "testing/gtest/include/gtest/gtest.h"
7#include "tools/gn/functions.h"
8#include "tools/gn/parse_tree.h"
9#include "tools/gn/test_with_scope.h"
10
11namespace {
12
[email protected]c526bbf2013-09-23 18:29:4013std::string RebaseOne(Scope* scope,
[email protected]ea3690c2013-09-23 17:59:2214 const char* input,
[email protected]ea3690c2013-09-23 17:59:2215 const char* to_dir,
[email protected]ee55eace2014-04-03 07:45:4216 const char* from_dir) {
[email protected]ea3690c2013-09-23 17:59:2217 std::vector<Value> args;
tfarina9b636af2014-12-23 00:52:0718 args.push_back(Value(nullptr, input));
19 args.push_back(Value(nullptr, to_dir));
20 args.push_back(Value(nullptr, from_dir));
[email protected]ea3690c2013-09-23 17:59:2221
22 Err err;
23 FunctionCallNode function;
[email protected]c526bbf2013-09-23 18:29:4024 Value result = functions::RunRebasePath(scope, &function, args, &err);
[email protected]ea3690c2013-09-23 17:59:2225 bool is_string = result.type() == Value::STRING;
26 EXPECT_TRUE(is_string);
27
28 return result.string_value();
29}
30
31} // namespace
32
33TEST(RebasePath, Strings) {
34 TestWithScope setup;
[email protected]ea3690c2013-09-23 17:59:2235 Scope* scope = setup.scope();
36 scope->set_source_dir(SourceDir("//tools/gn/"));
37
38 // Build-file relative paths.
[email protected]55a3dd762014-02-18 20:24:4039 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", "."));
[email protected]ea3690c2013-09-23 17:59:2244
slan910ac442015-12-04 01:40:2945 // Without a source root defined, we cannot move out of the source tree.
[email protected]55a3dd762014-02-18 20:24:4046 EXPECT_EQ("../..", RebaseOne(scope, "../../..", "//out/Debug", "."));
[email protected]ea3690c2013-09-23 17:59:2247
48 // Source-absolute input paths.
49 EXPECT_EQ("./", RebaseOne(scope, "//", "//", "//"));
50 EXPECT_EQ("foo", RebaseOne(scope, "//foo", "//", "//"));
51 EXPECT_EQ("foo/", RebaseOne(scope, "//foo/", "//", "//"));
[email protected]55a3dd762014-02-18 20:24:4052 EXPECT_EQ("../../foo/bar", RebaseOne(scope, "//foo/bar", "//out/Debug", "."));
53 EXPECT_EQ("./", RebaseOne(scope, "//foo/", "//foo/", "//"));
agrieve3d56c98e2016-09-16 18:40:4954 EXPECT_EQ(".", RebaseOne(scope, "//foo", "//foo", "//"));
[email protected]ea3690c2013-09-23 17:59:2255
56 // Test slash conversion.
[email protected]ee55eace2014-04-03 07:45:4257 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo/bar", ".", "."));
58 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo\\bar", ".", "."));
[email protected]ea3690c2013-09-23 17:59:2259
60 // Test system path output.
61#if defined(OS_WIN)
slan910ac442015-12-04 01:40:2962 setup.build_settings()->SetRootPath(base::FilePath(L"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
[email protected]ea3690c2013-09-23 17:59:2277#else
slan910ac442015-12-04 01:40:2978 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/", "", "//"));
[email protected]ea3690c2013-09-23 17:59:2292#endif
93}
94
zeuthen6f9c64562014-11-11 21:01:1395TEST(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(L"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("/hdd/src"));
125
126 // Test system absolute to-dir.
127 EXPECT_EQ("../../ssd/out/Debug",
128 RebaseOne(scope, ".", "//", "/ssd/out/Debug"));
129 EXPECT_EQ("../../ssd/out/Debug/",
130 RebaseOne(scope, "./", "//", "/ssd/out/Debug"));
131 EXPECT_EQ("../../ssd/out/Debug/foo",
132 RebaseOne(scope, "foo", "//", "/ssd/out/Debug"));
133 EXPECT_EQ("../../ssd/out/Debug/foo/",
134 RebaseOne(scope, "foo/", "//", "/ssd/out/Debug"));
135
136 // Test system absolute from-dir.
137 EXPECT_EQ("../../../hdd/src",
138 RebaseOne(scope, ".", "/ssd/out/Debug", "//"));
139 EXPECT_EQ("../../../hdd/src/",
140 RebaseOne(scope, "./", "/ssd/out/Debug", "//"));
141 EXPECT_EQ("../../../hdd/src/foo",
142 RebaseOne(scope, "foo", "/ssd/out/Debug", "//"));
143 EXPECT_EQ("../../../hdd/src/foo/",
144 RebaseOne(scope, "foo/", "/ssd/out/Debug", "//"));
145#endif
146}
147
[email protected]ea3690c2013-09-23 17:59:22148// Test list input.
149TEST(RebasePath, List) {
150 TestWithScope setup;
[email protected]ea3690c2013-09-23 17:59:22151 setup.scope()->set_source_dir(SourceDir("//tools/gn/"));
152
153 std::vector<Value> args;
tfarina9b636af2014-12-23 00:52:07154 args.push_back(Value(nullptr, Value::LIST));
155 args[0].list_value().push_back(Value(nullptr, "foo.txt"));
156 args[0].list_value().push_back(Value(nullptr, "bar.txt"));
157 args.push_back(Value(nullptr, "//out/Debug/"));
158 args.push_back(Value(nullptr, "."));
[email protected]ea3690c2013-09-23 17:59:22159
160 Err err;
161 FunctionCallNode function;
162 Value ret = functions::RunRebasePath(setup.scope(), &function, args, &err);
163 EXPECT_FALSE(err.has_error());
164
165 ASSERT_EQ(Value::LIST, ret.type());
166 ASSERT_EQ(2u, ret.list_value().size());
167
168 EXPECT_EQ("../../tools/gn/foo.txt", ret.list_value()[0].string_value());
169 EXPECT_EQ("../../tools/gn/bar.txt", ret.list_value()[1].string_value());
170}
171
172TEST(RebasePath, Errors) {
173 TestWithScope setup;
[email protected]ea3690c2013-09-23 17:59:22174
175 // No arg input should issue an error.
176 Err err;
177 std::vector<Value> args;
178 FunctionCallNode function;
179 Value ret = functions::RunRebasePath(setup.scope(), &function, args, &err);
180 EXPECT_TRUE(err.has_error());
[email protected]ea3690c2013-09-23 17:59:22181}