use of com.google.devtools.build.lib.packages.SkylarkClassObject in project bazel by bazelbuild.
the class SkylarkIntegrationTest method rulesReturningDeclaredProvidersCompatMode.
@Test
public void rulesReturningDeclaredProvidersCompatMode() throws Exception {
scratch.file("test/extension.bzl", "my_provider = provider()", "def _impl(ctx):", " return struct(providers = [my_provider(x = 1)])", "my_rule = rule(_impl)");
scratch.file("test/BUILD", "load(':extension.bzl', 'my_rule')", "my_rule(name = 'r')");
ConfiguredTarget configuredTarget = getConfiguredTarget("//test:r");
ClassObjectConstructor.Key key = new SkylarkClassObjectConstructor.SkylarkKey(Label.create(configuredTarget.getLabel().getPackageIdentifier(), "extension.bzl"), "my_provider");
SkylarkProviders skylarkProviders = configuredTarget.getProvider(SkylarkProviders.class);
assertThat(skylarkProviders).isNotNull();
SkylarkClassObject declaredProvider = skylarkProviders.getDeclaredProvider(key);
assertThat(declaredProvider).isNotNull();
assertThat(declaredProvider.getConstructor().getKey()).isEqualTo(key);
assertThat(declaredProvider.getValue("x")).isEqualTo(1);
}
use of com.google.devtools.build.lib.packages.SkylarkClassObject in project bazel by bazelbuild.
the class SkylarkRuleImplementationFunctionsTest method testDefaultProviderProvidedImplicitly.
@Test
public void testDefaultProviderProvidedImplicitly() throws Exception {
scratch.file("test/foo.bzl", "foo_provider = provider()", "def _impl(ctx):", " foo = foo_provider()", " return [foo]", "foo_rule = rule(", " implementation = _impl,", ")");
scratch.file("test/bar.bzl", "load(':foo.bzl', 'foo_provider')", "def _impl(ctx):", " dep = ctx.attr.deps[0]", // The goal is to test this object
" provider = dep[ctx.default_provider]", // so we return it here
" return struct(", " default = provider,", " )", "bar_rule = rule(", " implementation = _impl,", " attrs = {", " 'deps': attr.label_list(allow_files=True),", " }", ")");
scratch.file("test/BUILD", "load(':foo.bzl', 'foo_rule')", "load(':bar.bzl', 'bar_rule')", "foo_rule(name = 'dep_rule')", "bar_rule(name = 'my_rule', deps = [':dep_rule'])");
ConfiguredTarget configuredTarget = getConfiguredTarget("//test:my_rule");
Object provider = configuredTarget.getProvider(SkylarkProviders.class).getValue("default");
assertThat(provider).isInstanceOf(SkylarkClassObject.class);
SkylarkClassObject defaultProvider = (SkylarkClassObject) provider;
assertThat((defaultProvider).getConstructor().getPrintableName()).isEqualTo("default_provider");
}
use of com.google.devtools.build.lib.packages.SkylarkClassObject in project bazel by bazelbuild.
the class SkylarkRuleImplementationFunctionsTest method testDeclaredProviders.
@Test
public void testDeclaredProviders() throws Exception {
scratch.file("test/foo.bzl", "foo_provider = provider()", "foobar_provider = provider()", "def _impl(ctx):", " foo = foo_provider()", " foobar = foobar_provider()", " return [foo, foobar]", "foo_rule = rule(", " implementation = _impl,", " attrs = {", " \"srcs\": attr.label_list(allow_files=True),", " }", ")");
scratch.file("test/bar.bzl", "load(':foo.bzl', 'foo_provider')", "def _impl(ctx):", " dep = ctx.attr.deps[0]", // The goal is to test this object
" provider = dep[foo_provider]", // so we return it here
" return struct(proxy = provider)", "bar_rule = rule(", " implementation = _impl,", " attrs = {", " 'srcs': attr.label_list(allow_files=True),", " 'deps': attr.label_list(allow_files=True),", " }", ")");
scratch.file("test/BUILD", "load(':foo.bzl', 'foo_rule')", "load(':bar.bzl', 'bar_rule')", "foo_rule(name = 'dep_rule')", "bar_rule(name = 'my_rule', deps = [':dep_rule'])");
ConfiguredTarget configuredTarget = getConfiguredTarget("//test:my_rule");
Object provider = configuredTarget.getProvider(SkylarkProviders.class).getValue("proxy");
assertThat(provider).isInstanceOf(SkylarkClassObject.class);
assertThat(((SkylarkClassObject) provider).getConstructor().getPrintableName()).isEqualTo("foo_provider");
}
use of com.google.devtools.build.lib.packages.SkylarkClassObject in project bazel by bazelbuild.
the class SkylarkRuleImplementationFunctionsTest method testDefaultProvider.
@Test
public void testDefaultProvider() throws Exception {
scratch.file("test/foo.bzl", "foo_provider = provider()", "def _impl(ctx):", " default = ctx.default_provider(", " runfiles=ctx.runfiles(ctx.files.runs),", " )", " foo = foo_provider()", " return [foo, default]", "foo_rule = rule(", " implementation = _impl,", " attrs = {", " 'runs': attr.label_list(allow_files=True),", " }", ")");
scratch.file("test/bar.bzl", "load(':foo.bzl', 'foo_provider')", "def _impl(ctx):", " dep = ctx.attr.deps[0]", // The goal is to test this object
" provider = dep[ctx.default_provider]", // so we return it here
" return struct(", " default = provider,", " )", "bar_rule = rule(", " implementation = _impl,", " attrs = {", " 'deps': attr.label_list(allow_files=True),", " }", ")");
scratch.file("test/BUILD", "load(':foo.bzl', 'foo_rule')", "load(':bar.bzl', 'bar_rule')", "foo_rule(name = 'dep_rule', runs = ['run.file', 'run2.file'])", "bar_rule(name = 'my_rule', deps = [':dep_rule'])");
ConfiguredTarget configuredTarget = getConfiguredTarget("//test:my_rule");
Object provider = configuredTarget.getProvider(SkylarkProviders.class).getValue("default");
assertThat(provider).isInstanceOf(SkylarkClassObject.class);
SkylarkClassObject defaultProvider = (SkylarkClassObject) provider;
assertThat((defaultProvider).getConstructor().getPrintableName()).isEqualTo("default_provider");
// Test .runfiles
Object runfilesProvider = defaultProvider.getValue("runfiles");
assertThat(runfilesProvider).isInstanceOf(RunfilesProvider.class);
assertThat(Iterables.transform(((RunfilesProvider) runfilesProvider).getDefaultRunfiles().getAllArtifacts(), TO_STRING)).containsExactly("Artifact:[/workspace[source]]test/run.file", "Artifact:[/workspace[source]]test/run2.file");
}
use of com.google.devtools.build.lib.packages.SkylarkClassObject in project bazel by bazelbuild.
the class SkylarkRuleContextTest method testDependencyActionsProvider.
@Test
public void testDependencyActionsProvider() throws Exception {
scratch.file("test/rules.bzl", getSimpleUnderTestDefinition("ctx.action(outputs=[out], command='echo foo123 > ' + out.path)"), testingRuleDefinition);
scratch.file("test/BUILD", simpleBuildDefinition);
SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
Object provider = evalRuleContextCode(ruleContext, "ruleContext.attr.dep[Actions]");
assertThat(provider).isInstanceOf(SkylarkClassObject.class);
assertThat(((SkylarkClassObject) provider).getConstructor()).isEqualTo(ActionsProvider.SKYLARK_CONSTRUCTOR);
update("actions", provider);
Object mapping = eval("actions.by_file");
assertThat(mapping).isInstanceOf(SkylarkDict.class);
assertThat((SkylarkDict<?, ?>) mapping).hasSize(1);
update("file", eval("list(ruleContext.attr.dep.files)[0]"));
Object actionUnchecked = eval("actions.by_file[file]");
assertThat(actionUnchecked).isInstanceOf(ActionAnalysisMetadata.class);
}
Aggregations