Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 1 | // 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. |
ccalvarin | f39dc6f | 2017-06-09 11:51:45 -0400 | [diff] [blame] | 14 | package com.google.devtools.common.options; |
Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 15 | |
Googler | 7c7255e | 2017-06-27 20:05:20 +0200 | [diff] [blame] | 16 | import com.google.common.collect.ImmutableList; |
ccalvarin | f39dc6f | 2017-06-09 11:51:45 -0400 | [diff] [blame] | 17 | import com.google.devtools.common.options.InvocationPolicyEnforcerTestBase.ToListConverter; |
Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 18 | import java.util.List; |
ccalvarin | c65147b | 2017-08-16 21:31:52 +0200 | [diff] [blame] | 19 | import java.util.Map; |
| 20 | import java.util.TreeSet; |
Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 21 | |
| 22 | /** Options for testing. */ |
| 23 | public class TestOptions extends OptionsBase { |
| 24 | |
| 25 | /* |
| 26 | * Basic types |
| 27 | */ |
| 28 | |
| 29 | public static final String TEST_STRING_DEFAULT = "test string default"; |
| 30 | |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 31 | @Option( |
| 32 | name = "test_string", |
| 33 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 34 | effectTags = {OptionEffectTag.NO_OP}, |
ccalvarin | c65147b | 2017-08-16 21:31:52 +0200 | [diff] [blame] | 35 | defaultValue = TEST_STRING_DEFAULT, |
| 36 | help = "a string-valued option to test simple option operations" |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 37 | ) |
Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 38 | public String testString; |
| 39 | |
ccalvarin | 06e6874 | 2018-03-01 07:29:45 -0800 | [diff] [blame] | 40 | @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 Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 49 | /* |
| 50 | * Repeated flags |
| 51 | */ |
| 52 | |
| 53 | @Option( |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 54 | name = "test_multiple_string", |
| 55 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 56 | effectTags = {OptionEffectTag.NO_OP}, |
| 57 | defaultValue = "", // default value is ignored when allowMultiple=true. |
ccalvarin | c65147b | 2017-08-16 21:31:52 +0200 | [diff] [blame] | 58 | allowMultiple = true, |
| 59 | help = "a repeatable string-valued flag with its own unhelpful help text" |
Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 60 | ) |
| 61 | public List<String> testMultipleString; |
| 62 | |
| 63 | /* |
| 64 | * Flags with converters that return lists |
| 65 | */ |
| 66 | |
| 67 | @Option( |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 68 | name = "test_list_converters", |
| 69 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 70 | effectTags = {OptionEffectTag.NO_OP}, |
| 71 | defaultValue = "", |
| 72 | allowMultiple = true, |
ccalvarin | c65147b | 2017-08-16 21:31:52 +0200 | [diff] [blame] | 73 | 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 Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 77 | ) |
| 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( |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 90 | 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" |
ccalvarin | c65147b | 2017-08-16 21:31:52 +0200 | [diff] [blame] | 101 | }, |
| 102 | help = "this expands to an alphabet soup." |
Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 103 | ) |
| 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( |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 112 | 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", |
ccalvarin | c65147b | 2017-08-16 21:31:52 +0200 | [diff] [blame] | 119 | }, |
| 120 | help = "Lets the children do all the work." |
Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 121 | ) |
| 122 | public Void testRecursiveExpansionTopLevel; |
| 123 | |
| 124 | @Option( |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 125 | 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 Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 133 | ) |
| 134 | public Void testRecursiveExpansionMiddle1; |
| 135 | |
| 136 | @Option( |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 137 | 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 Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 145 | ) |
| 146 | public Void testRecursiveExpansionMiddle2; |
| 147 | |
| 148 | public static final boolean EXPANDED_A_DEFAULT = true; |
| 149 | |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 150 | @Option( |
| 151 | name = "expanded_a", |
| 152 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
ccalvarin | 77e3a5c | 2017-09-26 18:11:53 -0400 | [diff] [blame] | 153 | effectTags = {OptionEffectTag.UNKNOWN}, |
| 154 | defaultValue = "true", |
| 155 | help = "A boolean flag with unknown effect to test tagless usage text." |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 156 | ) |
Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 157 | public boolean expandedA; |
| 158 | |
| 159 | public static final boolean EXPANDED_B_DEFAULT = true; |
| 160 | |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 161 | @Option( |
| 162 | name = "expanded_b", |
| 163 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 164 | effectTags = {OptionEffectTag.NO_OP}, |
| 165 | defaultValue = "true" |
| 166 | ) |
Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 167 | public boolean expandedB; |
| 168 | |
| 169 | public static final int EXPANDED_C_DEFAULT = 12; |
| 170 | |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 171 | @Option( |
| 172 | name = "expanded_c", |
| 173 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 174 | effectTags = {OptionEffectTag.NO_OP}, |
ccalvarin | c65147b | 2017-08-16 21:31:52 +0200 | [diff] [blame] | 175 | defaultValue = "12", |
| 176 | help = "an int-value'd flag used to test expansion logic" |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 177 | ) |
Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 178 | public int expandedC; |
| 179 | |
| 180 | public static final String EXPANDED_D_DEFAULT = "foo"; |
| 181 | |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 182 | @Option( |
| 183 | name = "expanded_d", |
| 184 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 185 | effectTags = {OptionEffectTag.NO_OP}, |
| 186 | defaultValue = "foo" |
| 187 | ) |
Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 188 | public String expandedD; |
| 189 | |
| 190 | /* |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 191 | * 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 | |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 197 | @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" |
ccalvarin | c65147b | 2017-08-16 21:31:52 +0200 | [diff] [blame] | 205 | }, |
| 206 | help = "Go forth and multiply, they said." |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 207 | ) |
| 208 | public Void testExpansionToRepeatable; |
| 209 | |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 210 | /* |
Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 211 | * 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( |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 218 | name = "test_implicit_requirement", |
| 219 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 220 | effectTags = {OptionEffectTag.NO_OP}, |
| 221 | defaultValue = TEST_IMPLICIT_REQUIREMENT_DEFAULT, |
ccalvarin | c65147b | 2017-08-16 21:31:52 +0200 | [diff] [blame] | 222 | implicitRequirements = {"--implicit_requirement_a=" + IMPLICIT_REQUIREMENT_A_REQUIRED}, |
| 223 | help = "this option really needs that other one, isolation of purpose has failed." |
Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 224 | ) |
| 225 | public String testImplicitRequirement; |
| 226 | |
| 227 | public static final String IMPLICIT_REQUIREMENT_A_DEFAULT = "implicit requirement, unrequired"; |
| 228 | |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 229 | @Option( |
| 230 | name = "implicit_requirement_a", |
| 231 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 232 | effectTags = {OptionEffectTag.NO_OP}, |
| 233 | defaultValue = IMPLICIT_REQUIREMENT_A_DEFAULT |
| 234 | ) |
Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 235 | 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( |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 241 | 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 Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 246 | ) |
| 247 | public String testRecursiveImplicitRequirement; |
Googler | 7c7255e | 2017-06-27 20:05:20 +0200 | [diff] [blame] | 248 | |
Googler | 7c7255e | 2017-06-27 20:05:20 +0200 | [diff] [blame] | 249 | 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 |
ccalvarin | 34a9fea | 2017-10-17 23:27:19 +0200 | [diff] [blame] | 254 | public ImmutableList<String> getExpansion(IsolatedOptionsData optionsData) { |
Googler | 7c7255e | 2017-06-27 20:05:20 +0200 | [diff] [blame] | 255 | 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", |
ccalvarin | 59a0e4f | 2017-06-30 16:01:36 +0200 | [diff] [blame] | 262 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
ccalvarin | 77e3a5c | 2017-09-26 18:11:53 -0400 | [diff] [blame] | 263 | effectTags = { |
gregce | e1b09f2 | 2017-10-04 18:11:22 +0200 | [diff] [blame] | 264 | OptionEffectTag.ACTION_COMMAND_LINES, |
ccalvarin | 77e3a5c | 2017-09-26 18:11:53 -0400 | [diff] [blame] | 265 | 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." |
Googler | 7c7255e | 2017-06-27 20:05:20 +0200 | [diff] [blame] | 271 | ) |
| 272 | public Void testVoidExpansionFunction; |
ccalvarin | c65147b | 2017-08-16 21:31:52 +0200 | [diff] [blame] | 273 | |
| 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 |
ccalvarin | 34a9fea | 2017-10-17 23:27:19 +0200 | [diff] [blame] | 282 | public ImmutableList<String> getExpansion(IsolatedOptionsData optionsData) { |
ccalvarin | c65147b | 2017-08-16 21:31:52 +0200 | [diff] [blame] | 283 | TreeSet<String> flags = new TreeSet<>(); |
ccalvarin | 34a9fea | 2017-10-17 23:27:19 +0200 | [diff] [blame] | 284 | for (Map.Entry<String, ?> entry : optionsData.getAllOptionDefinitions()) { |
ccalvarin | c65147b | 2017-08-16 21:31:52 +0200 | [diff] [blame] | 285 | 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 | |
ccalvarin | c65147b | 2017-08-16 21:31:52 +0200 | [diff] [blame] | 303 | @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; |
juliexxia | 9cd6fa2 | 2018-08-27 11:42:51 -0700 | [diff] [blame^] | 318 | |
| 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 Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 326 | } |