Search in sources :

Example 21 with SkylarkClassObject

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);
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) SkylarkClassObjectConstructor(com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor) ClassObjectConstructor(com.google.devtools.build.lib.packages.ClassObjectConstructor) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) RuleConfiguredTarget(com.google.devtools.build.lib.analysis.RuleConfiguredTarget) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) FileConfiguredTarget(com.google.devtools.build.lib.analysis.FileConfiguredTarget) Test(org.junit.Test)

Example 22 with SkylarkClassObject

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");
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) Test(org.junit.Test)

Example 23 with SkylarkClassObject

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");
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) Test(org.junit.Test)

Example 24 with SkylarkClassObject

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");
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) RunfilesProvider(com.google.devtools.build.lib.analysis.RunfilesProvider) Test(org.junit.Test)

Example 25 with SkylarkClassObject

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);
}
Also used : SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext) SkylarkDict(com.google.devtools.build.lib.syntax.SkylarkDict) Test(org.junit.Test)

Aggregations

SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)27 Test (org.junit.Test)15 SkylarkProviders (com.google.devtools.build.lib.analysis.SkylarkProviders)10 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)9 EvalException (com.google.devtools.build.lib.syntax.EvalException)6 Location (com.google.devtools.build.lib.events.Location)4 SkylarkClassObjectConstructor (com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor)4 ClassObject (com.google.devtools.build.lib.syntax.ClassObject)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Artifact (com.google.devtools.build.lib.actions.Artifact)3 FileConfiguredTarget (com.google.devtools.build.lib.analysis.FileConfiguredTarget)2 RuleConfiguredTarget (com.google.devtools.build.lib.analysis.RuleConfiguredTarget)2 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)2 Runfiles (com.google.devtools.build.lib.analysis.Runfiles)2 RunfilesProvider (com.google.devtools.build.lib.analysis.RunfilesProvider)2 ClassObjectConstructor (com.google.devtools.build.lib.packages.ClassObjectConstructor)2 SkylarkKey (com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey)2 SkylarkRuleContext (com.google.devtools.build.lib.rules.SkylarkRuleContext)2 Environment (com.google.devtools.build.lib.syntax.Environment)2 Mutability (com.google.devtools.build.lib.syntax.Mutability)2