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 11namespace { 12 13std::string RebaseOne(Scope* scope, 14 const char* input, 15 const char* to_dir, 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)); 21 22 Err err; 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); 27 28 return result.string_value(); 29} 30 31} // namespace 32 33TEST(RebasePath, Strings) { 34 TestWithScope setup; 35 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); 36 Scope* scope = setup.scope(); 37 scope->set_source_dir(SourceDir("//tools/gn/")); 38 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", ".")); 45 46 // We don't allow going above the root source dir. 47 EXPECT_EQ("../..", RebaseOne(scope, "../../..", "//out/Debug", ".")); 48 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 56 // necessary. 57 EXPECT_EQ("../foo", RebaseOne(scope, "//foo", "//foo", "//")); 58 59 // Test slash conversion. 60 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo/bar", ".", ".")); 61 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo\\bar", ".", ".")); 62 63 // Test system path output. 64#if defined(OS_WIN) 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", "", ".")); 71#else 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", "", ".")); 78#endif 79} 80 81// Test list input. 82TEST(RebasePath, List) { 83 TestWithScope setup; 84 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); 85 setup.scope()->set_source_dir(SourceDir("//tools/gn/")); 86 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, ".")); 93 94 Err err; 95 FunctionCallNode function; 96 Value ret = functions::RunRebasePath(setup.scope(), &function, args, &err); 97 EXPECT_FALSE(err.has_error()); 98 99 ASSERT_EQ(Value::LIST, ret.type()); 100 ASSERT_EQ(2u, ret.list_value().size()); 101 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()); 104} 105 106TEST(RebasePath, Errors) { 107 TestWithScope setup; 108 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); 109 110 // No arg input should issue an error. 111 Err err; 112 std::vector<Value> args; 113 FunctionCallNode function; 114 Value ret = functions::RunRebasePath(setup.scope(), &function, args, &err); 115 EXPECT_TRUE(err.has_error()); 116} 117