[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 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 "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 | |
| 11 | namespace { |
| 12 | |
[email protected] | c526bbf | 2013-09-23 18:29:40 | [diff] [blame] | 13 | std::string RebaseOne(Scope* scope, |
[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 14 | const char* input, |
[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 15 | const char* to_dir, |
[email protected] | ee55eace | 2014-04-03 07:45:42 | [diff] [blame] | 16 | const char* from_dir) { |
[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 17 | std::vector<Value> args; |
tfarina | 9b636af | 2014-12-23 00:52:07 | [diff] [blame] | 18 | args.push_back(Value(nullptr, input)); |
| 19 | args.push_back(Value(nullptr, to_dir)); |
| 20 | args.push_back(Value(nullptr, from_dir)); |
[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 21 | |
| 22 | Err err; |
| 23 | FunctionCallNode function; |
[email protected] | c526bbf | 2013-09-23 18:29:40 | [diff] [blame] | 24 | Value result = functions::RunRebasePath(scope, &function, args, &err); |
[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 25 | bool is_string = result.type() == Value::STRING; |
| 26 | EXPECT_TRUE(is_string); |
| 27 | |
| 28 | return result.string_value(); |
| 29 | } |
| 30 | |
| 31 | } // namespace |
| 32 | |
| 33 | TEST(RebasePath, Strings) { |
| 34 | TestWithScope setup; |
[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 35 | Scope* scope = setup.scope(); |
| 36 | scope->set_source_dir(SourceDir("//tools/gn/")); |
| 37 | |
| 38 | // Build-file relative paths. |
[email protected] | 55a3dd76 | 2014-02-18 20:24:40 | [diff] [blame] | 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", ".")); |
[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 44 | |
slan | 910ac44 | 2015-12-04 01:40:29 | [diff] [blame] | 45 | // Without a source root defined, we cannot move out of the source tree. |
[email protected] | 55a3dd76 | 2014-02-18 20:24:40 | [diff] [blame] | 46 | EXPECT_EQ("../..", RebaseOne(scope, "../../..", "//out/Debug", ".")); |
[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 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/", "//", "//")); |
[email protected] | 55a3dd76 | 2014-02-18 20:24:40 | [diff] [blame] | 52 | EXPECT_EQ("../../foo/bar", RebaseOne(scope, "//foo/bar", "//out/Debug", ".")); |
| 53 | EXPECT_EQ("./", RebaseOne(scope, "//foo/", "//foo/", "//")); |
agrieve | 3d56c98e | 2016-09-16 18:40:49 | [diff] [blame] | 54 | EXPECT_EQ(".", RebaseOne(scope, "//foo", "//foo", "//")); |
[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 55 | |
| 56 | // Test slash conversion. |
[email protected] | ee55eace | 2014-04-03 07:45:42 | [diff] [blame] | 57 | EXPECT_EQ("foo/bar", RebaseOne(scope, "foo/bar", ".", ".")); |
| 58 | EXPECT_EQ("foo/bar", RebaseOne(scope, "foo\\bar", ".", ".")); |
[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 59 | |
| 60 | // Test system path output. |
| 61 | #if defined(OS_WIN) |
slan | 910ac44 | 2015-12-04 01:40:29 | [diff] [blame] | 62 | 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] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 77 | #else |
slan | 910ac44 | 2015-12-04 01:40:29 | [diff] [blame] | 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/", "", "//")); |
[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 92 | #endif |
| 93 | } |
| 94 | |
zeuthen | 6f9c6456 | 2014-11-11 21:01:13 | [diff] [blame] | 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(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] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 148 | // Test list input. |
| 149 | TEST(RebasePath, List) { |
| 150 | TestWithScope setup; |
[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 151 | setup.scope()->set_source_dir(SourceDir("//tools/gn/")); |
| 152 | |
| 153 | std::vector<Value> args; |
tfarina | 9b636af | 2014-12-23 00:52:07 | [diff] [blame] | 154 | 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] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 159 | |
| 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 | |
| 172 | TEST(RebasePath, Errors) { |
| 173 | TestWithScope setup; |
[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 174 | |
| 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] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 181 | } |