Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 1 | // Copyright 2015 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. |
| 14 | package com.google.devtools.build.lib.runtime; |
| 15 | |
Carmi Grushko | fd8acab | 2015-11-10 17:19:13 +0000 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
Carmi Grushko | fd8acab | 2015-11-10 17:19:13 +0000 | [diff] [blame] | 17 | |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider; |
adonovan | 240bdea | 2020-09-03 15:24:12 -0700 | [diff] [blame] | 20 | import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions; |
Luis Fernando Pino Duque | 3fedf9e | 2016-04-28 15:47:29 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.testutil.TestConstants; |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 22 | import com.google.devtools.common.options.Option; |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 23 | import com.google.devtools.common.options.OptionDocumentationCategory; |
ccalvarin | c82a197 | 2017-07-17 21:13:39 +0200 | [diff] [blame] | 24 | import com.google.devtools.common.options.OptionEffectTag; |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 25 | import com.google.devtools.common.options.OptionsBase; |
| 26 | import com.google.devtools.common.options.OptionsParser; |
juliexxia | e91a450 | 2018-08-15 14:42:29 -0700 | [diff] [blame] | 27 | import com.google.devtools.common.options.OptionsParsingResult; |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 28 | import java.util.ArrayList; |
| 29 | import java.util.Collection; |
| 30 | import java.util.Collections; |
| 31 | import java.util.List; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 32 | import org.junit.Test; |
| 33 | import org.junit.runner.RunWith; |
| 34 | import org.junit.runners.JUnit4; |
| 35 | |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 36 | /** |
| 37 | * Tests {@link BlazeCommand}. |
| 38 | */ |
| 39 | @RunWith(JUnit4.class) |
| 40 | public class AbstractCommandTest { |
| 41 | |
| 42 | public static class FooOptions extends OptionsBase { |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 43 | @Option( |
| 44 | name = "foo", |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 45 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 46 | effectTags = {OptionEffectTag.NO_OP}, |
| 47 | defaultValue = "0" |
| 48 | ) |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 49 | public int foo; |
| 50 | } |
| 51 | |
| 52 | public static class BarOptions extends OptionsBase { |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 53 | @Option( |
| 54 | name = "bar", |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 55 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 56 | effectTags = {OptionEffectTag.NO_OP}, |
| 57 | defaultValue = "42" |
| 58 | ) |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 59 | public int foo; |
| 60 | |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 61 | @Option( |
| 62 | name = "baz", |
ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 63 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 64 | effectTags = {OptionEffectTag.NO_OP}, |
| 65 | defaultValue = "oops" |
| 66 | ) |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 67 | public String baz; |
| 68 | } |
| 69 | |
| 70 | private static class ConcreteCommand implements BlazeCommand { |
| 71 | @Override |
juliexxia | e91a450 | 2018-08-15 14:42:29 -0700 | [diff] [blame] | 72 | public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult options) { |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 73 | throw new UnsupportedOperationException(); |
| 74 | } |
| 75 | |
| 76 | @Override |
ulfjack | 95aa487 | 2017-06-06 09:32:18 -0400 | [diff] [blame] | 77 | public void editOptions(OptionsParser optionsParser) {} |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Command(name = "test_name", |
| 81 | help = "Usage: some funny usage for %{command} ...;\n\n%{options}; end", |
| 82 | options = {FooOptions.class, BarOptions.class}, |
| 83 | shortDescription = "a short description", |
| 84 | allowResidue = false) |
| 85 | private static class TestCommand extends ConcreteCommand {} |
| 86 | |
| 87 | @Test |
| 88 | public void testGetNameYieldsAnnotatedName() { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 89 | assertThat(new TestCommand().getClass().getAnnotation(Command.class).name()) |
| 90 | .isEqualTo("test_name"); |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testGetOptionsYieldsAnnotatedOptions() { |
| 95 | ConfiguredRuleClassProvider ruleClassProvider = new ConfiguredRuleClassProvider.Builder() |
Luis Fernando Pino Duque | 3fedf9e | 2016-04-28 15:47:29 +0000 | [diff] [blame] | 96 | .setToolsRepository(TestConstants.TOOLS_REPOSITORY) |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 97 | .build(); |
| 98 | |
Carmi Grushko | fd8acab | 2015-11-10 17:19:13 +0000 | [diff] [blame] | 99 | assertThat( |
| 100 | BlazeCommandUtils.getOptions( |
| 101 | TestCommand.class, ImmutableList.<BlazeModule>of(), ruleClassProvider)) |
| 102 | .containsExactlyElementsIn(optionClassesWithDefault(FooOptions.class, BarOptions.class)); |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /*************************************************************************** |
| 106 | * The tests below test how a command interacts with the dispatcher except * |
| 107 | * for execution, which is tested in {@link BlazeCommandDispatcherTest}. * |
| 108 | ***************************************************************************/ |
| 109 | |
| 110 | @Command(name = "a", options = {FooOptions.class}, shortDescription = "", help = "") |
| 111 | private static class CommandA extends ConcreteCommand {} |
| 112 | |
Googler | 6afaade | 2024-06-20 02:15:28 -0700 | [diff] [blame] | 113 | @Command( |
| 114 | name = "b", |
| 115 | options = {BarOptions.class}, |
| 116 | inheritsOptionsFrom = {CommandA.class}, |
| 117 | shortDescription = "", |
| 118 | help = "") |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 119 | private static class CommandB extends ConcreteCommand {} |
| 120 | |
| 121 | @Test |
| 122 | public void testOptionsAreInherited() { |
| 123 | ConfiguredRuleClassProvider ruleClassProvider = new ConfiguredRuleClassProvider.Builder() |
Luis Fernando Pino Duque | 3fedf9e | 2016-04-28 15:47:29 +0000 | [diff] [blame] | 124 | .setToolsRepository(TestConstants.TOOLS_REPOSITORY) |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 125 | .build(); |
Carmi Grushko | fd8acab | 2015-11-10 17:19:13 +0000 | [diff] [blame] | 126 | assertThat( |
| 127 | BlazeCommandUtils.getOptions( |
| 128 | CommandA.class, ImmutableList.<BlazeModule>of(), ruleClassProvider)) |
| 129 | .containsExactlyElementsIn(optionClassesWithDefault(FooOptions.class)); |
| 130 | assertThat( |
| 131 | BlazeCommandUtils.getOptions( |
| 132 | CommandB.class, ImmutableList.<BlazeModule>of(), ruleClassProvider)) |
| 133 | .containsExactlyElementsIn(optionClassesWithDefault(FooOptions.class, BarOptions.class)); |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | private Collection<Class<?>> optionClassesWithDefault(Class<?>... optionClasses) { |
| 137 | List<Class<?>> result = new ArrayList<>(); |
| 138 | Collections.addAll(result, optionClasses); |
ulfjack | b1294b4 | 2019-07-25 07:11:06 -0700 | [diff] [blame] | 139 | result.add(UiOptions.class); |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 140 | result.add(CommonCommandOptions.class); |
ccalvarin | b5158a9 | 2017-10-26 23:41:08 +0200 | [diff] [blame] | 141 | result.add(ClientOptions.class); |
adonovan | 240bdea | 2020-09-03 15:24:12 -0700 | [diff] [blame] | 142 | result.add(BuildLanguageOptions.class); |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 143 | return result; |
| 144 | } |
| 145 | } |