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 13 std::string RebaseOne(Scope* scope, 14 const char* input, 15 const char* from_dir, 16 const char* to_dir, 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)); 22 if (sep) 23 args.push_back(Value(NULL, sep)); 24 25 Err err; 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); 30 31 return result.string_value(); 32 } 33 34 } // namespace 35 36 TEST(RebasePath, Strings) { 37 TestWithScope setup; 38 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); 39 Scope* scope = setup.scope(); 40 scope->set_source_dir(SourceDir("//tools/gn/")); 41 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")); 48 49 // We don't allow going above the root source dir. 50 EXPECT_EQ("../..", RebaseOne(scope, "../../..", ".", "//out/Debug")); 51 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 59 // necessary. 60 EXPECT_EQ("../foo", RebaseOne(scope, "//foo", "//", "//foo")); 61 62 // Test slash conversion. 63 #if defined(OS_WIN) 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")); 67 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")); 75 #endif 76 77 // Test system path output. 78 #if defined(OS_WIN) 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", ".", "")); 85 #else 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", ".", "")); 92 #endif 93 } 94 95 // Test list input. 96 TEST(RebasePath, List) { 97 TestWithScope setup; 98 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); 99 setup.scope()->set_source_dir(SourceDir("//tools/gn/")); 100 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/")); 107 108 Err err; 109 FunctionCallNode function; 110 Value ret = functions::RunRebasePath(setup.scope(), &function, args, &err); 111 EXPECT_FALSE(err.has_error()); 112 113 ASSERT_EQ(Value::LIST, ret.type()); 114 ASSERT_EQ(2u, ret.list_value().size()); 115 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()); 118 } 119 120 TEST(RebasePath, Errors) { 121 TestWithScope setup; 122 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); 123 124 // No arg input should issue an error. 125 Err err; 126 std::vector<Value> args; 127 FunctionCallNode function; 128 Value ret = functions::RunRebasePath(setup.scope(), &function, args, &err); 129 EXPECT_TRUE(err.has_error()); 130 131 // One arg int input. 132 args.push_back(Value(NULL, static_cast<int64>(5))); 133 err = Err(); 134 ret = functions::RunRebasePath(setup.scope(), &function, args, &err); 135 EXPECT_TRUE(err.has_error()); 136 137 // Two arg string input. 138 args.clear(); 139 args.push_back(Value(NULL, "hello")); 140 args.push_back(Value(NULL, "world")); 141 err = Err(); 142 ret = functions::RunRebasePath(setup.scope(), &function, args, &err); 143 EXPECT_TRUE(err.has_error()); 144 } 145