OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 <algorithm> | 5 #include <algorithm> |
6 #include <sstream> | 6 #include <sstream> |
7 | 7 |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "tools/gn/file_template.h" | 9 #include "tools/gn/file_template.h" |
10 #include "tools/gn/ninja_copy_target_writer.h" | 10 #include "tools/gn/ninja_copy_target_writer.h" |
11 #include "tools/gn/test_with_scope.h" | 11 #include "tools/gn/test_with_scope.h" |
12 | 12 |
| 13 // Tests mutliple files with an output pattern and no toolchain dependency. |
13 TEST(NinjaCopyTargetWriter, Run) { | 14 TEST(NinjaCopyTargetWriter, Run) { |
14 TestWithScope setup; | 15 TestWithScope setup; |
| 16 setup.settings()->set_target_os(Settings::LINUX); |
15 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | 17 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
16 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 18 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
17 target.set_output_type(Target::COPY_FILES); | 19 target.set_output_type(Target::COPY_FILES); |
18 | 20 |
19 target.sources().push_back(SourceFile("//foo/input1.txt")); | 21 target.sources().push_back(SourceFile("//foo/input1.txt")); |
20 target.sources().push_back(SourceFile("//foo/input2.txt")); | 22 target.sources().push_back(SourceFile("//foo/input2.txt")); |
21 | 23 |
22 target.action_values().outputs().push_back( | 24 target.action_values().outputs().push_back( |
23 SourceFile("//out/Debug/{{source_name_part}}.out")); | 25 SourceFile("//out/Debug/{{source_name_part}}.out")); |
24 | 26 |
25 // Posix. | 27 std::ostringstream out; |
26 { | 28 NinjaCopyTargetWriter writer(&target, setup.toolchain(), out); |
27 setup.settings()->set_target_os(Settings::LINUX); | 29 writer.Run(); |
28 | 30 |
29 std::ostringstream out; | 31 const char expected_linux[] = |
30 NinjaCopyTargetWriter writer(&target, setup.toolchain(), out); | 32 "build input1.out: copy ../../foo/input1.txt\n" |
31 writer.Run(); | 33 "build input2.out: copy ../../foo/input2.txt\n" |
| 34 "\n" |
| 35 "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; |
| 36 std::string out_str = out.str(); |
| 37 EXPECT_EQ(expected_linux, out_str); |
| 38 } |
32 | 39 |
33 const char expected_linux[] = | 40 // Tests a single file with no output pattern and a toolchain dependency. |
34 "build input1.out: copy ../../foo/input1.txt\n" | 41 TEST(NinjaCopyTargetWriter, ToolchainDeps) { |
35 "build input2.out: copy ../../foo/input2.txt\n" | 42 TestWithScope setup; |
36 "\n" | 43 setup.settings()->set_target_os(Settings::LINUX); |
37 "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; | 44 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
38 std::string out_str = out.str(); | 45 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
39 #if defined(OS_WIN) | 46 target.set_output_type(Target::COPY_FILES); |
40 std::replace(out_str.begin(), out_str.end(), '\\', '/'); | |
41 #endif | |
42 EXPECT_EQ(expected_linux, out_str); | |
43 } | |
44 | 47 |
45 // Windows. | 48 // Toolchain dependency. Here we make a target in the same toolchain for |
46 { | 49 // simplicity, but in real life (using the Builder) this would be rejected |
47 setup.settings()->set_target_os(Settings::WIN); | 50 // because it would be a circular dependency (the target depends on its |
| 51 // toolchain, and the toolchain depends on this target). |
| 52 Target toolchain_dep_target(setup.settings(), |
| 53 Label(SourceDir("//foo/"), "setup")); |
| 54 toolchain_dep_target.set_output_type(Target::ACTION); |
| 55 setup.toolchain()->deps().push_back(LabelTargetPair(&toolchain_dep_target)); |
48 | 56 |
49 std::ostringstream out; | 57 target.sources().push_back(SourceFile("//foo/input1.txt")); |
50 NinjaCopyTargetWriter writer(&target, setup.toolchain(), out); | |
51 writer.Run(); | |
52 | 58 |
53 // TODO(brettw) I think we'll need to worry about backslashes here | 59 target.action_values().outputs().push_back( |
54 // depending if we're on actual Windows or Linux pretending to be Windows. | 60 SourceFile("//out/Debug/output.out")); |
55 const char expected_win[] = | 61 |
56 "build input1.out: copy ../../foo/input1.txt\n" | 62 std::ostringstream out; |
57 "build input2.out: copy ../../foo/input2.txt\n" | 63 NinjaCopyTargetWriter writer(&target, setup.toolchain(), out); |
58 "\n" | 64 writer.Run(); |
59 "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; | 65 |
60 std::string out_str = out.str(); | 66 const char expected_linux[] = |
61 #if defined(OS_WIN) | 67 "build obj/foo/bar.inputdeps.stamp: stamp obj/foo/setup.stamp\n" |
62 std::replace(out_str.begin(), out_str.end(), '\\', '/'); | 68 "build output.out: copy ../../foo/input1.txt | " |
63 #endif | 69 "obj/foo/bar.inputdeps.stamp\n" |
64 EXPECT_EQ(expected_win, out_str); | 70 "\n" |
65 } | 71 "build obj/foo/bar.stamp: stamp output.out\n"; |
| 72 std::string out_str = out.str(); |
| 73 EXPECT_EQ(expected_linux, out_str); |
66 } | 74 } |
OLD | NEW |