From ffa14a6a372b22b16a9ac12f3d78a1b2859043cc Mon Sep 17 00:00:00 2001 From: Greg Estren Date: Thu, 28 Apr 2016 17:04:58 +0000 Subject: 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 --- .../select/AbstractAttributeMapperTest.java | 10 ++++-- .../select/AggregatingAttributeMapperTest.java | 40 +++++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) (limited to 'src/test/java/com/google/devtools/build/lib/analysis/select') 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 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( @@ -138,6 +144,23 @@ public class AggregatingAttributeMapperTest extends AbstractAttributeMapperTest "//a:a.sh", "//a:b.sh", "//a:default.sh", "//conditions:a", "//conditions:b")); } + @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", @@ -169,6 +192,21 @@ public class AggregatingAttributeMapperTest extends AbstractAttributeMapperTest assertThat(mapper.getReachableLabels("srcs", false)).containsExactlyElementsIn(valueLabels); } + @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) { -- cgit v1.2.3