Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(415)

Side by Side Diff: tools/gn/substitution_writer_unittest.cc

Issue 2940873002: Implement tracking of BUILD.gn files used to define target, toolchain or (Closed)
Patch Set: Fix compilation after rebase. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/gn/string_utils_unittest.cc ('k') | tools/gn/target.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <sstream> 5 #include <sstream>
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "tools/gn/err.h" 9 #include "tools/gn/err.h"
10 #include "tools/gn/escape.h" 10 #include "tools/gn/escape.h"
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 99
100 EXPECT_EQ( 100 EXPECT_EQ(
101 "-i ${in} --out=bar\"${source_name_part}\".o", 101 "-i ${in} --out=bar\"${source_name_part}\".o",
102 out.str()); 102 out.str());
103 } 103 }
104 104
105 TEST(SubstitutionWriter, SourceSubstitutions) { 105 TEST(SubstitutionWriter, SourceSubstitutions) {
106 TestWithScope setup; 106 TestWithScope setup;
107 Err err; 107 Err err;
108 108
109 Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz")); 109 Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz"), {});
110 target.set_output_type(Target::STATIC_LIBRARY); 110 target.set_output_type(Target::STATIC_LIBRARY);
111 target.SetToolchain(setup.toolchain()); 111 target.SetToolchain(setup.toolchain());
112 ASSERT_TRUE(target.OnResolved(&err)); 112 ASSERT_TRUE(target.OnResolved(&err));
113 113
114 // Call to get substitutions relative to the build dir. 114 // Call to get substitutions relative to the build dir.
115 #define GetRelSubst(str, what) \ 115 #define GetRelSubst(str, what) \
116 SubstitutionWriter::GetSourceSubstitution( \ 116 SubstitutionWriter::GetSourceSubstitution( \
117 &target, \ 117 &target, \
118 setup.settings(), \ 118 setup.settings(), \
119 SourceFile(str), \ 119 SourceFile(str), \
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 GetAbsSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE_TARGET_RELATIVE)); 190 GetAbsSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE_TARGET_RELATIVE));
191 191
192 #undef GetAbsSubst 192 #undef GetAbsSubst
193 #undef GetRelSubst 193 #undef GetRelSubst
194 } 194 }
195 195
196 TEST(SubstitutionWriter, TargetSubstitutions) { 196 TEST(SubstitutionWriter, TargetSubstitutions) {
197 TestWithScope setup; 197 TestWithScope setup;
198 Err err; 198 Err err;
199 199
200 Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz")); 200 Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz"), {});
201 target.set_output_type(Target::STATIC_LIBRARY); 201 target.set_output_type(Target::STATIC_LIBRARY);
202 target.SetToolchain(setup.toolchain()); 202 target.SetToolchain(setup.toolchain());
203 ASSERT_TRUE(target.OnResolved(&err)); 203 ASSERT_TRUE(target.OnResolved(&err));
204 204
205 std::string result; 205 std::string result;
206 EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution( 206 EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution(
207 &target, SUBSTITUTION_LABEL, &result)); 207 &target, SUBSTITUTION_LABEL, &result));
208 EXPECT_EQ("//foo/bar:baz", result); 208 EXPECT_EQ("//foo/bar:baz", result);
209 209
210 EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution( 210 EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution(
(...skipping 18 matching lines...) Expand all
229 229
230 EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution( 230 EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution(
231 &target, SUBSTITUTION_TARGET_OUTPUT_NAME, &result)); 231 &target, SUBSTITUTION_TARGET_OUTPUT_NAME, &result));
232 EXPECT_EQ("libbaz", result); 232 EXPECT_EQ("libbaz", result);
233 } 233 }
234 234
235 TEST(SubstitutionWriter, CompilerSubstitutions) { 235 TEST(SubstitutionWriter, CompilerSubstitutions) {
236 TestWithScope setup; 236 TestWithScope setup;
237 Err err; 237 Err err;
238 238
239 Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz")); 239 Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz"), {});
240 target.set_output_type(Target::STATIC_LIBRARY); 240 target.set_output_type(Target::STATIC_LIBRARY);
241 target.SetToolchain(setup.toolchain()); 241 target.SetToolchain(setup.toolchain());
242 ASSERT_TRUE(target.OnResolved(&err)); 242 ASSERT_TRUE(target.OnResolved(&err));
243 243
244 // The compiler substitution is just source + target combined. So test one 244 // The compiler substitution is just source + target combined. So test one
245 // of each of those classes of things to make sure this is hooked up. 245 // of each of those classes of things to make sure this is hooked up.
246 EXPECT_EQ("file", 246 EXPECT_EQ("file",
247 SubstitutionWriter::GetCompilerSubstitution( 247 SubstitutionWriter::GetCompilerSubstitution(
248 &target, SourceFile("//foo/bar/file.txt"), 248 &target, SourceFile("//foo/bar/file.txt"),
249 SUBSTITUTION_SOURCE_NAME_PART)); 249 SUBSTITUTION_SOURCE_NAME_PART));
250 EXPECT_EQ("gen/foo/bar", 250 EXPECT_EQ("gen/foo/bar",
251 SubstitutionWriter::GetCompilerSubstitution( 251 SubstitutionWriter::GetCompilerSubstitution(
252 &target, SourceFile("//foo/bar/file.txt"), 252 &target, SourceFile("//foo/bar/file.txt"),
253 SUBSTITUTION_TARGET_GEN_DIR)); 253 SUBSTITUTION_TARGET_GEN_DIR));
254 } 254 }
255 255
256 TEST(SubstitutionWriter, LinkerSubstitutions) { 256 TEST(SubstitutionWriter, LinkerSubstitutions) {
257 TestWithScope setup; 257 TestWithScope setup;
258 Err err; 258 Err err;
259 259
260 Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz")); 260 Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz"), {});
261 target.set_output_type(Target::SHARED_LIBRARY); 261 target.set_output_type(Target::SHARED_LIBRARY);
262 target.SetToolchain(setup.toolchain()); 262 target.SetToolchain(setup.toolchain());
263 ASSERT_TRUE(target.OnResolved(&err)); 263 ASSERT_TRUE(target.OnResolved(&err));
264 264
265 const Tool* tool = setup.toolchain()->GetToolForTargetFinalOutput(&target); 265 const Tool* tool = setup.toolchain()->GetToolForTargetFinalOutput(&target);
266 266
267 // The compiler substitution is just target + OUTPUT_EXTENSION combined. So 267 // The compiler substitution is just target + OUTPUT_EXTENSION combined. So
268 // test one target one plus the output extension. 268 // test one target one plus the output extension.
269 EXPECT_EQ(".so", 269 EXPECT_EQ(".so",
270 SubstitutionWriter::GetLinkerSubstitution( 270 SubstitutionWriter::GetLinkerSubstitution(
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 // This tool has an output directory pattern and uses that for the 303 // This tool has an output directory pattern and uses that for the
304 // output name. 304 // output name.
305 Tool tool; 305 Tool tool;
306 SubstitutionPattern out_dir_pattern; 306 SubstitutionPattern out_dir_pattern;
307 ASSERT_TRUE(out_dir_pattern.Parse("{{root_out_dir}}/{{target_output_name}}", 307 ASSERT_TRUE(out_dir_pattern.Parse("{{root_out_dir}}/{{target_output_name}}",
308 nullptr, &err)); 308 nullptr, &err));
309 tool.set_default_output_dir(out_dir_pattern); 309 tool.set_default_output_dir(out_dir_pattern);
310 tool.SetComplete(); 310 tool.SetComplete();
311 311
312 // Default target with no output dir overrides. 312 // Default target with no output dir overrides.
313 Target target(setup.settings(), Label(SourceDir("//foo/"), "baz")); 313 Target target(setup.settings(), Label(SourceDir("//foo/"), "baz"), {});
314 target.set_output_type(Target::EXECUTABLE); 314 target.set_output_type(Target::EXECUTABLE);
315 target.SetToolchain(setup.toolchain()); 315 target.SetToolchain(setup.toolchain());
316 ASSERT_TRUE(target.OnResolved(&err)); 316 ASSERT_TRUE(target.OnResolved(&err));
317 317
318 // The output should expand the default from the patterns in the tool. 318 // The output should expand the default from the patterns in the tool.
319 SubstitutionPattern output_name; 319 SubstitutionPattern output_name;
320 ASSERT_TRUE(output_name.Parse("{{output_dir}}/{{target_output_name}}.exe", 320 ASSERT_TRUE(output_name.Parse("{{output_dir}}/{{target_output_name}}.exe",
321 nullptr, &err)); 321 nullptr, &err));
322 EXPECT_EQ("./baz/baz.exe", 322 EXPECT_EQ("./baz/baz.exe",
323 SubstitutionWriter::ApplyPatternToLinkerAsOutputFile( 323 SubstitutionWriter::ApplyPatternToLinkerAsOutputFile(
324 &target, &tool, output_name).value()); 324 &target, &tool, output_name).value());
325 325
326 // Override the output name to the root build dir. 326 // Override the output name to the root build dir.
327 target.set_output_dir(SourceDir("//out/Debug/")); 327 target.set_output_dir(SourceDir("//out/Debug/"));
328 EXPECT_EQ("./baz.exe", 328 EXPECT_EQ("./baz.exe",
329 SubstitutionWriter::ApplyPatternToLinkerAsOutputFile( 329 SubstitutionWriter::ApplyPatternToLinkerAsOutputFile(
330 &target, &tool, output_name).value()); 330 &target, &tool, output_name).value());
331 331
332 // Override the output name to a new subdirectory. 332 // Override the output name to a new subdirectory.
333 target.set_output_dir(SourceDir("//out/Debug/foo/bar")); 333 target.set_output_dir(SourceDir("//out/Debug/foo/bar"));
334 EXPECT_EQ("foo/bar/baz.exe", 334 EXPECT_EQ("foo/bar/baz.exe",
335 SubstitutionWriter::ApplyPatternToLinkerAsOutputFile( 335 SubstitutionWriter::ApplyPatternToLinkerAsOutputFile(
336 &target, &tool, output_name).value()); 336 &target, &tool, output_name).value());
337 } 337 }
OLDNEW
« no previous file with comments | « tools/gn/string_utils_unittest.cc ('k') | tools/gn/target.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698