diff options
author | 2017-04-07 17:33:20 +0000 | |
---|---|---|
committer | 2017-04-10 11:43:45 +0200 | |
commit | e193df0aa45e84f08fb2f0df65f076d8c3fd844e (patch) | |
tree | 7ec0cec17589ba03c1126cf16880855bb4d63897 /src/test/java/com/google/devtools/build/lib/rules/config/ConfigSettingTest.java | |
parent | d0f13120ac85af77f9ee2324b51fe6aba2f7f405 (diff) |
Add flag_values attribute to config_setting.
This gives the ability to select on config_feature_flags. They still
have not been publicly documented, because there's no way to set them.
But, progress.
config_setting still needs to have either values or flag_values; it cannot
have both be empty. However, values is no longer mandatory, nor must it be
nonempty (as long as flag_values is set nonempty).
RELNOTES: None.
PiperOrigin-RevId: 152515036
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/rules/config/ConfigSettingTest.java')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/rules/config/ConfigSettingTest.java | 639 |
1 files changed, 637 insertions, 2 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/rules/config/ConfigSettingTest.java b/src/test/java/com/google/devtools/build/lib/rules/config/ConfigSettingTest.java index 7c9bad5f35..33514d38b5 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/config/ConfigSettingTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/config/ConfigSettingTest.java @@ -114,6 +114,7 @@ public class ConfigSettingTest extends BuildViewTestCase { protected ConfiguredRuleClassProvider getRuleClassProvider() { ConfiguredRuleClassProvider.Builder builder = new ConfiguredRuleClassProvider.Builder(); TestRuleClassProvider.addStandardRules(builder); + builder.addRuleDefinition(new FeatureFlagSetterRule()); builder.addConfigurationOptions(LateBoundTestOptions.class); builder.addConfigurationFragment(new LateBoundTestOptionsLoader()); builder.addConfigurationOptions(InternalTestOptions.class); @@ -224,12 +225,13 @@ public class ConfigSettingTest extends BuildViewTestCase { } /** - * Tests that *some* settings must be specified. + * Tests that *some* settings (values or flag_values) must be specified. */ @Test public void emptySettings() throws Exception { checkError("foo", "empty", - "//foo:empty: no settings specified", + "in config_setting rule //foo:empty: " + + "Either values or flag_values must be specified and non-empty", "config_setting(", " name = 'empty',", " values = {})"); @@ -329,4 +331,637 @@ public class ConfigSettingTest extends BuildViewTestCase { assertThat(target.getRuleClassObject().getOptionReferenceFunction().apply(target)) .containsExactly("copt", "javacopt"); } + + @Test + public void matchesIfFlagValuesAndValuesBothMatch() throws Exception { + useConfiguration("--copt=-Dright"); + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'match',", + " flag_values = {", + " ':flag': 'right',", + " },", + " values = {", + " 'copt': '-Dright',", + " },", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['right'],", + " default_value = 'right',", + ")"); + assertThat(getConfigMatchingProvider("//test:match").matches()).isTrue(); + } + + @Test + public void matchesIfFlagValuesMatchAndValuesAreEmpty() throws Exception { + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'match',", + " flag_values = {", + " ':flag': 'right',", + " },", + " values = {},", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['right'],", + " default_value = 'right',", + ")"); + assertThat(getConfigMatchingProvider("//test:match").matches()).isTrue(); + } + + @Test + public void matchesIfValuesMatchAndFlagValuesAreEmpty() throws Exception { + useConfiguration("--copt=-Dright"); + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'match',", + " flag_values = {},", + " values = {", + " 'copt': '-Dright',", + " },", + ")"); + assertThat(getConfigMatchingProvider("//test:match").matches()).isTrue(); + } + + @Test + public void doesNotMatchIfNeitherFlagValuesNorValuesMatches() throws Exception { + useConfiguration("--copt=-Dright"); + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'match',", + " flag_values = {", + " ':flag': 'wrong',", + " },", + " values = {", + " 'copt': '-Dwrong',", + " },", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['right', 'wrong'],", + " default_value = 'right',", + ")"); + assertThat(getConfigMatchingProvider("//test:match").matches()).isFalse(); + } + + @Test + public void doesNotMatchIfFlagValuesDoNotMatchAndValuesAreEmpty() throws Exception { + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'match',", + " flag_values = {", + " ':flag': 'wrong',", + " },", + " values = {},", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['right', 'wrong'],", + " default_value = 'right',", + ")"); + assertThat(getConfigMatchingProvider("//test:match").matches()).isFalse(); + } + + @Test + public void doesNotMatchIfFlagValuesDoNotMatchButValuesDo() throws Exception { + useConfiguration("--copt=-Dright"); + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'match',", + " flag_values = {", + " ':flag': 'wrong',", + " },", + " values = {", + " 'copt': '-Dright',", + " },", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['right', 'wrong'],", + " default_value = 'right',", + ")"); + assertThat(getConfigMatchingProvider("//test:match").matches()).isFalse(); + } + + @Test + public void doesNotMatchIfValuesDoNotMatchAndFlagValuesAreEmpty() throws Exception { + useConfiguration("--copt=-Dright"); + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'match',", + " flag_values = {},", + " values = {", + " 'copt': '-Dwrong',", + " },", + ")"); + assertThat(getConfigMatchingProvider("//test:match").matches()).isFalse(); + } + + @Test + public void doesNotMatchIfValuesDoNotMatchButFlagValuesDo() throws Exception { + useConfiguration("--copt=-Dright"); + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'match',", + " flag_values = {", + " ':flag': 'right',", + " },", + " values = {", + " 'copt': '-Dwrong',", + " },", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['right', 'wrong'],", + " default_value = 'right',", + ")"); + assertThat(getConfigMatchingProvider("//test:match").matches()).isFalse(); + } + + @Test + public void doesNotMatchIfEvenOneFlagValueDoesNotMatch() throws Exception { + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'match',", + " flag_values = {", + " ':flag': 'right',", + " ':flag2': 'bad',", + " },", + " values = {},", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['right', 'wrong'],", + " default_value = 'right',", + ")", + "config_feature_flag(", + " name = 'flag2',", + " allowed_values = ['good', 'bad'],", + " default_value = 'good',", + ")"); + assertThat(getConfigMatchingProvider("//test:match").matches()).isFalse(); + } + + @Test + public void matchesIfNonDefaultIsSpecifiedAndFlagValueIsThatValue() throws Exception { + scratch.file( + "test/BUILD", + "feature_flag_setter(", + " name = 'setter',", + " exports_setting = ':match',", + " flag_values = {':flag': 'actual'},", + ")", + "config_setting(", + " name = 'match',", + " flag_values = {", + " ':flag': 'actual',", + " },", + " values = {},", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['default', 'actual'],", + " default_value = 'default',", + ")"); + assertThat(getConfigMatchingProvider("//test:setter").matches()).isTrue(); + } + + @Test + public void doesNotMatchIfDefaultIsSpecifiedAndFlagValueIsNotDefault() throws Exception { + scratch.file( + "test/BUILD", + "feature_flag_setter(", + " name = 'setter',", + " exports_setting = ':match',", + " flag_values = {':flag': 'actual'},", + ")", + "config_setting(", + " name = 'match',", + " flag_values = {", + " ':flag': 'default',", + " },", + " values = {},", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['default', 'actual'],", + " default_value = 'default',", + ")"); + assertThat(getConfigMatchingProvider("//test:setter").matches()).isFalse(); + } + + @Test + public void doesNotRefineSettingWithSameValuesAndSameFlagValues() throws Exception { + useConfiguration("--copt=-Dright", "--javacopt=-Dgood"); + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'refined',", + " flag_values = {", + " ':flag': 'right',", + " ':flag2': 'good',", + " },", + " values = {", + " 'copt': '-Dright',", + " 'javacopt': '-Dgood',", + " },", + ")", + "config_setting(", + " name = 'other',", + " flag_values = {", + " ':flag': 'right',", + " ':flag2': 'good',", + " },", + " values = {", + " 'copt': '-Dright',", + " 'javacopt': '-Dgood',", + " },", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['right', 'wrong'],", + " default_value = 'right',", + ")", + "config_feature_flag(", + " name = 'flag2',", + " allowed_values = ['good', 'bad'],", + " default_value = 'good',", + ")"); + assertThat( + getConfigMatchingProvider("//test:refined") + .refines(getConfigMatchingProvider("//test:other"))) + .isFalse(); + } + + @Test + public void doesNotRefineSettingWithDifferentValuesAndSameFlagValues() throws Exception { + useConfiguration("--copt=-Dright", "--javacopt=-Dgood"); + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'refined',", + " flag_values = {", + " ':flag': 'right',", + " ':flag2': 'good',", + " },", + " values = {", + " 'javacopt': '-Dgood',", + " },", + ")", + "config_setting(", + " name = 'other',", + " flag_values = {", + " ':flag': 'right',", + " ':flag2': 'good',", + " },", + " values = {", + " 'copt': '-Dright',", + " },", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['right', 'wrong'],", + " default_value = 'right',", + ")", + "config_feature_flag(", + " name = 'flag2',", + " allowed_values = ['good', 'bad'],", + " default_value = 'good',", + ")"); + assertThat( + getConfigMatchingProvider("//test:refined") + .refines(getConfigMatchingProvider("//test:other"))) + .isFalse(); + } + + @Test + public void doesNotRefineSettingWithSameValuesAndDifferentFlagValues() throws Exception { + useConfiguration("--copt=-Dright", "--javacopt=-Dgood"); + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'refined',", + " flag_values = {", + " ':flag': 'right',", + " },", + " values = {", + " 'copt': '-Dright',", + " 'javacopt': '-Dgood',", + " },", + ")", + "config_setting(", + " name = 'other',", + " flag_values = {", + " ':flag2': 'good',", + " },", + " values = {", + " 'copt': '-Dright',", + " 'javacopt': '-Dgood',", + " },", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['right', 'wrong'],", + " default_value = 'right',", + ")", + "config_feature_flag(", + " name = 'flag2',", + " allowed_values = ['good', 'bad'],", + " default_value = 'good',", + ")"); + assertThat( + getConfigMatchingProvider("//test:refined") + .refines(getConfigMatchingProvider("//test:other"))) + .isFalse(); + } + + @Test + public void doesNotRefineSettingWithDifferentValuesAndDifferentFlagValues() throws Exception { + useConfiguration("--copt=-Dright", "--javacopt=-Dgood"); + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'refined',", + " flag_values = {", + " ':flag': 'right',", + " },", + " values = {", + " 'copt': '-Dright',", + " },", + ")", + "config_setting(", + " name = 'other',", + " flag_values = {", + " ':flag2': 'good',", + " },", + " values = {", + " 'javacopt': '-Dgood',", + " },", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['right', 'wrong'],", + " default_value = 'right',", + ")", + "config_feature_flag(", + " name = 'flag2',", + " allowed_values = ['good', 'bad'],", + " default_value = 'good',", + ")"); + assertThat( + getConfigMatchingProvider("//test:refined") + .refines(getConfigMatchingProvider("//test:other"))) + .isFalse(); + } + + @Test + public void doesNotRefineSettingWithDifferentValuesAndSubsetFlagValues() throws Exception { + useConfiguration("--copt=-Dright", "--javacopt=-Dgood"); + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'refined',", + " flag_values = {", + " ':flag': 'right',", + " ':flag2': 'good',", + " },", + " values = {", + " 'copt': '-Dright',", + " },", + ")", + "config_setting(", + " name = 'other',", + " flag_values = {", + " ':flag': 'right',", + " },", + " values = {", + " 'javacopt': '-Dgood',", + " },", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['right', 'wrong'],", + " default_value = 'right',", + ")", + "config_feature_flag(", + " name = 'flag2',", + " allowed_values = ['good', 'bad'],", + " default_value = 'good',", + ")"); + assertThat( + getConfigMatchingProvider("//test:refined") + .refines(getConfigMatchingProvider("//test:other"))) + .isFalse(); + } + + @Test + public void doesNotRefineSettingWithSubsetValuesAndDifferentFlagValues() throws Exception { + useConfiguration("--copt=-Dright", "--javacopt=-Dgood"); + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'refined',", + " flag_values = {", + " ':flag': 'right',", + " },", + " values = {", + " 'copt': '-Dright',", + " 'javacopt': '-Dgood',", + " },", + ")", + "config_setting(", + " name = 'other',", + " flag_values = {", + " ':flag2': 'good',", + " },", + " values = {", + " 'copt': '-Dright',", + " },", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['right', 'wrong'],", + " default_value = 'right',", + ")", + "config_feature_flag(", + " name = 'flag2',", + " allowed_values = ['good', 'bad'],", + " default_value = 'good',", + ")"); + assertThat( + getConfigMatchingProvider("//test:refined") + .refines(getConfigMatchingProvider("//test:other"))) + .isFalse(); + } + + @Test + public void refinesSettingWithSubsetValuesAndSameFlagValues() throws Exception { + useConfiguration("--copt=-Dright", "--javacopt=-Dgood"); + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'refined',", + " flag_values = {", + " ':flag': 'right',", + " ':flag2': 'good',", + " },", + " values = {", + " 'copt': '-Dright',", + " 'javacopt': '-Dgood',", + " },", + ")", + "config_setting(", + " name = 'other',", + " flag_values = {", + " ':flag': 'right',", + " ':flag2': 'good',", + " },", + " values = {", + " 'copt': '-Dright',", + " },", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['right', 'wrong'],", + " default_value = 'right',", + ")", + "config_feature_flag(", + " name = 'flag2',", + " allowed_values = ['good', 'bad'],", + " default_value = 'good',", + ")"); + assertThat( + getConfigMatchingProvider("//test:refined") + .refines(getConfigMatchingProvider("//test:other"))) + .isTrue(); + } + + @Test + public void refinesSettingWithSameValuesAndSubsetFlagValues() throws Exception { + useConfiguration("--copt=-Dright", "--javacopt=-Dgood"); + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'refined',", + " flag_values = {", + " ':flag': 'right',", + " ':flag2': 'good',", + " },", + " values = {", + " 'copt': '-Dright',", + " 'javacopt': '-Dgood',", + " },", + ")", + "config_setting(", + " name = 'other',", + " flag_values = {", + " ':flag': 'right',", + " },", + " values = {", + " 'copt': '-Dright',", + " 'javacopt': '-Dgood',", + " },", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['right', 'wrong'],", + " default_value = 'right',", + ")", + "config_feature_flag(", + " name = 'flag2',", + " allowed_values = ['good', 'bad'],", + " default_value = 'good',", + ")"); + assertThat( + getConfigMatchingProvider("//test:refined") + .refines(getConfigMatchingProvider("//test:other"))) + .isTrue(); + } + + @Test + public void refinesSettingWithSubsetValuesAndSubsetFlagValues() throws Exception { + useConfiguration("--copt=-Dright", "--javacopt=-Dgood"); + scratch.file( + "test/BUILD", + "config_setting(", + " name = 'refined',", + " flag_values = {", + " ':flag': 'right',", + " ':flag2': 'good',", + " },", + " values = {", + " 'copt': '-Dright',", + " 'javacopt': '-Dgood',", + " },", + ")", + "config_setting(", + " name = 'other',", + " flag_values = {", + " ':flag': 'right',", + " },", + " values = {", + " 'copt': '-Dright',", + " },", + ")", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['right', 'wrong'],", + " default_value = 'right',", + ")", + "config_feature_flag(", + " name = 'flag2',", + " allowed_values = ['good', 'bad'],", + " default_value = 'good',", + ")"); + assertThat( + getConfigMatchingProvider("//test:refined") + .refines(getConfigMatchingProvider("//test:other"))) + .isTrue(); + } + + @Test + public void forbidsNonConfigFeatureFlagRulesForFlagValues() throws Exception { + checkError("test", "invalid_flag", + "in flag_values attribute of config_setting rule //test:invalid_flag: " + + "'//test:genrule' does not have mandatory provider 'FeatureFlagInfo'", + "config_setting(", + " name = 'invalid_flag',", + " flag_values = {", + " ':genrule': 'lolz',", + " })", + "genrule(", + " name = 'genrule',", + " outs = ['output'],", + " cmd = 'echo >$@',", + " )"); + } + + @Test + public void requiresValidValueForFlagValues() throws Exception { + checkError("test", "invalid_flag", + "in flag_values attribute of config_setting rule //test:invalid_flag: " + + "error while parsing user-defined configuration values: " + + "'invalid' is not a valid value for '//test:flag'", + "config_setting(", + " name = 'invalid_flag',", + " flag_values = {", + " ':flag': 'invalid',", + " })", + "config_feature_flag(", + " name = 'flag',", + " allowed_values = ['right', 'valid'],", + " default_value = 'valid',", + ")"); + } } |