diff options
author | mstaib <mstaib@google.com> | 2018-03-22 08:04:26 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-03-22 08:06:00 -0700 |
commit | 3dc6c7253ef273530702fa84e1bf67f416a49da6 (patch) | |
tree | cc9acc8e406db722185dfb1e8658e32647a693d8 /src/test/java/com | |
parent | 886d3733f67465b980768066111e1b361188a629 (diff) |
Move ConfigFeatureFlagConfiguration.Options to a top-level class.
This class is about to get bigger in a major way, so let's put it in its own class.
This also moves the tests to their own test class, since that class is about to get a lot bigger too.
RELNOTES: None.
PiperOrigin-RevId: 190065717
Diffstat (limited to 'src/test/java/com')
3 files changed, 143 insertions, 127 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlagConfigurationTest.java b/src/test/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlagConfigurationTest.java index 09bf495e2b..07dcae5dc2 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlagConfigurationTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlagConfigurationTest.java @@ -16,15 +16,10 @@ package com.google.devtools.build.lib.rules.config; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth8.assertThat; -import static org.junit.Assert.fail; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; -import com.google.common.testing.EqualsTester; import com.google.devtools.build.lib.actions.util.LabelArtifactOwner; import com.google.devtools.build.lib.cmdline.Label; -import com.google.devtools.common.options.OptionsParser; -import com.google.devtools.common.options.OptionsParsingException; import java.util.Map; import java.util.Optional; import org.junit.Test; @@ -34,114 +29,6 @@ import org.junit.runners.JUnit4; /** Tests for feature flag configuration fragments. */ @RunWith(JUnit4.class) public final class ConfigFeatureFlagConfigurationTest { - - @Test - public void options_getFlagValues_startsEmpty() throws Exception { - assertThat(new ConfigFeatureFlagConfiguration.Options().getFlagValues()).isEmpty(); - } - - @Test - public void options_replaceFlagValues_reflectedInGetFlagValues() throws Exception { - Map<Label, String> originalMap = - ImmutableMap.of( - Label.parseAbsoluteUnchecked("//label:a"), "value", - Label.parseAbsoluteUnchecked("//label:b"), "otherValue"); - ConfigFeatureFlagConfiguration.Options options = new ConfigFeatureFlagConfiguration.Options(); - options.replaceFlagValues(originalMap); - assertThat(options.getFlagValues()).containsExactlyEntriesIn(originalMap); - } - - @Test - public void options_replaceFlagValues_totallyReplacesFlagValuesMap() throws Exception { - Map<Label, String> originalMap = - ImmutableMap.of( - Label.parseAbsoluteUnchecked("//label:a"), "value", - Label.parseAbsoluteUnchecked("//label:b"), "otherValue"); - Map<Label, String> newMap = - ImmutableMap.of( - Label.parseAbsoluteUnchecked("//label:a"), "differentValue", - Label.parseAbsoluteUnchecked("//label:c"), "differentFlag"); - ConfigFeatureFlagConfiguration.Options options = new ConfigFeatureFlagConfiguration.Options(); - options.replaceFlagValues(originalMap); - options.replaceFlagValues(newMap); - assertThat(options.getFlagValues()).containsExactlyEntriesIn(newMap); - } - - @Test - public void options_getDefault_isEmpty() throws Exception { - assertThat( - ((ConfigFeatureFlagConfiguration.Options) - new ConfigFeatureFlagConfiguration.Options().getDefault()) - .getFlagValues()) - .isEmpty(); - } - - @Test - public void options_getHost_isEmpty() throws Exception { - assertThat( - ((ConfigFeatureFlagConfiguration.Options) - new ConfigFeatureFlagConfiguration.Options().getHost()) - .getFlagValues()) - .isEmpty(); - assertThat( - ((ConfigFeatureFlagConfiguration.Options) - new ConfigFeatureFlagConfiguration.Options().getHost()) - .getFlagValues()) - .isEmpty(); - } - - @Test - public void options_equals_forEquivalentMaps() throws Exception { - new EqualsTester() - .addEqualityGroup( - getOptionsWith(ImmutableMap.<Label, String>of()), - new ConfigFeatureFlagConfiguration.Options(), - new ConfigFeatureFlagConfiguration.Options().getDefault(), - new ConfigFeatureFlagConfiguration.Options().getHost(), - new ConfigFeatureFlagConfiguration.Options().getHost()) - .addEqualityGroup( - getOptionsWith(ImmutableMap.of(Label.parseAbsoluteUnchecked("//a:a"), "a")), - getOptionsWith(ImmutableMap.of(Label.parseAbsoluteUnchecked("//a:a"), "a"))) - .addEqualityGroup( - getOptionsWith(ImmutableMap.of(Label.parseAbsoluteUnchecked("//b:b"), "a"))) - .addEqualityGroup( - getOptionsWith(ImmutableMap.of(Label.parseAbsoluteUnchecked("//a:a"), "b"))) - .addEqualityGroup( - getOptionsWith(ImmutableMap.of(Label.parseAbsoluteUnchecked("//b:b"), "b"))) - .addEqualityGroup( - getOptionsWith( - ImmutableMap.of( - Label.parseAbsoluteUnchecked("//a:a"), "b", - Label.parseAbsoluteUnchecked("//b:b"), "a")), - getOptionsWith( - ImmutableMap.of( - Label.parseAbsoluteUnchecked("//b:b"), "a", - Label.parseAbsoluteUnchecked("//a:a"), "b"))) - .testEquals(); - } - - @Test - public void options_doesNotAllowFlagValuesToBeParsed() throws Exception { - OptionsParser parser = - OptionsParser.newOptionsParser(ConfigFeatureFlagConfiguration.Options.class); - try { - parser.parse( - "--" - + Iterables.getOnlyElement( - new ConfigFeatureFlagConfiguration.Options().asMap().keySet()) - + "={}"); - fail("Flags successfully parsed despite passing a private flag."); - } catch (OptionsParsingException expected) { - assertThat(expected).hasMessageThat().contains("Unrecognized option:"); - } - } - - private ConfigFeatureFlagConfiguration.Options getOptionsWith(Map<Label, String> values) { - ConfigFeatureFlagConfiguration.Options result = new ConfigFeatureFlagConfiguration.Options(); - result.replaceFlagValues(values); - return result; - } - @Test public void getFeatureFlagValue_returnsValueOfFlagWhenRequestingSetFlag() throws Exception { Label ruleLabel = Label.parseAbsoluteUnchecked("//a:a"); @@ -246,7 +133,7 @@ public final class ConfigFeatureFlagConfigurationTest { /** Generates a configuration fragment with the given set of flag-value pairs. */ private static ConfigFeatureFlagConfiguration getConfigurationWithFlags( Map<Label, String> flags) { - ConfigFeatureFlagConfiguration.Options options = new ConfigFeatureFlagConfiguration.Options(); + ConfigFeatureFlagOptions options = new ConfigFeatureFlagOptions(); options.replaceFlagValues(flags); return new ConfigFeatureFlagConfiguration(options); } diff --git a/src/test/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlagOptionsTest.java b/src/test/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlagOptionsTest.java new file mode 100644 index 0000000000..3550000216 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlagOptionsTest.java @@ -0,0 +1,132 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License + +package com.google.devtools.build.lib.rules.config; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.fail; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; +import com.google.common.testing.EqualsTester; +import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.common.options.OptionsParser; +import com.google.devtools.common.options.OptionsParsingException; +import java.util.Map; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for feature flag option fragments. */ +@RunWith(JUnit4.class) +public final class ConfigFeatureFlagOptionsTest { + + @Test + public void getFlagValues_startsEmpty() throws Exception { + assertThat(new ConfigFeatureFlagOptions().getFlagValues()).isEmpty(); + } + + @Test + public void replaceFlagValues_reflectedInGetFlagValues() throws Exception { + Map<Label, String> originalMap = + ImmutableMap.of( + Label.parseAbsoluteUnchecked("//label:a"), "value", + Label.parseAbsoluteUnchecked("//label:b"), "otherValue"); + ConfigFeatureFlagOptions options = new ConfigFeatureFlagOptions(); + options.replaceFlagValues(originalMap); + assertThat(options.getFlagValues()).containsExactlyEntriesIn(originalMap); + } + + @Test + public void replaceFlagValues_totallyReplacesFlagValuesMap() throws Exception { + Map<Label, String> originalMap = + ImmutableMap.of( + Label.parseAbsoluteUnchecked("//label:a"), "value", + Label.parseAbsoluteUnchecked("//label:b"), "otherValue"); + Map<Label, String> newMap = + ImmutableMap.of( + Label.parseAbsoluteUnchecked("//label:a"), "differentValue", + Label.parseAbsoluteUnchecked("//label:c"), "differentFlag"); + ConfigFeatureFlagOptions options = new ConfigFeatureFlagOptions(); + options.replaceFlagValues(originalMap); + options.replaceFlagValues(newMap); + assertThat(options.getFlagValues()).containsExactlyEntriesIn(newMap); + } + + @Test + public void getDefault_isEmpty() throws Exception { + assertThat( + ((ConfigFeatureFlagOptions) new ConfigFeatureFlagOptions().getDefault()) + .getFlagValues()) + .isEmpty(); + } + + @Test + public void getHost_isEmpty() throws Exception { + assertThat( + ((ConfigFeatureFlagOptions) new ConfigFeatureFlagOptions().getHost()).getFlagValues()) + .isEmpty(); + assertThat( + ((ConfigFeatureFlagOptions) new ConfigFeatureFlagOptions().getHost()).getFlagValues()) + .isEmpty(); + } + + @Test + public void equals_forEquivalentMaps() throws Exception { + new EqualsTester() + .addEqualityGroup( + getOptionsWith(ImmutableMap.<Label, String>of()), + new ConfigFeatureFlagOptions(), + new ConfigFeatureFlagOptions().getDefault(), + new ConfigFeatureFlagOptions().getHost(), + new ConfigFeatureFlagOptions().getHost()) + .addEqualityGroup( + getOptionsWith(ImmutableMap.of(Label.parseAbsoluteUnchecked("//a:a"), "a")), + getOptionsWith(ImmutableMap.of(Label.parseAbsoluteUnchecked("//a:a"), "a"))) + .addEqualityGroup( + getOptionsWith(ImmutableMap.of(Label.parseAbsoluteUnchecked("//b:b"), "a"))) + .addEqualityGroup( + getOptionsWith(ImmutableMap.of(Label.parseAbsoluteUnchecked("//a:a"), "b"))) + .addEqualityGroup( + getOptionsWith(ImmutableMap.of(Label.parseAbsoluteUnchecked("//b:b"), "b"))) + .addEqualityGroup( + getOptionsWith( + ImmutableMap.of( + Label.parseAbsoluteUnchecked("//a:a"), "b", + Label.parseAbsoluteUnchecked("//b:b"), "a")), + getOptionsWith( + ImmutableMap.of( + Label.parseAbsoluteUnchecked("//b:b"), "a", + Label.parseAbsoluteUnchecked("//a:a"), "b"))) + .testEquals(); + } + + @Test + public void parser_doesNotAllowFlagValuesToBeParsed() throws Exception { + OptionsParser parser = OptionsParser.newOptionsParser(ConfigFeatureFlagOptions.class); + try { + parser.parse( + "--" + Iterables.getOnlyElement(new ConfigFeatureFlagOptions().asMap().keySet()) + "={}"); + fail("Flags successfully parsed despite passing a private flag."); + } catch (OptionsParsingException expected) { + assertThat(expected).hasMessageThat().contains("Unrecognized option:"); + } + } + + private ConfigFeatureFlagOptions getOptionsWith(Map<Label, String> values) { + ConfigFeatureFlagOptions result = new ConfigFeatureFlagOptions(); + result.replaceFlagValues(values); + return result; + } +} diff --git a/src/test/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlagTransitionFactoryTest.java b/src/test/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlagTransitionFactoryTest.java index 9f18282fe6..74533cfcf5 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlagTransitionFactoryTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/config/ConfigFeatureFlagTransitionFactoryTest.java @@ -49,10 +49,9 @@ public final class ConfigFeatureFlagTransitionFactoryTest extends BuildViewTestC @Override @Nullable public <O extends OptionsBase> O getOptions(Class<O> optionsClass) { - if (optionsClass.equals(ConfigFeatureFlagConfiguration.Options.class)) { - ConfigFeatureFlagConfiguration.Options options = - (ConfigFeatureFlagConfiguration.Options) - new ConfigFeatureFlagConfiguration.Options().getDefault(); + if (optionsClass.equals(ConfigFeatureFlagOptions.class)) { + ConfigFeatureFlagOptions options = + (ConfigFeatureFlagOptions) new ConfigFeatureFlagOptions().getDefault(); options.replaceFlagValues(flagValues); return optionsClass.cast(options); } @@ -67,8 +66,7 @@ public final class ConfigFeatureFlagTransitionFactoryTest extends BuildViewTestC private static BuildOptions getOptionsWithFlagFragment(Map<Label, String> values) { return BuildOptions.of( - ImmutableList.<Class<? extends FragmentOptions>>of( - ConfigFeatureFlagConfiguration.Options.class), + ImmutableList.<Class<? extends FragmentOptions>>of(ConfigFeatureFlagOptions.class), new ConfigFeatureFlagsOptionsProvider(values)); } @@ -90,7 +88,7 @@ public final class ConfigFeatureFlagTransitionFactoryTest extends BuildViewTestC BuildOptions converted = transition.apply(original); assertThat(converted).isSameAs(original); - assertThat(original.contains(ConfigFeatureFlagConfiguration.Options.class)).isFalse(); + assertThat(original.contains(ConfigFeatureFlagOptions.class)).isFalse(); } @Test @@ -113,7 +111,7 @@ public final class ConfigFeatureFlagTransitionFactoryTest extends BuildViewTestC BuildOptions converted = transition.apply(original); assertThat(converted).isSameAs(original); - assertThat(original.contains(ConfigFeatureFlagConfiguration.Options.class)).isFalse(); + assertThat(original.contains(ConfigFeatureFlagOptions.class)).isFalse(); } @Test @@ -127,10 +125,9 @@ public final class ConfigFeatureFlagTransitionFactoryTest extends BuildViewTestC BuildOptions converted = transition.apply(original); assertThat(converted).isNotSameAs(original); - assertThat(original.get(ConfigFeatureFlagConfiguration.Options.class).getFlagValues()) + assertThat(original.get(ConfigFeatureFlagOptions.class).getFlagValues()) .containsExactlyEntriesIn(originalFlagMap); - assertThat(converted.get(ConfigFeatureFlagConfiguration.Options.class).getFlagValues()) - .isEmpty(); + assertThat(converted.get(ConfigFeatureFlagOptions.class).getFlagValues()).isEmpty(); } @Test @@ -156,9 +153,9 @@ public final class ConfigFeatureFlagTransitionFactoryTest extends BuildViewTestC BuildOptions converted = transition.apply(original); assertThat(converted).isNotSameAs(original); - assertThat(original.get(ConfigFeatureFlagConfiguration.Options.class).getFlagValues()) + assertThat(original.get(ConfigFeatureFlagOptions.class).getFlagValues()) .containsExactlyEntriesIn(originalFlagMap); - assertThat(converted.get(ConfigFeatureFlagConfiguration.Options.class).getFlagValues()) + assertThat(converted.get(ConfigFeatureFlagOptions.class).getFlagValues()) .containsExactlyEntriesIn(expectedFlagMap); } |