blob: 867d0df78a7e312ee42a31f618c6153aa0d8d41e [file] [log] [blame]
Kristina Chodorowaa8b1522015-10-12 14:43:05 +00001// 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.
14package com.google.devtools.build.lib.runtime;
15
Carmi Grushkofd8acab2015-11-10 17:19:13 +000016import static com.google.common.truth.Truth.assertThat;
Carmi Grushkofd8acab2015-11-10 17:19:13 +000017
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000018import com.google.common.collect.ImmutableList;
19import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider;
adonovan240bdea2020-09-03 15:24:12 -070020import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
Luis Fernando Pino Duque3fedf9e2016-04-28 15:47:29 +000021import com.google.devtools.build.lib.testutil.TestConstants;
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000022import com.google.devtools.common.options.Option;
ccalvarin835e8e32017-06-29 17:05:59 +020023import com.google.devtools.common.options.OptionDocumentationCategory;
ccalvarinc82a1972017-07-17 21:13:39 +020024import com.google.devtools.common.options.OptionEffectTag;
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000025import com.google.devtools.common.options.OptionsBase;
26import com.google.devtools.common.options.OptionsParser;
juliexxiae91a4502018-08-15 14:42:29 -070027import com.google.devtools.common.options.OptionsParsingResult;
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000028import java.util.ArrayList;
29import java.util.Collection;
30import java.util.Collections;
31import java.util.List;
lberkiaea56b32017-05-30 12:35:33 +020032import org.junit.Test;
33import org.junit.runner.RunWith;
34import org.junit.runners.JUnit4;
35
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000036/**
37 * Tests {@link BlazeCommand}.
38 */
39@RunWith(JUnit4.class)
40public class AbstractCommandTest {
41
42 public static class FooOptions extends OptionsBase {
ccalvarin835e8e32017-06-29 17:05:59 +020043 @Option(
44 name = "foo",
ccalvarin835e8e32017-06-29 17:05:59 +020045 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
46 effectTags = {OptionEffectTag.NO_OP},
47 defaultValue = "0"
48 )
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000049 public int foo;
50 }
51
52 public static class BarOptions extends OptionsBase {
ccalvarin835e8e32017-06-29 17:05:59 +020053 @Option(
54 name = "bar",
ccalvarin835e8e32017-06-29 17:05:59 +020055 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
56 effectTags = {OptionEffectTag.NO_OP},
57 defaultValue = "42"
58 )
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000059 public int foo;
60
ccalvarin835e8e32017-06-29 17:05:59 +020061 @Option(
62 name = "baz",
ccalvarin835e8e32017-06-29 17:05:59 +020063 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
64 effectTags = {OptionEffectTag.NO_OP},
65 defaultValue = "oops"
66 )
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000067 public String baz;
68 }
69
70 private static class ConcreteCommand implements BlazeCommand {
71 @Override
juliexxiae91a4502018-08-15 14:42:29 -070072 public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult options) {
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000073 throw new UnsupportedOperationException();
74 }
75
76 @Override
ulfjack95aa4872017-06-06 09:32:18 -040077 public void editOptions(OptionsParser optionsParser) {}
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000078 }
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() {
lberkiaea56b32017-05-30 12:35:33 +020089 assertThat(new TestCommand().getClass().getAnnotation(Command.class).name())
90 .isEqualTo("test_name");
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000091 }
92
93 @Test
94 public void testGetOptionsYieldsAnnotatedOptions() {
95 ConfiguredRuleClassProvider ruleClassProvider = new ConfiguredRuleClassProvider.Builder()
Luis Fernando Pino Duque3fedf9e2016-04-28 15:47:29 +000096 .setToolsRepository(TestConstants.TOOLS_REPOSITORY)
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000097 .build();
98
Carmi Grushkofd8acab2015-11-10 17:19:13 +000099 assertThat(
100 BlazeCommandUtils.getOptions(
101 TestCommand.class, ImmutableList.<BlazeModule>of(), ruleClassProvider))
102 .containsExactlyElementsIn(optionClassesWithDefault(FooOptions.class, BarOptions.class));
Kristina Chodorowaa8b1522015-10-12 14:43:05 +0000103 }
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
Googler6afaade2024-06-20 02:15:28 -0700113 @Command(
114 name = "b",
115 options = {BarOptions.class},
116 inheritsOptionsFrom = {CommandA.class},
117 shortDescription = "",
118 help = "")
Kristina Chodorowaa8b1522015-10-12 14:43:05 +0000119 private static class CommandB extends ConcreteCommand {}
120
121 @Test
122 public void testOptionsAreInherited() {
123 ConfiguredRuleClassProvider ruleClassProvider = new ConfiguredRuleClassProvider.Builder()
Luis Fernando Pino Duque3fedf9e2016-04-28 15:47:29 +0000124 .setToolsRepository(TestConstants.TOOLS_REPOSITORY)
Kristina Chodorowaa8b1522015-10-12 14:43:05 +0000125 .build();
Carmi Grushkofd8acab2015-11-10 17:19:13 +0000126 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 Chodorowaa8b1522015-10-12 14:43:05 +0000134 }
135
136 private Collection<Class<?>> optionClassesWithDefault(Class<?>... optionClasses) {
137 List<Class<?>> result = new ArrayList<>();
138 Collections.addAll(result, optionClasses);
ulfjackb1294b42019-07-25 07:11:06 -0700139 result.add(UiOptions.class);
Kristina Chodorowaa8b1522015-10-12 14:43:05 +0000140 result.add(CommonCommandOptions.class);
ccalvarinb5158a92017-10-26 23:41:08 +0200141 result.add(ClientOptions.class);
adonovan240bdea2020-09-03 15:24:12 -0700142 result.add(BuildLanguageOptions.class);
Kristina Chodorowaa8b1522015-10-12 14:43:05 +0000143 return result;
144 }
145}