| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 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/filesystem_utils.h" |
| 8 #include "tools/gn/functions.h" |
| 9 #include "tools/gn/parse_tree.h" |
| 10 #include "tools/gn/test_with_scope.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 std::string CallGetTargetGenPath(Scope* scope, |
| 15 const char* target_name, |
| 16 const char* suffix) { |
| 17 std::vector<Value> args; |
| 18 args.push_back(Value(NULL, target_name)); |
| 19 args.push_back(Value(NULL, suffix)); |
| 20 |
| 21 Err err; |
| 22 FunctionCallNode function; |
| 23 Value result = functions::RunGetTargetGenPath(scope, &function, args, &err); |
| 24 EXPECT_EQ(Value::STRING, result.type()); |
| 25 EXPECT_FALSE(err.has_error()); |
| 26 return result.string_value(); |
| 27 } |
| 28 |
| 29 std::string GetExpectedTargetGenPath(const Target& target, const char* suffix) { |
| 30 std::string target_name = target.label().name(); |
| 31 SourceDir target_gen_dir = GetTargetGenDir(&target); |
| 32 return target_gen_dir.value() + target_name + std::string(suffix); |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 37 TEST(GetTargetGenPath, Strings) { |
| 38 TestWithScope setup; |
| 39 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 40 Scope* tools_gn = setup.scope(); |
| 41 tools_gn->set_source_dir(SourceDir("//tools/gn/")); |
| 42 |
| 43 SourceDir tools_gn_util_dir("//tools/gn/util"); |
| 44 |
| 45 Err err; |
| 46 Target foo_target(setup.settings(), Label(tools_gn->GetSourceDir(), "foo")); |
| 47 Target bar_target(setup.settings(), Label(tools_gn_util_dir, "bar")); |
| 48 |
| 49 // Target in same file. |
| 50 EXPECT_EQ( |
| 51 GetExpectedTargetGenPath(foo_target, ".ext"), |
| 52 CallGetTargetGenPath(tools_gn, ":foo", ".ext")); |
| 53 |
| 54 EXPECT_EQ( |
| 55 GetExpectedTargetGenPath(foo_target, ".ext"), |
| 56 CallGetTargetGenPath(tools_gn, "//tools/gn:foo", ".ext")); |
| 57 |
| 58 EXPECT_EQ( |
| 59 GetExpectedTargetGenPath(foo_target, ".ext"), |
| 60 CallGetTargetGenPath(tools_gn, "../gn:foo", ".ext")); |
| 61 |
| 62 // Target in different dir. |
| 63 EXPECT_EQ( |
| 64 GetExpectedTargetGenPath(bar_target, ".ext"), |
| 65 CallGetTargetGenPath(tools_gn, "util:bar", ".ext")); |
| 66 |
| 67 EXPECT_EQ( |
| 68 GetExpectedTargetGenPath(bar_target, ".ext"), |
| 69 CallGetTargetGenPath(tools_gn, "//tools/gn/util:bar", ".ext")); |
| 70 |
| 71 EXPECT_EQ( |
| 72 GetExpectedTargetGenPath(bar_target, ".ext"), |
| 73 CallGetTargetGenPath(tools_gn, "../gn/util:bar", ".ext")); |
| 74 |
| 75 } |
| 76 |
| 77 // Test list input. |
| 78 TEST(GetTargetGenPath, List) { |
| 79 TestWithScope setup; |
| 80 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 81 Scope* tools_gn = setup.scope(); |
| 82 tools_gn->set_source_dir(SourceDir("//tools/gn/")); |
| 83 |
| 84 SourceDir tools_gn_util_dir("//tools/gn/util"); |
| 85 |
| 86 Target foo_target(setup.settings(), Label(tools_gn->GetSourceDir(), "foo")); |
| 87 Target bar_target(setup.settings(), Label(tools_gn_util_dir, "bar")); |
| 88 |
| 89 Value inputs_list(NULL, Value::LIST); |
| 90 inputs_list.list_value().push_back(Value(NULL, ":foo")); |
| 91 inputs_list.list_value().push_back(Value(NULL, "util:bar")); |
| 92 |
| 93 std::vector<Value> args; |
| 94 args.push_back(inputs_list); |
| 95 args.push_back(Value(NULL, ".ext")); |
| 96 |
| 97 Err err; |
| 98 FunctionCallNode function; |
| 99 Value result = |
| 100 functions::RunGetTargetGenPath(tools_gn, &function, args, &err); |
| 101 ASSERT_EQ(Value::LIST, result.type()); |
| 102 EXPECT_FALSE(err.has_error()); |
| 103 |
| 104 ASSERT_EQ((size_t)2, result.list_value().size()); |
| 105 ASSERT_EQ(Value::STRING, result.list_value()[0].type()); |
| 106 ASSERT_EQ(Value::STRING, result.list_value()[1].type()); |
| 107 |
| 108 EXPECT_EQ( |
| 109 GetExpectedTargetGenPath(foo_target, ".ext"), |
| 110 result.list_value()[0].string_value()); |
| 111 EXPECT_EQ( |
| 112 GetExpectedTargetGenPath(bar_target, ".ext"), |
| 113 result.list_value()[1].string_value()); |
| 114 } |
| 115 |
| 116 bool DoErrorCheck(const std::string& gn_input) { |
| 117 TestWithScope setup; |
| 118 setup.scope()->set_source_dir(SourceDir("//")); |
| 119 TestParseInput input(gn_input); |
| 120 if (input.has_error()) { |
| 121 ADD_FAILURE() << input.parse_err().message(); |
| 122 return true; |
| 123 } |
| 124 |
| 125 Err err; |
| 126 input.parsed()->Execute(setup.scope(), &err); |
| 127 return err.has_error(); |
| 128 } |
| 129 |
| 130 TEST(GetTargetGenPath, Errors) { |
| 131 // Valid uses. |
| 132 EXPECT_FALSE(DoErrorCheck( |
| 133 "get_target_gen_path(\":foo\", \".ext\")" |
| 134 "get_target_gen_path([\":foo\", \":bar\"], \".ext\")")); |
| 135 |
| 136 EXPECT_FALSE( |
| 137 DoErrorCheck("get_target_gen_path(\":foo\", \"/subdir/file\")\n")); |
| 138 |
| 139 // Missing suffix. |
| 140 EXPECT_TRUE(DoErrorCheck("get_target_gen_path(\":foo\")")); |
| 141 EXPECT_TRUE(DoErrorCheck("get_target_gen_path([\":foo\"])")); |
| 142 |
| 143 // Suffix type wrong. |
| 144 EXPECT_TRUE(DoErrorCheck("get_target_gen_path(\":foo\", [\".ext\"])")); |
| 145 |
| 146 // Too many arguments. |
| 147 EXPECT_TRUE( |
| 148 DoErrorCheck("get_target_gen_path(\":foo\", \".ext\", \"extra\")")); |
| 149 |
| 150 // Invalid target name. |
| 151 EXPECT_TRUE( |
| 152 DoErrorCheck("get_target_gen_path(\"\\\\..\\:foo\", \".ext\")")); |
| 153 |
| 154 // Final path should be a file (not a directory). |
| 155 EXPECT_TRUE(DoErrorCheck("get_target_gen_path(\":foo\", \"/subdir/\")\n")); |
| 156 } |
| OLD | NEW |