Skip to content

Commit

Permalink
Improve subrule attribute tests
Browse files Browse the repository at this point in the history
Before this change, there was a risk we were not checking for the right attribute, and the tests were incorrectly passing.

PiperOrigin-RevId: 572554305
Change-Id: I5dc177331f50153f1eb05d09117e9e677e38891d
  • Loading branch information
hvadehra authored and copybara-github committed Oct 11, 2023
1 parent a4ed8a7 commit b038dcf
Showing 1 changed file with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.devtools.build.lib.analysis.starlark;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.truth.Truth.assertThat;
import static com.google.devtools.build.lib.analysis.starlark.StarlarkSubrule.getRuleAttrName;
import static org.junit.Assert.assertThrows;
Expand All @@ -27,6 +28,7 @@
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
import com.google.devtools.build.lib.packages.Attribute;
import com.google.devtools.build.lib.packages.AttributeValueSource;
import com.google.devtools.build.lib.packages.Provider;
import com.google.devtools.build.lib.packages.StarlarkProvider;
Expand Down Expand Up @@ -478,21 +480,30 @@ public void testSubruleAttrs_notVisibleInRuleCtx() throws Exception {
"load('myrule.bzl', 'my_rule')",
"my_rule(name = 'foo')");

ImmutableList<String> attributes =
ImmutableList<String> ruleClassAttributes =
getRuleContext(getConfiguredTarget("//subrule_testing:foo"))
.getRule()
.getRuleClassObject()
.getAttributes()
.stream()
.map(Attribute::getName)
.collect(toImmutableList());
ImmutableList<String> attributesVisibleToStarlark =
Sequence.cast(
getProvider("//subrule_testing:foo", "//subrule_testing:myrule.bzl", "MyInfo")
.getValue("result"),
String.class,
"")
.getImmutableList();
String ruleAttrName =
getRuleAttrName(
Label.parseCanonical("//subrule_testing:myrule.bzl"),
"_my_subrule",
"_foo",
AttributeValueSource.DIRECT);

assertThat(attributes)
.doesNotContain(
getRuleAttrName(
Label.parseCanonical("//subrule_testing:myrule.bzl"),
"_my_subrule",
"_foo",
AttributeValueSource.DIRECT));
assertThat(ruleClassAttributes).contains(ruleAttrName);
assertThat(attributesVisibleToStarlark).doesNotContain(ruleAttrName);
}

@Test
Expand Down Expand Up @@ -521,21 +532,33 @@ public void testSubruleAttrs_notVisibleInAspectCtx() throws Exception {
"load('myrule.bzl', 'my_rule')",
"my_rule(name = 'foo', dep = '//default')");

ImmutableList<String> attributes =
ImmutableList<String> aspectClassAttributes =
getRuleContext(getConfiguredTarget("//subrule_testing:foo"))
.getRule()
.getRuleClassObject()
.getAttributeByName("dep")
.getAspectsDetails()
.get(0)
.getAspectAttributes()
.stream()
.map(Attribute::getName)
.collect(toImmutableList());
ImmutableList<String> attributesVisibleToStarlark =
Sequence.cast(
getProvider("//subrule_testing:foo", "//subrule_testing:myrule.bzl", "MyInfo")
.getValue("result"),
String.class,
"")
.getImmutableList();

assertThat(attributes)
.doesNotContain(
getRuleAttrName(
Label.parseCanonical("//subrule_testing:myrule.bzl"),
"_my_subrule",
"_foo",
AttributeValueSource.DIRECT));
String ruleAttrName =
getRuleAttrName(
Label.parseCanonical("//subrule_testing:myrule.bzl"),
"_my_subrule",
"_foo",
AttributeValueSource.DIRECT);

assertThat(aspectClassAttributes).contains(ruleAttrName);
assertThat(attributesVisibleToStarlark).doesNotContain(ruleAttrName);
}

@Test
Expand Down

0 comments on commit b038dcf

Please sign in to comment.