Support "mandatoryProvidersList" in Skylark and added necessary tests
The type of attribute "providers" now is a list of lists of string. But a list
of string is still supported.
--
MOS_MIGRATED_REVID=115357326
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java
index 4ecfd72..20c9a4c 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java
@@ -184,6 +184,27 @@
}
@Test
+ public void testAttrWithProvidersList() throws Exception {
+ Attribute attr =
+ evalAttributeDefinition("attr.label_list(allow_files = True,"
+ + " providers = [['a', 'b'], ['c']])")
+ .build("a1");
+ assertThat(attr.getMandatoryProvidersList()).containsExactly(ImmutableSet.of("a", "b"),
+ ImmutableSet.of("c"));
+ }
+
+ @Test
+ public void testAttrWithWrongProvidersList() throws Exception {
+ checkErrorContains("Illegal argument: element in 'providers' is of unexpected type."
+ + " Should be list of string, but got list with an element of type int.",
+ "attr.label_list(allow_files = True, providers = [['a', 1], ['c']])");
+
+ checkErrorContains("Illegal argument: element in 'providers' is of unexpected type."
+ + " Should be list of string, but got string.",
+ "attr.label_list(allow_files = True, providers = [['a', 'b'], 'c'])");
+ }
+
+ @Test
public void testLabelListWithAspects() throws Exception {
SkylarkAttr.Descriptor attr =
(SkylarkAttr.Descriptor) evalRuleClassCode(
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java
index fe6003c..7f59515 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java
@@ -180,6 +180,83 @@
}
}
+ @Test
+ public void testMandatoryProvidersListWithSkylark() throws Exception {
+ scratch.file("test/BUILD",
+ "load('/test/rules', 'skylark_rule', 'my_rule', 'my_other_rule')",
+ "my_rule(name = 'mylib',",
+ " srcs = ['a.py'])",
+ "skylark_rule(name = 'skyrule1',",
+ " deps = [':mylib'])",
+ "my_other_rule(name = 'my_other_lib',",
+ " srcs = ['a.py'])",
+ "skylark_rule(name = 'skyrule2',",
+ " deps = [':my_other_lib'])");
+ scratch.file("test/rules.bzl",
+ "def _impl(ctx):",
+ " return",
+ "skylark_rule = rule(",
+ " implementation = _impl,",
+ " attrs = {",
+ " 'deps': attr.label_list(providers = [['a'], ['b', 'c']],",
+ " allow_files=True)",
+ " }",
+ ")",
+ "def my_rule_impl(ctx):",
+ " return struct(a = [])",
+ "my_rule = rule(implementation = my_rule_impl, ",
+ " attrs = { 'srcs' : attr.label_list(allow_files=True)})",
+ "def my_other_rule_impl(ctx):",
+ " return struct(b = [])",
+ "my_other_rule = rule(implementation = my_other_rule_impl, ",
+ " attrs = { 'srcs' : attr.label_list(allow_files=True)})");
+ reporter.removeHandler(failFastHandler);
+ assertNotNull(getConfiguredTarget("//test:skyrule1"));
+
+ try {
+ createRuleContext("//test:skyrule2");
+ fail("Should have failed because of wrong mandatory providers");
+ } catch (Exception ex) {
+ assertContainsEvent("ERROR /workspace/test/BUILD:9:10: in deps attribute of "
+ + "skylark_rule rule //test:skyrule2: '//test:my_other_lib' does not have "
+ + "mandatory provider 'a' or 'c'");
+ }
+ }
+
+ @Test
+ public void testMandatoryProvidersListWithNative() throws Exception {
+ scratch.file("test/BUILD",
+ "load('/test/rules', 'my_rule', 'my_other_rule')",
+ "my_rule(name = 'mylib',",
+ " srcs = ['a.py'])",
+ "testing_rule_for_mandatory_providers(name = 'skyrule1',",
+ " deps = [':mylib'])",
+ "my_other_rule(name = 'my_other_lib',",
+ " srcs = ['a.py'])",
+ "testing_rule_for_mandatory_providers(name = 'skyrule2',",
+ " deps = [':my_other_lib'])");
+ scratch.file("test/rules.bzl",
+ "def my_rule_impl(ctx):",
+ " return struct(a = [])",
+ "my_rule = rule(implementation = my_rule_impl, ",
+ " attrs = { 'srcs' : attr.label_list(allow_files=True)})",
+ "def my_other_rule_impl(ctx):",
+ " return struct(b = [])",
+ "my_other_rule = rule(implementation = my_other_rule_impl, ",
+ " attrs = { 'srcs' : attr.label_list(allow_files=True)})");
+ reporter.removeHandler(failFastHandler);
+ assertNotNull(getConfiguredTarget("//test:skyrule1"));
+
+ try {
+ createRuleContext("//test:skyrule2");
+ fail("Should have failed because of wrong mandatory providers");
+ } catch (Exception ex) {
+ assertContainsEvent("ERROR /workspace/test/BUILD:9:10: in deps attribute of "
+ + "testing_rule_for_mandatory_providers rule //test:skyrule2: '//test:my_other_lib' "
+ + "does not have mandatory provider 'a' or 'c'");
+ }
+ }
+
/* Sharing setup code between the testPackageBoundaryError*() methods is not possible since the
* errors already happen when loading the file. Consequently, all tests would fail at the same
* statement. */