blob: 3fbaa3832e225f7429f12d5c4783e06cc43e7504 [file] [log] [blame]
Chloe Calvarin7a8bd742017-03-21 16:11:22 +00001// Copyright 2017 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
ccalvarinf39dc6f2017-06-09 11:51:45 -040014package com.google.devtools.common.options;
Chloe Calvarin7a8bd742017-03-21 16:11:22 +000015
Googler7c7255e2017-06-27 20:05:20 +020016import com.google.common.collect.ImmutableList;
ccalvarinf39dc6f2017-06-09 11:51:45 -040017import com.google.devtools.common.options.InvocationPolicyEnforcerTestBase.ToListConverter;
Chloe Calvarin7a8bd742017-03-21 16:11:22 +000018import java.util.List;
ccalvarinc65147b2017-08-16 21:31:52 +020019import java.util.Map;
20import java.util.TreeSet;
Chloe Calvarin7a8bd742017-03-21 16:11:22 +000021
22/** Options for testing. */
23public class TestOptions extends OptionsBase {
24
25 /*
26 * Basic types
27 */
28
29 public static final String TEST_STRING_DEFAULT = "test string default";
30
ccalvarin835e8e32017-06-29 17:05:59 +020031 @Option(
32 name = "test_string",
33 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
34 effectTags = {OptionEffectTag.NO_OP},
ccalvarinc65147b2017-08-16 21:31:52 +020035 defaultValue = TEST_STRING_DEFAULT,
36 help = "a string-valued option to test simple option operations"
ccalvarin835e8e32017-06-29 17:05:59 +020037 )
Chloe Calvarin7a8bd742017-03-21 16:11:22 +000038 public String testString;
39
ccalvarin06e68742018-03-01 07:29:45 -080040 @Option(
41 name = "test_string_null_by_default",
42 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
43 effectTags = {OptionEffectTag.NO_OP},
44 defaultValue = "null",
45 help = "a string-valued option that has the special string 'null' as its default."
46 )
47 public String testStringNullByDefault;
48
Chloe Calvarin7a8bd742017-03-21 16:11:22 +000049 /*
50 * Repeated flags
51 */
52
53 @Option(
Googlerea0c1102020-03-27 09:30:59 -070054 name = "test_multiple_string",
55 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
56 effectTags = {OptionEffectTag.NO_OP},
57 defaultValue = "null",
58 allowMultiple = true,
59 help = "a repeatable string-valued flag with its own unhelpful help text")
Chloe Calvarin7a8bd742017-03-21 16:11:22 +000060 public List<String> testMultipleString;
61
62 /*
63 * Flags with converters that return lists
64 */
65
66 @Option(
Googlerea0c1102020-03-27 09:30:59 -070067 name = "test_list_converters",
68 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
69 effectTags = {OptionEffectTag.NO_OP},
70 defaultValue = "null",
71 allowMultiple = true,
72 converter = ToListConverter.class,
73 help =
74 "a repeatable flag that accepts lists, but doesn't want to have lists of lists "
75 + "as a final type")
Chloe Calvarin7a8bd742017-03-21 16:11:22 +000076 public List<String> testListConverters;
77
78 /*
79 * Expansion flags
80 */
81
82 public static final boolean EXPANDED_A_TEST_EXPANSION = false;
83 public static final boolean EXPANDED_B_TEST_EXPANSION = false;
84 public static final int EXPANDED_C_TEST_EXPANSION = 42;
85 public static final String EXPANDED_D_TEST_EXPANSION = "bar";
86
87 @Option(
ccalvarin835e8e32017-06-29 17:05:59 +020088 name = "test_expansion",
89 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
90 effectTags = {OptionEffectTag.NO_OP},
91 defaultValue = "null",
92 expansion = {
93 "--noexpanded_a",
94 "--expanded_b=false",
95 "--expanded_c",
96 "42",
97 "--expanded_d",
98 "bar"
ccalvarinc65147b2017-08-16 21:31:52 +020099 },
100 help = "this expands to an alphabet soup."
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000101 )
102 public Void testExpansion;
103
104 public static final boolean EXPANDED_A_TEST_RECURSIVE_EXPANSION = false;
105 public static final boolean EXPANDED_B_TEST_RECURSIVE_EXPANSION = false;
106 public static final int EXPANDED_C_TEST_RECURSIVE_EXPANSION = 56;
107 public static final String EXPANDED_D_TEST_RECURSIVE_EXPANSION = "baz";
108
109 @Option(
ccalvarin835e8e32017-06-29 17:05:59 +0200110 name = "test_recursive_expansion_top_level",
111 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
112 effectTags = {OptionEffectTag.NO_OP},
113 defaultValue = "null",
114 expansion = {
115 "--test_recursive_expansion_middle1",
116 "--test_recursive_expansion_middle2",
ccalvarinc65147b2017-08-16 21:31:52 +0200117 },
118 help = "Lets the children do all the work."
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000119 )
120 public Void testRecursiveExpansionTopLevel;
121
122 @Option(
ccalvarin835e8e32017-06-29 17:05:59 +0200123 name = "test_recursive_expansion_middle1",
124 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
125 effectTags = {OptionEffectTag.NO_OP},
126 defaultValue = "null",
127 expansion = {
128 "--expanded_a=false",
129 "--expanded_c=56",
130 }
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000131 )
132 public Void testRecursiveExpansionMiddle1;
133
134 @Option(
ccalvarin835e8e32017-06-29 17:05:59 +0200135 name = "test_recursive_expansion_middle2",
136 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
137 effectTags = {OptionEffectTag.NO_OP},
138 defaultValue = "null",
139 expansion = {
140 "--expanded_b=false",
141 "--expanded_d=baz",
142 }
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000143 )
144 public Void testRecursiveExpansionMiddle2;
145
146 public static final boolean EXPANDED_A_DEFAULT = true;
147
ccalvarin835e8e32017-06-29 17:05:59 +0200148 @Option(
149 name = "expanded_a",
150 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
ccalvarin77e3a5c2017-09-26 18:11:53 -0400151 effectTags = {OptionEffectTag.UNKNOWN},
152 defaultValue = "true",
153 help = "A boolean flag with unknown effect to test tagless usage text."
ccalvarin835e8e32017-06-29 17:05:59 +0200154 )
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000155 public boolean expandedA;
156
157 public static final boolean EXPANDED_B_DEFAULT = true;
158
ccalvarin835e8e32017-06-29 17:05:59 +0200159 @Option(
160 name = "expanded_b",
161 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
162 effectTags = {OptionEffectTag.NO_OP},
163 defaultValue = "true"
164 )
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000165 public boolean expandedB;
166
167 public static final int EXPANDED_C_DEFAULT = 12;
168
ccalvarin835e8e32017-06-29 17:05:59 +0200169 @Option(
170 name = "expanded_c",
171 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
172 effectTags = {OptionEffectTag.NO_OP},
ccalvarinc65147b2017-08-16 21:31:52 +0200173 defaultValue = "12",
174 help = "an int-value'd flag used to test expansion logic"
ccalvarin835e8e32017-06-29 17:05:59 +0200175 )
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000176 public int expandedC;
177
178 public static final String EXPANDED_D_DEFAULT = "foo";
179
ccalvarin835e8e32017-06-29 17:05:59 +0200180 @Option(
181 name = "expanded_d",
182 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
183 effectTags = {OptionEffectTag.NO_OP},
184 defaultValue = "foo"
185 )
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000186 public String expandedD;
187
188 /*
ccalvarinae1d0de2017-04-15 05:19:09 +0200189 * Expansion into repeatable flags.
190 */
191
192 public static final String EXPANDED_MULTIPLE_1 = "expandedFirstValue";
193 public static final String EXPANDED_MULTIPLE_2 = "expandedSecondValue";
194
ccalvarin835e8e32017-06-29 17:05:59 +0200195 @Option(
196 name = "test_expansion_to_repeatable",
197 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
198 effectTags = {OptionEffectTag.NO_OP},
199 defaultValue = "null",
200 expansion = {
201 "--test_multiple_string=expandedFirstValue",
202 "--test_multiple_string=expandedSecondValue"
ccalvarinc65147b2017-08-16 21:31:52 +0200203 },
204 help = "Go forth and multiply, they said."
ccalvarinae1d0de2017-04-15 05:19:09 +0200205 )
206 public Void testExpansionToRepeatable;
207
ccalvarinae1d0de2017-04-15 05:19:09 +0200208 /*
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000209 * Implicit requirement flags
210 */
211
212 public static final String TEST_IMPLICIT_REQUIREMENT_DEFAULT = "direct implicit";
213 public static final String IMPLICIT_REQUIREMENT_A_REQUIRED = "implicit requirement, required";
214
215 @Option(
ccalvarin835e8e32017-06-29 17:05:59 +0200216 name = "test_implicit_requirement",
217 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
218 effectTags = {OptionEffectTag.NO_OP},
219 defaultValue = TEST_IMPLICIT_REQUIREMENT_DEFAULT,
ccalvarinc65147b2017-08-16 21:31:52 +0200220 implicitRequirements = {"--implicit_requirement_a=" + IMPLICIT_REQUIREMENT_A_REQUIRED},
221 help = "this option really needs that other one, isolation of purpose has failed."
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000222 )
223 public String testImplicitRequirement;
224
225 public static final String IMPLICIT_REQUIREMENT_A_DEFAULT = "implicit requirement, unrequired";
226
ccalvarin835e8e32017-06-29 17:05:59 +0200227 @Option(
228 name = "implicit_requirement_a",
229 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
230 effectTags = {OptionEffectTag.NO_OP},
231 defaultValue = IMPLICIT_REQUIREMENT_A_DEFAULT
232 )
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000233 public String implicitRequirementA;
234
235 public static final String TEST_RECURSIVE_IMPLICIT_REQUIREMENT_DEFAULT = "recursive implicit";
236 public static final String TEST_IMPLICIT_REQUIREMENT_REQUIRED = "intermediate, required";
237
238 @Option(
ccalvarin835e8e32017-06-29 17:05:59 +0200239 name = "test_recursive_implicit_requirement",
240 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
241 effectTags = {OptionEffectTag.NO_OP},
242 defaultValue = TEST_RECURSIVE_IMPLICIT_REQUIREMENT_DEFAULT,
243 implicitRequirements = {"--test_implicit_requirement=" + TEST_IMPLICIT_REQUIREMENT_REQUIRED}
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000244 )
245 public String testRecursiveImplicitRequirement;
Googler7c7255e2017-06-27 20:05:20 +0200246
Googler7c7255e2017-06-27 20:05:20 +0200247 public static final String EXPANDED_D_VOID_EXPANSION_FUNCTION_VALUE = "void expanded";
248
249 /** Used for testing an expansion flag that doesn't requires a value. */
250 public static class TestVoidExpansionFunction implements ExpansionFunction {
251 @Override
ccalvarin34a9fea2017-10-17 23:27:19 +0200252 public ImmutableList<String> getExpansion(IsolatedOptionsData optionsData) {
Googler7c7255e2017-06-27 20:05:20 +0200253 return ImmutableList.of("--expanded_d", EXPANDED_D_VOID_EXPANSION_FUNCTION_VALUE);
254 }
255 }
256
257 @Option(
258 name = "test_void_expansion_function",
259 defaultValue = "null",
ccalvarin59a0e4f2017-06-30 16:01:36 +0200260 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
ccalvarin77e3a5c2017-09-26 18:11:53 -0400261 effectTags = {
gregcee1b09f22017-10-04 18:11:22 +0200262 OptionEffectTag.ACTION_COMMAND_LINES,
ccalvarin77e3a5c2017-09-26 18:11:53 -0400263 OptionEffectTag.TEST_RUNNER,
264 OptionEffectTag.TERMINAL_OUTPUT
265 },
266 metadataTags = {OptionMetadataTag.EXPERIMENTAL},
267 expansionFunction = TestVoidExpansionFunction.class,
268 help = "Listing a ton of random tags to test the usage output."
Googler7c7255e2017-06-27 20:05:20 +0200269 )
270 public Void testVoidExpansionFunction;
ccalvarinc65147b2017-08-16 21:31:52 +0200271
272 // Interestingly, the class needs to be public, or else the default constructor ends up not
273 // being public and the expander can't be instantiated.
274 /**
275 * Defines an expansion function that looks at other options defined with it and expands to
276 * options that match a pattern.
277 */
278 public static class ExpansionDependsOnOtherOptionDefinitions implements ExpansionFunction {
279 @Override
ccalvarin34a9fea2017-10-17 23:27:19 +0200280 public ImmutableList<String> getExpansion(IsolatedOptionsData optionsData) {
ccalvarinc65147b2017-08-16 21:31:52 +0200281 TreeSet<String> flags = new TreeSet<>();
ccalvarin34a9fea2017-10-17 23:27:19 +0200282 for (Map.Entry<String, ?> entry : optionsData.getAllOptionDefinitions()) {
ccalvarinc65147b2017-08-16 21:31:52 +0200283 if (entry.getKey().startsWith("specialexp_")) {
284 flags.add("--" + entry.getKey());
285 }
286 }
287 return ImmutableList.copyOf(flags);
288 }
289 }
290
291 @Option(
292 name = "prefix_expansion",
293 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
294 effectTags = {OptionEffectTag.NO_OP},
295 defaultValue = "null",
296 expansionFunction = ExpansionDependsOnOtherOptionDefinitions.class,
297 help = "Expands to all options with a specific prefix."
298 )
299 public Void specialExp;
300
ccalvarinc65147b2017-08-16 21:31:52 +0200301 @Option(
302 name = "specialexp_foo",
303 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
304 effectTags = {OptionEffectTag.NO_OP},
305 defaultValue = "false"
306 )
307 public boolean specialExpFoo;
308
309 @Option(
310 name = "specialexp_bar",
311 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
312 effectTags = {OptionEffectTag.NO_OP},
313 defaultValue = "false"
314 )
315 public boolean specialExpBar;
juliexxia9cd6fa22018-08-27 11:42:51 -0700316
317 @Option(
318 name = "test_deprecated",
319 defaultValue = "default",
320 deprecationWarning = "Flag for testing deprecation behavior.",
321 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
322 effectTags = {OptionEffectTag.NO_OP})
323 public String testDeprecated;
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000324}