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
, to_dir
));
21 args
.push_back(Value(NULL
, from_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.
63 EXPECT_EQ("foo/bar", RebaseOne(scope
, "foo/bar", ".", ".", "to_slash"));
64 EXPECT_EQ("foo/bar", RebaseOne(scope
, "foo\\bar", ".", ".", "to_slash"));
66 EXPECT_EQ("foo\\bar", RebaseOne(scope
, "foo/bar", ".", ".", "to_system"));
67 EXPECT_EQ("foo\\bar", RebaseOne(scope
, "foo\\bar", ".", ".", "to_system"));
70 // Test system path output.
72 setup
.build_settings()->SetRootPath(base::FilePath(L
"C:\\source"));
73 EXPECT_EQ("C:\\source", RebaseOne(scope
, ".", "", "//"));
74 EXPECT_EQ("C:\\source\\", RebaseOne(scope
, "//", "", "//"));
75 EXPECT_EQ("C:\\source\\foo", RebaseOne(scope
, "foo", "", "//"));
76 EXPECT_EQ("C:\\source\\foo\\", RebaseOne(scope
, "foo/", "", "//"));
77 EXPECT_EQ("C:\\source\\tools\\gn\\foo", RebaseOne(scope
, "foo", "", "."));
79 setup
.build_settings()->SetRootPath(base::FilePath("/source"));
80 EXPECT_EQ("/source", RebaseOne(scope
, ".", "", "//"));
81 EXPECT_EQ("/source/", RebaseOne(scope
, "//", "", "//"));
82 EXPECT_EQ("/source/foo", RebaseOne(scope
, "foo", "", "//"));
83 EXPECT_EQ("/source/foo/", RebaseOne(scope
, "foo/", "", "//"));
84 EXPECT_EQ("/source/tools/gn/foo", RebaseOne(scope
, "foo", "", "."));
89 TEST(RebasePath
, List
) {
91 setup
.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
92 setup
.scope()->set_source_dir(SourceDir("//tools/gn/"));
94 std::vector
<Value
> args
;
95 args
.push_back(Value(NULL
, Value::LIST
));
96 args
[0].list_value().push_back(Value(NULL
, "foo.txt"));
97 args
[0].list_value().push_back(Value(NULL
, "bar.txt"));
98 args
.push_back(Value(NULL
, "//out/Debug/"));
99 args
.push_back(Value(NULL
, "."));
102 FunctionCallNode function
;
103 Value ret
= functions::RunRebasePath(setup
.scope(), &function
, args
, &err
);
104 EXPECT_FALSE(err
.has_error());
106 ASSERT_EQ(Value::LIST
, ret
.type());
107 ASSERT_EQ(2u, ret
.list_value().size());
109 EXPECT_EQ("../../tools/gn/foo.txt", ret
.list_value()[0].string_value());
110 EXPECT_EQ("../../tools/gn/bar.txt", ret
.list_value()[1].string_value());
113 TEST(RebasePath
, Errors
) {
115 setup
.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
117 // No arg input should issue an error.
119 std::vector
<Value
> args
;
120 FunctionCallNode function
;
121 Value ret
= functions::RunRebasePath(setup
.scope(), &function
, args
, &err
);
122 EXPECT_TRUE(err
.has_error());