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.
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"
13 std::string
RebaseOne(Scope
* scope
,
17 const char* sep
= NULL
) {
18 std::vector
<Value
> args
;
19 args
.push_back(Value(NULL
, input
));
20 args
.push_back(Value(NULL
, from_dir
));
21 args
.push_back(Value(NULL
, to_dir
));
23 args
.push_back(Value(NULL
, sep
));
26 FunctionCallNode function
;
27 Value result
= functions::RunRebasePath(scope
, &function
, args
, &err
);
28 bool is_string
= result
.type() == Value::STRING
;
29 EXPECT_TRUE(is_string
);
31 return result
.string_value();
36 TEST(RebasePath
, Strings
) {
38 setup
.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
39 Scope
* scope
= setup
.scope();
40 scope
->set_source_dir(SourceDir("//tools/gn/"));
42 // Build-file relative paths.
43 EXPECT_EQ("../../tools/gn", RebaseOne(scope
, ".", ".", "//out/Debug"));
44 EXPECT_EQ("../../tools/gn/", RebaseOne(scope
, "./", ".", "//out/Debug"));
45 EXPECT_EQ("../../tools/gn/foo", RebaseOne(scope
, "foo", ".", "//out/Debug"));
46 EXPECT_EQ("../..", RebaseOne(scope
, "../..", ".", "//out/Debug"));
47 EXPECT_EQ("../../", RebaseOne(scope
, "../../", ".", "//out/Debug"));
49 // We don't allow going above the root source dir.
50 EXPECT_EQ("../..", RebaseOne(scope
, "../../..", ".", "//out/Debug"));
52 // Source-absolute input paths.
53 EXPECT_EQ("./", RebaseOne(scope
, "//", "//", "//"));
54 EXPECT_EQ("foo", RebaseOne(scope
, "//foo", "//", "//"));
55 EXPECT_EQ("foo/", RebaseOne(scope
, "//foo/", "//", "//"));
56 EXPECT_EQ("../../foo/bar", RebaseOne(scope
, "//foo/bar", ".", "//out/Debug"));
57 EXPECT_EQ("./", RebaseOne(scope
, "//foo/", "//", "//foo/"));
58 // Thie one is technically correct but could be simplified to "." if
60 EXPECT_EQ("../foo", RebaseOne(scope
, "//foo", "//", "//foo"));
62 // Test slash conversion.
64 EXPECT_EQ("foo/bar", RebaseOne(scope
, "foo/bar", ".", ".", "none"));
65 EXPECT_EQ("foo\\bar", RebaseOne(scope
, "foo/bar", ".", ".", "to_system"));
66 EXPECT_EQ("foo/bar", RebaseOne(scope
, "foo/bar", ".", ".", "from_system"));
68 EXPECT_EQ("foo\\bar", RebaseOne(scope
, "foo\\bar", ".", ".", "none"));
69 EXPECT_EQ("foo\\bar", RebaseOne(scope
, "foo\\bar", ".", ".", "to_system"));
70 EXPECT_EQ("foo/bar", RebaseOne(scope
, "foo\\bar", ".", ".", "from_system"));
71 #else // No transformations on Posix.
72 EXPECT_EQ("foo/bar", RebaseOne(scope
, "foo/bar", ".", ".", "none"));
73 EXPECT_EQ("foo/bar", RebaseOne(scope
, "foo/bar", ".", ".", "to_system"));
74 EXPECT_EQ("foo/bar", RebaseOne(scope
, "foo/bar", ".", ".", "from_system"));
77 // Test system path output.
79 setup
.build_settings()->SetRootPath(base::FilePath(L
"C:\\source"));
80 EXPECT_EQ("C:\\source", RebaseOne(scope
, ".", "//", ""));
81 EXPECT_EQ("C:\\source\\", RebaseOne(scope
, "//", "//", ""));
82 EXPECT_EQ("C:\\source\\foo", RebaseOne(scope
, "foo", "//", ""));
83 EXPECT_EQ("C:\\source\\foo\\", RebaseOne(scope
, "foo/", "//", ""));
84 EXPECT_EQ("C:\\source\\tools\\gn\\foo", RebaseOne(scope
, "foo", ".", ""));
86 setup
.build_settings()->SetRootPath(base::FilePath("/source"));
87 EXPECT_EQ("/source", RebaseOne(scope
, ".", "//", ""));
88 EXPECT_EQ("/source/", RebaseOne(scope
, "//", "//", ""));
89 EXPECT_EQ("/source/foo", RebaseOne(scope
, "foo", "//", ""));
90 EXPECT_EQ("/source/foo/", RebaseOne(scope
, "foo/", "//", ""));
91 EXPECT_EQ("/source/tools/gn/foo", RebaseOne(scope
, "foo", ".", ""));
96 TEST(RebasePath
, List
) {
98 setup
.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
99 setup
.scope()->set_source_dir(SourceDir("//tools/gn/"));
101 std::vector
<Value
> args
;
102 args
.push_back(Value(NULL
, Value::LIST
));
103 args
[0].list_value().push_back(Value(NULL
, "foo.txt"));
104 args
[0].list_value().push_back(Value(NULL
, "bar.txt"));
105 args
.push_back(Value(NULL
, "."));
106 args
.push_back(Value(NULL
, "//out/Debug/"));
109 FunctionCallNode function
;
110 Value ret
= functions::RunRebasePath(setup
.scope(), &function
, args
, &err
);
111 EXPECT_FALSE(err
.has_error());
113 ASSERT_EQ(Value::LIST
, ret
.type());
114 ASSERT_EQ(2u, ret
.list_value().size());
116 EXPECT_EQ("../../tools/gn/foo.txt", ret
.list_value()[0].string_value());
117 EXPECT_EQ("../../tools/gn/bar.txt", ret
.list_value()[1].string_value());
120 TEST(RebasePath
, Errors
) {
122 setup
.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
124 // No arg input should issue an error.
126 std::vector
<Value
> args
;
127 FunctionCallNode function
;
128 Value ret
= functions::RunRebasePath(setup
.scope(), &function
, args
, &err
);
129 EXPECT_TRUE(err
.has_error());
131 // One arg int input.
132 args
.push_back(Value(NULL
, static_cast<int64
>(5)));
134 ret
= functions::RunRebasePath(setup
.scope(), &function
, args
, &err
);
135 EXPECT_TRUE(err
.has_error());
137 // Two arg string input.
139 args
.push_back(Value(NULL
, "hello"));
140 args
.push_back(Value(NULL
, "world"));
142 ret
= functions::RunRebasePath(setup
.scope(), &function
, args
, &err
);
143 EXPECT_TRUE(err
.has_error());