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 "gn/scheduler.h"
6 #include "gn/test_with_scheduler.h"
7 #include "gn/test_with_scope.h"
8 #include "util/test/test.h"
9
10 using ActionTargetGenerator = TestWithScheduler;
11
12 // Tests that actions can't have output substitutions.
TEST_F(ActionTargetGenerator,ActionOutputSubstitutions)13 TEST_F(ActionTargetGenerator, ActionOutputSubstitutions) {
14 TestWithScope setup;
15 Scope::ItemVector items_;
16 setup.scope()->set_item_collector(&items_);
17
18 // First test one with no substitutions, this should be valid.
19 TestParseInput input_good(
20 R"(action("foo") {
21 script = "//foo.py"
22 sources = [ "//bar.txt" ]
23 outputs = [ "//out/Debug/one.txt" ]
24 })");
25 ASSERT_FALSE(input_good.has_error());
26
27 // This should run fine.
28 Err err;
29 input_good.parsed()->Execute(setup.scope(), &err);
30 ASSERT_FALSE(err.has_error()) << err.message();
31
32 // Same thing with a pattern in the output should fail.
33 TestParseInput input_bad(
34 R"(action("foo") {
35 script = "//foo.py"
36 sources = [ "//bar.txt" ]
37 outputs = [ "//out/Debug/{{source_name_part}}.txt" ]
38 })");
39 ASSERT_FALSE(input_bad.has_error());
40
41 // This should run fine.
42 input_bad.parsed()->Execute(setup.scope(), &err);
43 ASSERT_TRUE(err.has_error());
44 }
45
46 // Tests that arg and response file substitutions are validated for
47 // action_foreach targets.
TEST_F(ActionTargetGenerator,ActionForeachSubstitutions)48 TEST_F(ActionTargetGenerator, ActionForeachSubstitutions) {
49 TestWithScope setup;
50 Scope::ItemVector items_;
51 setup.scope()->set_item_collector(&items_);
52
53 // Args listing a response file but missing a response file definition should
54 // fail.
55 TestParseInput input_missing_resp_file(
56 R"(action_foreach("foo") {
57 script = "//foo.py"
58 sources = [ "//bar.txt" ]
59 outputs = [ "//out/Debug/{{source_name_part}}" ]
60 args = [ "{{response_file_name}}" ]
61 })");
62 ASSERT_FALSE(input_missing_resp_file.has_error());
63 Err err;
64 input_missing_resp_file.parsed()->Execute(setup.scope(), &err);
65 ASSERT_TRUE(err.has_error());
66
67 // Adding a response file definition should pass.
68 err = Err();
69 TestParseInput input_resp_file(
70 R"(action_foreach("foo") {
71 script = "//foo.py"
72 sources = [ "//bar.txt" ]
73 outputs = [ "//out/Debug/{{source_name_part}}" ]
74 args = [ "{{response_file_name}}" ]
75 response_file_contents = [ "{{source_name_part}}" ]
76 })");
77 ASSERT_FALSE(input_resp_file.has_error());
78 input_resp_file.parsed()->Execute(setup.scope(), &err);
79 ASSERT_FALSE(err.has_error()) << err.message();
80
81 // Defining a response file but not referencing it should fail.
82 err = Err();
83 TestParseInput input_missing_rsp_args(
84 R"(action_foreach("foo") {
85 script = "//foo.py"
86 sources = [ "//bar.txt" ]
87 outputs = [ "//out/Debug/{{source_name_part}}" ]
88 args = [ "{{source_name_part}}" ]
89 response_file_contents = [ "{{source_name_part}}" ]
90 })");
91 ASSERT_FALSE(input_missing_rsp_args.has_error());
92 input_missing_rsp_args.parsed()->Execute(setup.scope(), &err);
93 ASSERT_TRUE(err.has_error()) << err.message();
94
95 // Bad substitutions in args.
96 err = Err();
97 TestParseInput input_bad_args(
98 R"(action_foreach("foo") {
99 script = "//foo.py"
100 sources = [ "//bar.txt" ]
101 outputs = [ "//out/Debug/{{source_name_part}}" ]
102 args = [ "{{response_file_name}} {{ldflags}}" ]
103 response_file_contents = [ "{{source_name_part}}" ]
104 })");
105 ASSERT_FALSE(input_bad_args.has_error());
106 input_bad_args.parsed()->Execute(setup.scope(), &err);
107 ASSERT_TRUE(err.has_error()) << err.message();
108
109 // Bad substitutions in response file contents.
110 err = Err();
111 TestParseInput input_bad_rsp(
112 R"(action_foreach("foo") {
113 script = "//foo.py"
114 sources = [ "//bar.txt" ]
115 outputs = [ "//out/Debug/{{source_name_part}}" ]
116 args = [ "{{response_file_name}}" ]
117 response_file_contents = [ "{{source_name_part}} {{ldflags}}" ]
118 })");
119 ASSERT_FALSE(input_bad_rsp.has_error());
120 input_bad_rsp.parsed()->Execute(setup.scope(), &err);
121 ASSERT_TRUE(err.has_error()) << err.message();
122 }
123