diff options
author | Greg Estren <gregce@google.com> | 2016-04-28 17:04:58 +0000 |
---|---|---|
committer | Yun Peng <pcloudy@google.com> | 2016-04-29 08:09:34 +0000 |
commit | ffa14a6a372b22b16a9ac12f3d78a1b2859043cc (patch) | |
tree | 18ee1abb88979bdf0e0de158174b98cba4e7e137 /src/test/java | |
parent | 6144616cf3116df20551003d30379b104edca4cf (diff) |
Let select() "unset" values via: select({"//some:condition: None }).
This not only uses the default value when applicable, but also causes
ConfiguredAttributeMapper.isAttributeValueExplicitlySpecified to
return false.
Note the default value can come from two places: from the rule
definition if specified, otherwise from the type default.
RELNOTES[NEW]: select({"//some:condition: None }) is now possible (this "unsets" the attribute).
--
MOS_MIGRATED_REVID=121029815
Diffstat (limited to 'src/test/java')
3 files changed, 120 insertions, 12 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/ConfiguredAttributeMapperTest.java b/src/test/java/com/google/devtools/build/lib/analysis/ConfiguredAttributeMapperTest.java index 9097371374..8f97a49d77 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/ConfiguredAttributeMapperTest.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/ConfiguredAttributeMapperTest.java @@ -56,10 +56,10 @@ public class ConfiguredAttributeMapperTest extends BuildViewTestCase { scratch.file("conditions/BUILD", "config_setting(", " name = 'a',", - " values = {'compilation_mode': 'opt'})", + " values = {'define': 'mode=a'})", "config_setting(", " name = 'b',", - " values = {'compilation_mode': 'dbg'})"); + " values = {'define': 'mode=b'})"); } /** @@ -80,13 +80,13 @@ public class ConfiguredAttributeMapperTest extends BuildViewTestCase { " '" + BuildType.Selector.DEFAULT_CONDITION_KEY + "': 'default command',", " }))"); - useConfiguration("-c", "opt"); + useConfiguration("--define", "mode=a"); assertEquals("a command", getMapper("//a:gen").get("cmd", Type.STRING)); - useConfiguration("-c", "dbg"); + useConfiguration("--define", "mode=b"); assertEquals("b command", getMapper("//a:gen").get("cmd", Type.STRING)); - useConfiguration("-c", "fastbuild"); + useConfiguration("--define", "mode=c"); assertEquals("default command", getMapper("//a:gen").get("cmd", Type.STRING)); } @@ -128,19 +128,19 @@ public class ConfiguredAttributeMapperTest extends BuildViewTestCase { final Label binSrc = Label.parseAbsolute("//a:bin.sh"); - useConfiguration("-c", "opt"); + useConfiguration("--define", "mode=a"); getMapper("//a:bin").visitLabels(testVisitor); assertThat(visitedLabels) .containsExactlyElementsIn(ImmutableList.of(binSrc, Label.parseAbsolute("//a:adep"))); visitedLabels.clear(); - useConfiguration("-c", "dbg"); + useConfiguration("--define", "mode=b"); getMapper("//a:bin").visitLabels(testVisitor); assertThat(visitedLabels) .containsExactlyElementsIn(ImmutableList.of(binSrc, Label.parseAbsolute("//a:bdep"))); visitedLabels.clear(); - useConfiguration("-c", "fastbuild"); + useConfiguration("--define", "mode=c"); getMapper("//a:bin").visitLabels(testVisitor); assertThat(visitedLabels) .containsExactlyElementsIn(ImmutableList.of(binSrc, Label.parseAbsolute("//a:defaultdep"))); @@ -173,7 +173,7 @@ public class ConfiguredAttributeMapperTest extends BuildViewTestCase { "sh_binary(", " name = 'defaultdep',", " srcs = ['defaultdep.sh'])"); - useConfiguration("-c", "dbg"); + useConfiguration("--define", "mode=b"); // Target configuration is in dbg mode, so we should match //conditions:b: assertThat(getMapper("//a:gen").get("tools", BuildType.LABEL_LIST)) @@ -206,4 +206,68 @@ public class ConfiguredAttributeMapperTest extends BuildViewTestCase { ImmutableList.of( Label.parseAbsolute("//hello:a.in"), Label.parseAbsolute("//hello:d.in"))); } + + @Test + public void testNoneValuesMeansAttributeIsNotExplicitlySet() throws Exception { + writeConfigRules(); + scratch.file("a/BUILD", + "genrule(", + " name = 'gen',", + " srcs = [],", + " outs = ['out'],", + " cmd = '',", + " message = select({", + " '//conditions:a': 'defined message',", + " '//conditions:b': None,", + " }))"); + + useConfiguration("--define", "mode=a"); + assertThat(getMapper("//a:gen").isAttributeValueExplicitlySpecified("message")).isTrue(); + + useConfiguration("--define", "mode=b"); + assertThat(getMapper("//a:gen").isAttributeValueExplicitlySpecified("message")).isFalse(); + } + + @Test + public void testNoneValuesWithMultipleSelectsAllNone() throws Exception { + writeConfigRules(); + scratch.file("a/BUILD", + "genrule(", + " name = 'gen',", + " srcs = [],", + " outs = ['out'],", + " cmd = '',", + " message = select({", + " '//conditions:a': 'defined message 1',", + " '//conditions:b': None,", + " }) + select({", + " '//conditions:a': 'defined message 2',", + " '//conditions:b': None,", + " }),", + ")"); + + useConfiguration("--define", "mode=a"); + assertThat(getMapper("//a:gen").isAttributeValueExplicitlySpecified("message")).isTrue(); + + useConfiguration("--define", "mode=b"); + assertThat(getMapper("//a:gen").isAttributeValueExplicitlySpecified("message")).isFalse(); + } + + @Test + public void testNoneValueOnDefaultConditionWithNullDefault() throws Exception { + writeConfigRules(); + scratch.file("a/BUILD", + "cc_library(", + " name = 'lib',", + " srcs = ['lib.cc'],", + " linkstamp = select({", + " '//conditions:a': 'notused_linkstamp.cc',", + " '" + BuildType.Selector.DEFAULT_CONDITION_KEY + "': None,", + " }),", + ")"); + + useConfiguration(); + assertThat(getMapper("//a:lib").isAttributeValueExplicitlySpecified("linkstamp")).isFalse(); + assertThat(getMapper("//a:lib").get("linkstamp", BuildType.LABEL)).isNull(); + } } diff --git a/src/test/java/com/google/devtools/build/lib/analysis/select/AbstractAttributeMapperTest.java b/src/test/java/com/google/devtools/build/lib/analysis/select/AbstractAttributeMapperTest.java index beb15a8f97..4ecd5ed560 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/select/AbstractAttributeMapperTest.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/select/AbstractAttributeMapperTest.java @@ -137,9 +137,15 @@ public class AbstractAttributeMapperTest extends FoundationTestCase { protected static class VisitationRecorder implements AttributeMap.AcceptsLabelAttribute { public List<String> labelsVisited = Lists.newArrayList(); + private final String attrName; + + public VisitationRecorder(String attrName) { + this.attrName = attrName; + } + @Override public void acceptLabelAttribute(Label label, Attribute attribute) { - if (attribute.getName().equals("srcs")) { + if (attribute.getName().equals(attrName)) { labelsVisited.add(label.toString()); } } @@ -147,7 +153,7 @@ public class AbstractAttributeMapperTest extends FoundationTestCase { @Test public void testVisitation() throws Exception { - VisitationRecorder recorder = new VisitationRecorder(); + VisitationRecorder recorder = new VisitationRecorder("srcs"); mapper.visitLabels(recorder); assertThat(recorder.labelsVisited) .containsExactlyElementsIn(ImmutableList.of("//x:a", "//x:b", "//x:c")); diff --git a/src/test/java/com/google/devtools/build/lib/analysis/select/AggregatingAttributeMapperTest.java b/src/test/java/com/google/devtools/build/lib/analysis/select/AggregatingAttributeMapperTest.java index 4aafb58752..e9f4305d29 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/select/AggregatingAttributeMapperTest.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/select/AggregatingAttributeMapperTest.java @@ -16,6 +16,7 @@ package com.google.devtools.build.lib.analysis.select; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertNull; +import com.google.common.base.Verify; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.devtools.build.lib.cmdline.Label; @@ -43,6 +44,11 @@ public class AggregatingAttributeMapperTest extends AbstractAttributeMapperTest mapper = AggregatingAttributeMapper.of(rule); } + private static Label getDefaultMallocLabel(Rule rule) { + return Verify.verifyNotNull( + (Label) rule.getRuleClassObject().getAttributeByName("malloc").getDefaultValueForTesting()); + } + /** * Tests that {@link AggregatingAttributeMapper#visitAttribute} returns an * attribute's sole value when declared directly (i.e. not as a configurable dict). @@ -130,7 +136,7 @@ public class AggregatingAttributeMapperTest extends AbstractAttributeMapperTest " '" + BuildType.Selector.DEFAULT_CONDITION_KEY + "': ['default.sh'],", " }))"); - VisitationRecorder recorder = new VisitationRecorder(); + VisitationRecorder recorder = new VisitationRecorder("srcs"); AggregatingAttributeMapper.of(rule).visitLabels(recorder); assertThat(recorder.labelsVisited) .containsExactlyElementsIn( @@ -139,6 +145,23 @@ public class AggregatingAttributeMapperTest extends AbstractAttributeMapperTest } @Test + public void testVisitationWithDefaultValues() throws Exception { + Rule rule = createRule("a", "myrule", + "cc_binary(name = 'myrule',", + " srcs = [],", + " malloc = select({", + " '//conditions:a': None,", + " }))"); + + VisitationRecorder recorder = new VisitationRecorder("malloc"); + AggregatingAttributeMapper.of(rule).visitLabels(recorder); + assertThat(recorder.labelsVisited) + .containsExactlyElementsIn( + ImmutableList.of("//conditions:a", getDefaultMallocLabel(rule).toString())); + } + + + @Test public void testGetReachableLabels() throws Exception { Rule rule = createRule("x", "main", "cc_binary(", @@ -170,6 +193,21 @@ public class AggregatingAttributeMapperTest extends AbstractAttributeMapperTest } @Test + public void testGetReachableLabelsWithDefaultValues() throws Exception { + Rule rule = createRule("a", "myrule", + "cc_binary(name = 'myrule',", + " srcs = [],", + " malloc = select({", + " '//conditions:a': None,", + " }))"); + + AggregatingAttributeMapper mapper = AggregatingAttributeMapper.of(rule); + assertThat(mapper.getReachableLabels("malloc", true)) + .containsExactly( + getDefaultMallocLabel(rule), Label.create("@//conditions", "a")); + } + + @Test public void testDuplicateCheckOnNullValues() throws Exception { if (TestConstants.THIS_IS_BAZEL) { return; |