blob: d006240f270a798d93ec118a82a3efa8b6142e37 [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(
ccalvarin835e8e32017-06-29 17:05:59 +020054 name = "test_multiple_string",
55 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
56 effectTags = {OptionEffectTag.NO_OP},
57 defaultValue = "", // default value is ignored when allowMultiple=true.
ccalvarinc65147b2017-08-16 21:31:52 +020058 allowMultiple = true,
59 help = "a repeatable string-valued flag with its own unhelpful help text"
Chloe Calvarin7a8bd742017-03-21 16:11:22 +000060 )
61 public List<String> testMultipleString;
62
63 /*
64 * Flags with converters that return lists
65 */
66
67 @Option(
ccalvarin835e8e32017-06-29 17:05:59 +020068 name = "test_list_converters",
69 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
70 effectTags = {OptionEffectTag.NO_OP},
71 defaultValue = "",
72 allowMultiple = true,
ccalvarinc65147b2017-08-16 21:31:52 +020073 converter = ToListConverter.class,
74 help =
75 "a repeatable flag that accepts lists, but doesn't want to have lists of lists "
76 + "as a final type"
Chloe Calvarin7a8bd742017-03-21 16:11:22 +000077 )
78 public List<String> testListConverters;
79
80 /*
81 * Expansion flags
82 */
83
84 public static final boolean EXPANDED_A_TEST_EXPANSION = false;
85 public static final boolean EXPANDED_B_TEST_EXPANSION = false;
86 public static final int EXPANDED_C_TEST_EXPANSION = 42;
87 public static final String EXPANDED_D_TEST_EXPANSION = "bar";
88
89 @Option(
ccalvarin835e8e32017-06-29 17:05:59 +020090 name = "test_expansion",
91 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
92 effectTags = {OptionEffectTag.NO_OP},
93 defaultValue = "null",
94 expansion = {
95 "--noexpanded_a",
96 "--expanded_b=false",
97 "--expanded_c",
98 "42",
99 "--expanded_d",
100 "bar"
ccalvarinc65147b2017-08-16 21:31:52 +0200101 },
102 help = "this expands to an alphabet soup."
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000103 )
104 public Void testExpansion;
105
106 public static final boolean EXPANDED_A_TEST_RECURSIVE_EXPANSION = false;
107 public static final boolean EXPANDED_B_TEST_RECURSIVE_EXPANSION = false;
108 public static final int EXPANDED_C_TEST_RECURSIVE_EXPANSION = 56;
109 public static final String EXPANDED_D_TEST_RECURSIVE_EXPANSION = "baz";
110
111 @Option(
ccalvarin835e8e32017-06-29 17:05:59 +0200112 name = "test_recursive_expansion_top_level",
113 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
114 effectTags = {OptionEffectTag.NO_OP},
115 defaultValue = "null",
116 expansion = {
117 "--test_recursive_expansion_middle1",
118 "--test_recursive_expansion_middle2",
ccalvarinc65147b2017-08-16 21:31:52 +0200119 },
120 help = "Lets the children do all the work."
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000121 )
122 public Void testRecursiveExpansionTopLevel;
123
124 @Option(
ccalvarin835e8e32017-06-29 17:05:59 +0200125 name = "test_recursive_expansion_middle1",
126 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
127 effectTags = {OptionEffectTag.NO_OP},
128 defaultValue = "null",
129 expansion = {
130 "--expanded_a=false",
131 "--expanded_c=56",
132 }
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000133 )
134 public Void testRecursiveExpansionMiddle1;
135
136 @Option(
ccalvarin835e8e32017-06-29 17:05:59 +0200137 name = "test_recursive_expansion_middle2",
138 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
139 effectTags = {OptionEffectTag.NO_OP},
140 defaultValue = "null",
141 expansion = {
142 "--expanded_b=false",
143 "--expanded_d=baz",
144 }
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000145 )
146 public Void testRecursiveExpansionMiddle2;
147
148 public static final boolean EXPANDED_A_DEFAULT = true;
149
ccalvarin835e8e32017-06-29 17:05:59 +0200150 @Option(
151 name = "expanded_a",
152 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
ccalvarin77e3a5c2017-09-26 18:11:53 -0400153 effectTags = {OptionEffectTag.UNKNOWN},
154 defaultValue = "true",
155 help = "A boolean flag with unknown effect to test tagless usage text."
ccalvarin835e8e32017-06-29 17:05:59 +0200156 )
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000157 public boolean expandedA;
158
159 public static final boolean EXPANDED_B_DEFAULT = true;
160
ccalvarin835e8e32017-06-29 17:05:59 +0200161 @Option(
162 name = "expanded_b",
163 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
164 effectTags = {OptionEffectTag.NO_OP},
165 defaultValue = "true"
166 )
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000167 public boolean expandedB;
168
169 public static final int EXPANDED_C_DEFAULT = 12;
170
ccalvarin835e8e32017-06-29 17:05:59 +0200171 @Option(
172 name = "expanded_c",
173 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
174 effectTags = {OptionEffectTag.NO_OP},
ccalvarinc65147b2017-08-16 21:31:52 +0200175 defaultValue = "12",
176 help = "an int-value'd flag used to test expansion logic"
ccalvarin835e8e32017-06-29 17:05:59 +0200177 )
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000178 public int expandedC;
179
180 public static final String EXPANDED_D_DEFAULT = "foo";
181
ccalvarin835e8e32017-06-29 17:05:59 +0200182 @Option(
183 name = "expanded_d",
184 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
185 effectTags = {OptionEffectTag.NO_OP},
186 defaultValue = "foo"
187 )
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000188 public String expandedD;
189
190 /*
ccalvarinae1d0de2017-04-15 05:19:09 +0200191 * Expansion into repeatable flags.
192 */
193
194 public static final String EXPANDED_MULTIPLE_1 = "expandedFirstValue";
195 public static final String EXPANDED_MULTIPLE_2 = "expandedSecondValue";
196
ccalvarin835e8e32017-06-29 17:05:59 +0200197 @Option(
198 name = "test_expansion_to_repeatable",
199 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
200 effectTags = {OptionEffectTag.NO_OP},
201 defaultValue = "null",
202 expansion = {
203 "--test_multiple_string=expandedFirstValue",
204 "--test_multiple_string=expandedSecondValue"
ccalvarinc65147b2017-08-16 21:31:52 +0200205 },
206 help = "Go forth and multiply, they said."
ccalvarinae1d0de2017-04-15 05:19:09 +0200207 )
208 public Void testExpansionToRepeatable;
209
ccalvarinae1d0de2017-04-15 05:19:09 +0200210 /*
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000211 * Implicit requirement flags
212 */
213
214 public static final String TEST_IMPLICIT_REQUIREMENT_DEFAULT = "direct implicit";
215 public static final String IMPLICIT_REQUIREMENT_A_REQUIRED = "implicit requirement, required";
216
217 @Option(
ccalvarin835e8e32017-06-29 17:05:59 +0200218 name = "test_implicit_requirement",
219 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
220 effectTags = {OptionEffectTag.NO_OP},
221 defaultValue = TEST_IMPLICIT_REQUIREMENT_DEFAULT,
ccalvarinc65147b2017-08-16 21:31:52 +0200222 implicitRequirements = {"--implicit_requirement_a=" + IMPLICIT_REQUIREMENT_A_REQUIRED},
223 help = "this option really needs that other one, isolation of purpose has failed."
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000224 )
225 public String testImplicitRequirement;
226
227 public static final String IMPLICIT_REQUIREMENT_A_DEFAULT = "implicit requirement, unrequired";
228
ccalvarin835e8e32017-06-29 17:05:59 +0200229 @Option(
230 name = "implicit_requirement_a",
231 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
232 effectTags = {OptionEffectTag.NO_OP},
233 defaultValue = IMPLICIT_REQUIREMENT_A_DEFAULT
234 )
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000235 public String implicitRequirementA;
236
237 public static final String TEST_RECURSIVE_IMPLICIT_REQUIREMENT_DEFAULT = "recursive implicit";
238 public static final String TEST_IMPLICIT_REQUIREMENT_REQUIRED = "intermediate, required";
239
240 @Option(
ccalvarin835e8e32017-06-29 17:05:59 +0200241 name = "test_recursive_implicit_requirement",
242 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
243 effectTags = {OptionEffectTag.NO_OP},
244 defaultValue = TEST_RECURSIVE_IMPLICIT_REQUIREMENT_DEFAULT,
245 implicitRequirements = {"--test_implicit_requirement=" + TEST_IMPLICIT_REQUIREMENT_REQUIRED}
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000246 )
247 public String testRecursiveImplicitRequirement;
Googler7c7255e2017-06-27 20:05:20 +0200248
Googler7c7255e2017-06-27 20:05:20 +0200249 public static final String EXPANDED_D_VOID_EXPANSION_FUNCTION_VALUE = "void expanded";
250
251 /** Used for testing an expansion flag that doesn't requires a value. */
252 public static class TestVoidExpansionFunction implements ExpansionFunction {
253 @Override
ccalvarin34a9fea2017-10-17 23:27:19 +0200254 public ImmutableList<String> getExpansion(IsolatedOptionsData optionsData) {
Googler7c7255e2017-06-27 20:05:20 +0200255 return ImmutableList.of("--expanded_d", EXPANDED_D_VOID_EXPANSION_FUNCTION_VALUE);
256 }
257 }
258
259 @Option(
260 name = "test_void_expansion_function",
261 defaultValue = "null",
ccalvarin59a0e4f2017-06-30 16:01:36 +0200262 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
ccalvarin77e3a5c2017-09-26 18:11:53 -0400263 effectTags = {
gregcee1b09f22017-10-04 18:11:22 +0200264 OptionEffectTag.ACTION_COMMAND_LINES,
ccalvarin77e3a5c2017-09-26 18:11:53 -0400265 OptionEffectTag.TEST_RUNNER,
266 OptionEffectTag.TERMINAL_OUTPUT
267 },
268 metadataTags = {OptionMetadataTag.EXPERIMENTAL},
269 expansionFunction = TestVoidExpansionFunction.class,
270 help = "Listing a ton of random tags to test the usage output."
Googler7c7255e2017-06-27 20:05:20 +0200271 )
272 public Void testVoidExpansionFunction;
ccalvarinc65147b2017-08-16 21:31:52 +0200273
274 // Interestingly, the class needs to be public, or else the default constructor ends up not
275 // being public and the expander can't be instantiated.
276 /**
277 * Defines an expansion function that looks at other options defined with it and expands to
278 * options that match a pattern.
279 */
280 public static class ExpansionDependsOnOtherOptionDefinitions implements ExpansionFunction {
281 @Override
ccalvarin34a9fea2017-10-17 23:27:19 +0200282 public ImmutableList<String> getExpansion(IsolatedOptionsData optionsData) {
ccalvarinc65147b2017-08-16 21:31:52 +0200283 TreeSet<String> flags = new TreeSet<>();
ccalvarin34a9fea2017-10-17 23:27:19 +0200284 for (Map.Entry<String, ?> entry : optionsData.getAllOptionDefinitions()) {
ccalvarinc65147b2017-08-16 21:31:52 +0200285 if (entry.getKey().startsWith("specialexp_")) {
286 flags.add("--" + entry.getKey());
287 }
288 }
289 return ImmutableList.copyOf(flags);
290 }
291 }
292
293 @Option(
294 name = "prefix_expansion",
295 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
296 effectTags = {OptionEffectTag.NO_OP},
297 defaultValue = "null",
298 expansionFunction = ExpansionDependsOnOtherOptionDefinitions.class,
299 help = "Expands to all options with a specific prefix."
300 )
301 public Void specialExp;
302
ccalvarinc65147b2017-08-16 21:31:52 +0200303 @Option(
304 name = "specialexp_foo",
305 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
306 effectTags = {OptionEffectTag.NO_OP},
307 defaultValue = "false"
308 )
309 public boolean specialExpFoo;
310
311 @Option(
312 name = "specialexp_bar",
313 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
314 effectTags = {OptionEffectTag.NO_OP},
315 defaultValue = "false"
316 )
317 public boolean specialExpBar;
juliexxia9cd6fa22018-08-27 11:42:51 -0700318
319 @Option(
320 name = "test_deprecated",
321 defaultValue = "default",
322 deprecationWarning = "Flag for testing deprecation behavior.",
323 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
324 effectTags = {OptionEffectTag.NO_OP})
325 public String testDeprecated;
Chloe Calvarin7a8bd742017-03-21 16:11:22 +0000326}