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
,
16 const char* from_dir
) {
17 std::vector
<Value
> args
;
18 args
.push_back(Value(NULL
, input
));
19 args
.push_back(Value(NULL
, to_dir
));
20 args
.push_back(Value(NULL
, from_dir
));
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
);
28 return result
.string_value();
33 TEST(RebasePath
, Strings
) {
35 setup
.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
36 Scope
* scope
= setup
.scope();
37 scope
->set_source_dir(SourceDir("//tools/gn/"));
39 // Build-file relative paths.
40 EXPECT_EQ("../../tools/gn", RebaseOne(scope
, ".", "//out/Debug", "."));
41 EXPECT_EQ("../../tools/gn/", RebaseOne(scope
, "./", "//out/Debug", "."));
42 EXPECT_EQ("../../tools/gn/foo", RebaseOne(scope
, "foo", "//out/Debug", "."));
43 EXPECT_EQ("../..", RebaseOne(scope
, "../..", "//out/Debug", "."));
44 EXPECT_EQ("../../", RebaseOne(scope
, "../../", "//out/Debug", "."));
46 // We don't allow going above the root source dir.
47 EXPECT_EQ("../..", RebaseOne(scope
, "../../..", "//out/Debug", "."));
49 // Source-absolute input paths.
50 EXPECT_EQ("./", RebaseOne(scope
, "//", "//", "//"));
51 EXPECT_EQ("foo", RebaseOne(scope
, "//foo", "//", "//"));
52 EXPECT_EQ("foo/", RebaseOne(scope
, "//foo/", "//", "//"));
53 EXPECT_EQ("../../foo/bar", RebaseOne(scope
, "//foo/bar", "//out/Debug", "."));
54 EXPECT_EQ("./", RebaseOne(scope
, "//foo/", "//foo/", "//"));
55 // Thie one is technically correct but could be simplified to "." if
57 EXPECT_EQ("../foo", RebaseOne(scope
, "//foo", "//foo", "//"));
59 // Test slash conversion.
60 EXPECT_EQ("foo/bar", RebaseOne(scope
, "foo/bar", ".", "."));
61 EXPECT_EQ("foo/bar", RebaseOne(scope
, "foo\\bar", ".", "."));
63 // Test system path output.
65 setup
.build_settings()->SetRootPath(base::FilePath(L
"C:/source"));
66 EXPECT_EQ("C:/source", RebaseOne(scope
, ".", "", "//"));
67 EXPECT_EQ("C:/source/", RebaseOne(scope
, "//", "", "//"));
68 EXPECT_EQ("C:/source/foo", RebaseOne(scope
, "foo", "", "//"));
69 EXPECT_EQ("C:/source/foo/", RebaseOne(scope
, "foo/", "", "//"));
70 EXPECT_EQ("C:/source/tools/gn/foo", RebaseOne(scope
, "foo", "", "."));
72 setup
.build_settings()->SetRootPath(base::FilePath("/source"));
73 EXPECT_EQ("/source", RebaseOne(scope
, ".", "", "//"));
74 EXPECT_EQ("/source/", RebaseOne(scope
, "//", "", "//"));
75 EXPECT_EQ("/source/foo", RebaseOne(scope
, "foo", "", "//"));
76 EXPECT_EQ("/source/foo/", RebaseOne(scope
, "foo/", "", "//"));
77 EXPECT_EQ("/source/tools/gn/foo", RebaseOne(scope
, "foo", "", "."));
82 TEST(RebasePath
, List
) {
84 setup
.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
85 setup
.scope()->set_source_dir(SourceDir("//tools/gn/"));
87 std::vector
<Value
> args
;
88 args
.push_back(Value(NULL
, Value::LIST
));
89 args
[0].list_value().push_back(Value(NULL
, "foo.txt"));
90 args
[0].list_value().push_back(Value(NULL
, "bar.txt"));
91 args
.push_back(Value(NULL
, "//out/Debug/"));
92 args
.push_back(Value(NULL
, "."));
95 FunctionCallNode function
;
96 Value ret
= functions::RunRebasePath(setup
.scope(), &function
, args
, &err
);
97 EXPECT_FALSE(err
.has_error());
99 ASSERT_EQ(Value::LIST
, ret
.type());
100 ASSERT_EQ(2u, ret
.list_value().size());
102 EXPECT_EQ("../../tools/gn/foo.txt", ret
.list_value()[0].string_value());
103 EXPECT_EQ("../../tools/gn/bar.txt", ret
.list_value()[1].string_value());
106 TEST(RebasePath
, Errors
) {
108 setup
.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
110 // No arg input should issue an error.
112 std::vector
<Value
> args
;
113 FunctionCallNode function
;
114 Value ret
= functions::RunRebasePath(setup
.scope(), &function
, args
, &err
);
115 EXPECT_TRUE(err
.has_error());