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 | |
| 16 | import com.google.common.collect.ImmutableList; |
| 17 | import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider; |
| 18 | import com.google.devtools.build.lib.testutil.MoreAsserts; |
| 19 | import com.google.devtools.build.lib.util.ExitCode; |
| 20 | import com.google.devtools.common.options.Option; |
| 21 | import com.google.devtools.common.options.OptionsBase; |
| 22 | import com.google.devtools.common.options.OptionsParser; |
| 23 | import com.google.devtools.common.options.OptionsProvider; |
| 24 | import org.junit.Test; |
| 25 | import org.junit.runner.RunWith; |
| 26 | import org.junit.runners.JUnit4; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Collection; |
| 30 | import java.util.Collections; |
| 31 | import java.util.List; |
| 32 | |
| 33 | import static org.junit.Assert.assertEquals; |
| 34 | |
| 35 | /** |
| 36 | * Tests {@link BlazeCommand}. |
| 37 | */ |
| 38 | @RunWith(JUnit4.class) |
| 39 | public class AbstractCommandTest { |
| 40 | |
| 41 | public static class FooOptions extends OptionsBase { |
| 42 | @Option(name = "foo", category = "one", defaultValue = "0") |
| 43 | public int foo; |
| 44 | } |
| 45 | |
| 46 | public static class BarOptions extends OptionsBase { |
| 47 | @Option(name = "bar", category = "two", defaultValue = "42") |
| 48 | public int foo; |
| 49 | |
| 50 | @Option(name = "baz", category = "one", defaultValue = "oops") |
| 51 | public String baz; |
| 52 | } |
| 53 | |
| 54 | private static class ConcreteCommand implements BlazeCommand { |
| 55 | @Override |
| 56 | public ExitCode exec(CommandEnvironment env, OptionsProvider options) { |
| 57 | throw new UnsupportedOperationException(); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void editOptions(CommandEnvironment env, OptionsParser optionsParser) {} |
| 62 | } |
| 63 | |
| 64 | @Command(name = "test_name", |
| 65 | help = "Usage: some funny usage for %{command} ...;\n\n%{options}; end", |
| 66 | options = {FooOptions.class, BarOptions.class}, |
| 67 | shortDescription = "a short description", |
| 68 | allowResidue = false) |
| 69 | private static class TestCommand extends ConcreteCommand {} |
| 70 | |
| 71 | @Test |
| 72 | public void testGetNameYieldsAnnotatedName() { |
| 73 | assertEquals("test_name", |
| 74 | new TestCommand().getClass().getAnnotation(Command.class).name()); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void testGetOptionsYieldsAnnotatedOptions() { |
| 79 | ConfiguredRuleClassProvider ruleClassProvider = new ConfiguredRuleClassProvider.Builder() |
| 80 | .build(); |
| 81 | |
| 82 | MoreAsserts.assertSameContents(optionClassesWithDefault(FooOptions.class, BarOptions.class), |
| 83 | BlazeCommandUtils.getOptions( |
| 84 | TestCommand.class, ImmutableList.<BlazeModule>of(), ruleClassProvider)); |
| 85 | } |
| 86 | |
| 87 | /*************************************************************************** |
| 88 | * The tests below test how a command interacts with the dispatcher except * |
| 89 | * for execution, which is tested in {@link BlazeCommandDispatcherTest}. * |
| 90 | ***************************************************************************/ |
| 91 | |
| 92 | @Command(name = "a", options = {FooOptions.class}, shortDescription = "", help = "") |
| 93 | private static class CommandA extends ConcreteCommand {} |
| 94 | |
| 95 | @Command(name = "b", options = {BarOptions.class}, inherits = {CommandA.class}, |
| 96 | shortDescription = "", help = "") |
| 97 | private static class CommandB extends ConcreteCommand {} |
| 98 | |
| 99 | @Test |
| 100 | public void testOptionsAreInherited() { |
| 101 | ConfiguredRuleClassProvider ruleClassProvider = new ConfiguredRuleClassProvider.Builder() |
| 102 | .build(); |
| 103 | MoreAsserts.assertSameContents(optionClassesWithDefault(FooOptions.class), |
| 104 | BlazeCommandUtils.getOptions( |
| 105 | CommandA.class, ImmutableList.<BlazeModule>of(), ruleClassProvider)); |
| 106 | MoreAsserts.assertSameContents(optionClassesWithDefault(FooOptions.class, BarOptions.class), |
| 107 | BlazeCommandUtils.getOptions( |
| 108 | CommandB.class, ImmutableList.<BlazeModule>of(), ruleClassProvider)); |
| 109 | } |
| 110 | |
| 111 | private Collection<Class<?>> optionClassesWithDefault(Class<?>... optionClasses) { |
| 112 | List<Class<?>> result = new ArrayList<>(); |
| 113 | Collections.addAll(result, optionClasses); |
| 114 | result.add(BlazeCommandEventHandler.Options.class); |
| 115 | result.add(CommonCommandOptions.class); |
| 116 | return result; |
| 117 | } |
| 118 | } |