[email protected] | b90c75a | 2013-09-18 22:02:50 | [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 | |
[email protected] | c9f0540 | 2013-09-23 23:12:58 | [diff] [blame] | 5 | #include <algorithm> |
[email protected] | b90c75a | 2013-09-18 22:02:50 | [diff] [blame] | 6 | #include <sstream> |
| 7 | |
| 8 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | b90c75a | 2013-09-18 22:02:50 | [diff] [blame] | 9 | #include "tools/gn/ninja_copy_target_writer.h" |
[email protected] | 0dfcae7 | 2014-08-19 22:52:16 | [diff] [blame] | 10 | #include "tools/gn/target.h" |
[email protected] | b90c75a | 2013-09-18 22:02:50 | [diff] [blame] | 11 | #include "tools/gn/test_with_scope.h" |
| 12 | |
dpranke | 44128e3 | 2014-10-31 23:01:29 | [diff] [blame] | 13 | // Tests multiple files with an output pattern and no toolchain dependency. |
[email protected] | b90c75a | 2013-09-18 22:02:50 | [diff] [blame] | 14 | TEST(NinjaCopyTargetWriter, Run) { |
Brett Wilson | 85423a0 | 2014-09-02 19:29:42 | [diff] [blame] | 15 | Err err; |
sdefresne | 3fed805 | 2016-07-01 13:29:47 | [diff] [blame] | 16 | TestWithScope setup; |
[email protected] | 0dfcae7 | 2014-08-19 22:52:16 | [diff] [blame] | 17 | |
Dirk Pranke | c9126ec98 | 2017-08-17 15:05:07 | [diff] [blame^] | 18 | Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
[email protected] | b90c75a | 2013-09-18 22:02:50 | [diff] [blame] | 19 | target.set_output_type(Target::COPY_FILES); |
| 20 | |
| 21 | target.sources().push_back(SourceFile("//foo/input1.txt")); |
| 22 | target.sources().push_back(SourceFile("//foo/input2.txt")); |
| 23 | |
[email protected] | 012db594 | 2014-08-06 20:38:45 | [diff] [blame] | 24 | target.action_values().outputs() = |
| 25 | SubstitutionList::MakeForTest("//out/Debug/{{source_name_part}}.out"); |
[email protected] | b90c75a | 2013-09-18 22:02:50 | [diff] [blame] | 26 | |
[email protected] | 0dfcae7 | 2014-08-19 22:52:16 | [diff] [blame] | 27 | target.SetToolchain(setup.toolchain()); |
Brett Wilson | 85423a0 | 2014-09-02 19:29:42 | [diff] [blame] | 28 | ASSERT_TRUE(target.OnResolved(&err)); |
[email protected] | 0dfcae7 | 2014-08-19 22:52:16 | [diff] [blame] | 29 | |
[email protected] | abc283c | 2014-06-26 18:12:30 | [diff] [blame] | 30 | std::ostringstream out; |
[email protected] | 0dfcae7 | 2014-08-19 22:52:16 | [diff] [blame] | 31 | NinjaCopyTargetWriter writer(&target, out); |
[email protected] | abc283c | 2014-06-26 18:12:30 | [diff] [blame] | 32 | writer.Run(); |
[email protected] | b90c75a | 2013-09-18 22:02:50 | [diff] [blame] | 33 | |
[email protected] | abc283c | 2014-06-26 18:12:30 | [diff] [blame] | 34 | const char expected_linux[] = |
| 35 | "build input1.out: copy ../../foo/input1.txt\n" |
| 36 | "build input2.out: copy ../../foo/input2.txt\n" |
| 37 | "\n" |
| 38 | "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; |
| 39 | std::string out_str = out.str(); |
| 40 | EXPECT_EQ(expected_linux, out_str); |
| 41 | } |
[email protected] | b90c75a | 2013-09-18 22:02:50 | [diff] [blame] | 42 | |
[email protected] | e161f84 | 2014-07-23 22:05:19 | [diff] [blame] | 43 | // Tests a single file with no output pattern. |
[email protected] | abc283c | 2014-06-26 18:12:30 | [diff] [blame] | 44 | TEST(NinjaCopyTargetWriter, ToolchainDeps) { |
Brett Wilson | 85423a0 | 2014-09-02 19:29:42 | [diff] [blame] | 45 | Err err; |
sdefresne | 3fed805 | 2016-07-01 13:29:47 | [diff] [blame] | 46 | TestWithScope setup; |
[email protected] | 0dfcae7 | 2014-08-19 22:52:16 | [diff] [blame] | 47 | |
Dirk Pranke | c9126ec98 | 2017-08-17 15:05:07 | [diff] [blame^] | 48 | Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
[email protected] | abc283c | 2014-06-26 18:12:30 | [diff] [blame] | 49 | target.set_output_type(Target::COPY_FILES); |
[email protected] | b90c75a | 2013-09-18 22:02:50 | [diff] [blame] | 50 | |
[email protected] | abc283c | 2014-06-26 18:12:30 | [diff] [blame] | 51 | target.sources().push_back(SourceFile("//foo/input1.txt")); |
[email protected] | b90c75a | 2013-09-18 22:02:50 | [diff] [blame] | 52 | |
[email protected] | 012db594 | 2014-08-06 20:38:45 | [diff] [blame] | 53 | target.action_values().outputs() = |
| 54 | SubstitutionList::MakeForTest("//out/Debug/output.out"); |
[email protected] | abc283c | 2014-06-26 18:12:30 | [diff] [blame] | 55 | |
[email protected] | 0dfcae7 | 2014-08-19 22:52:16 | [diff] [blame] | 56 | target.SetToolchain(setup.toolchain()); |
Brett Wilson | 85423a0 | 2014-09-02 19:29:42 | [diff] [blame] | 57 | ASSERT_TRUE(target.OnResolved(&err)); |
[email protected] | 0dfcae7 | 2014-08-19 22:52:16 | [diff] [blame] | 58 | |
[email protected] | abc283c | 2014-06-26 18:12:30 | [diff] [blame] | 59 | std::ostringstream out; |
[email protected] | 0dfcae7 | 2014-08-19 22:52:16 | [diff] [blame] | 60 | NinjaCopyTargetWriter writer(&target, out); |
[email protected] | abc283c | 2014-06-26 18:12:30 | [diff] [blame] | 61 | writer.Run(); |
| 62 | |
| 63 | const char expected_linux[] = |
[email protected] | e161f84 | 2014-07-23 22:05:19 | [diff] [blame] | 64 | "build output.out: copy ../../foo/input1.txt\n" |
[email protected] | abc283c | 2014-06-26 18:12:30 | [diff] [blame] | 65 | "\n" |
| 66 | "build obj/foo/bar.stamp: stamp output.out\n"; |
| 67 | std::string out_str = out.str(); |
| 68 | EXPECT_EQ(expected_linux, out_str); |
[email protected] | b90c75a | 2013-09-18 22:02:50 | [diff] [blame] | 69 | } |
dpranke | 44128e3 | 2014-10-31 23:01:29 | [diff] [blame] | 70 | |
| 71 | TEST(NinjaCopyTargetWriter, OrderOnlyDeps) { |
dpranke | 44128e3 | 2014-10-31 23:01:29 | [diff] [blame] | 72 | Err err; |
sdefresne | 3fed805 | 2016-07-01 13:29:47 | [diff] [blame] | 73 | TestWithScope setup; |
dpranke | 44128e3 | 2014-10-31 23:01:29 | [diff] [blame] | 74 | |
Dirk Pranke | c9126ec98 | 2017-08-17 15:05:07 | [diff] [blame^] | 75 | Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
dpranke | 44128e3 | 2014-10-31 23:01:29 | [diff] [blame] | 76 | target.set_output_type(Target::COPY_FILES); |
| 77 | target.sources().push_back(SourceFile("//foo/input1.txt")); |
| 78 | target.action_values().outputs() = |
| 79 | SubstitutionList::MakeForTest("//out/Debug/{{source_name_part}}.out"); |
| 80 | target.inputs().push_back(SourceFile("//foo/script.py")); |
| 81 | target.SetToolchain(setup.toolchain()); |
| 82 | ASSERT_TRUE(target.OnResolved(&err)); |
| 83 | |
| 84 | std::ostringstream out; |
| 85 | NinjaCopyTargetWriter writer(&target, out); |
| 86 | writer.Run(); |
| 87 | |
| 88 | const char expected_linux[] = |
brettw | 4445f617 | 2016-02-03 22:57:14 | [diff] [blame] | 89 | "build input1.out: copy ../../foo/input1.txt || ../../foo/script.py\n" |
dpranke | 44128e3 | 2014-10-31 23:01:29 | [diff] [blame] | 90 | "\n" |
| 91 | "build obj/foo/bar.stamp: stamp input1.out\n"; |
| 92 | std::string out_str = out.str(); |
| 93 | EXPECT_EQ(expected_linux, out_str); |
| 94 | } |