Search in sources :

Example 1 with SkylarkKey

use of com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey in project bazel by bazelbuild.

the class SkylarkAspectsTest method aspectWithDeclaredProviders.

@Test
public void aspectWithDeclaredProviders() throws Exception {
    scratch.file("test/aspect.bzl", "foo = provider()", "bar = provider()", "def _impl(target, ctx):", "   return [foo(), bar()]", "MyAspect = aspect(implementation=_impl)");
    scratch.file("test/BUILD", "java_library(name = 'xxx',)");
    AnalysisResult analysisResult = update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:xxx");
    assertThat(getLabelsToBuild(analysisResult)).containsExactly("//test:xxx");
    assertThat(getAspectDescriptions(analysisResult)).containsExactly("//test:aspect.bzl%MyAspect(//test:xxx)");
    List<Key> providers = getDeclaredProviderKeys(analysisResult);
    assertThat((providers.get(0))).isEqualTo(new SkylarkKey(Label.parseAbsolute("//test:aspect.bzl"), "foo"));
    assertThat((providers.get(1))).isEqualTo(new SkylarkKey(Label.parseAbsolute("//test:aspect.bzl"), "bar"));
}
Also used : SkylarkKey(com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) SkylarkKey(com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey) Key(com.google.devtools.build.lib.packages.ClassObjectConstructor.Key) Test(org.junit.Test)

Example 2 with SkylarkKey

use of com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey in project bazel by bazelbuild.

the class SkylarkAspectsTest method aspectWithDeclaredProvidersInAStruct.

@Test
public void aspectWithDeclaredProvidersInAStruct() throws Exception {
    scratch.file("test/aspect.bzl", "foo = provider()", "bar = provider()", "def _impl(target, ctx):", "   return struct(foobar='foobar', providers=[foo(), bar()])", "MyAspect = aspect(implementation=_impl)");
    scratch.file("test/BUILD", "java_library(name = 'xxx',)");
    AnalysisResult analysisResult = update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:xxx");
    assertThat(getLabelsToBuild(analysisResult)).containsExactly("//test:xxx");
    assertThat(getAspectDescriptions(analysisResult)).containsExactly("//test:aspect.bzl%MyAspect(//test:xxx)");
    List<Key> providers = getDeclaredProviderKeys(analysisResult);
    assertThat((providers.get(0))).isEqualTo(new SkylarkKey(Label.parseAbsolute("//test:aspect.bzl"), "foo"));
    assertThat((providers.get(1))).isEqualTo(new SkylarkKey(Label.parseAbsolute("//test:aspect.bzl"), "bar"));
}
Also used : SkylarkKey(com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) SkylarkKey(com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey) Key(com.google.devtools.build.lib.packages.ClassObjectConstructor.Key) Test(org.junit.Test)

Example 3 with SkylarkKey

use of com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey in project bazel by bazelbuild.

the class JavaSkylarkApiTest method javaProviderExposedOnJavaLibrary.

@Test
public void javaProviderExposedOnJavaLibrary() throws Exception {
    scratch.file("foo/extension.bzl", "my_provider = provider()", "def _impl(ctx):", "  dep_params = ctx.attr.dep[java_common.provider]", "  return [my_provider(p = dep_params)]", "my_rule = rule(_impl, attrs = { 'dep' : attr.label() })");
    scratch.file("foo/BUILD", "load(':extension.bzl', 'my_rule')", "java_library(name = 'jl', srcs = ['java/A.java'])", "my_rule(name = 'r', dep = ':jl')");
    ConfiguredTarget myRuleTarget = getConfiguredTarget("//foo:r");
    ConfiguredTarget javaLibraryTarget = getConfiguredTarget("//foo:jl");
    SkylarkKey myProviderKey = new SkylarkKey(Label.parseAbsolute("//foo:extension.bzl"), "my_provider");
    SkylarkClassObject declaredProvider = myRuleTarget.getProvider(SkylarkProviders.class).getDeclaredProvider(myProviderKey);
    Object javaProvider = declaredProvider.getValue("p");
    assertThat(javaProvider).isInstanceOf(JavaProvider.class);
    assertThat(javaLibraryTarget.getProvider(JavaProvider.class)).isEqualTo(javaProvider);
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) SkylarkKey(com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey) 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 4 with SkylarkKey

use of com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey in project bazel by bazelbuild.

the class JavaSkylarkApiTest method testJavaPlugin.

@Test
public void testJavaPlugin() throws Exception {
    scratch.file("java/test/extension.bzl", "result = provider()", "def impl(ctx):", "   depj = ctx.attr.dep.java", "   return [result(", "             processor_classpath = depj.annotation_processing.processor_classpath,", "             processor_classnames = depj.annotation_processing.processor_classnames,", "          )]", "my_rule = rule(impl, attrs = { 'dep' : attr.label() })");
    scratch.file("java/test/BUILD", "load(':extension.bzl', 'my_rule')", "java_library(name = 'plugin_dep',", "    srcs = [ 'ProcessorDep.java'])", "java_plugin(name = 'plugin',", "    srcs = ['AnnotationProcessor.java'],", "    processor_class = 'com.google.process.stuff',", "    deps = [ ':plugin_dep' ])", "java_library(name = 'to_be_processed',", "    plugins = [':plugin'],", "    srcs = ['ToBeProcessed.java'])", "my_rule(name = 'my', dep = ':to_be_processed')");
    ConfiguredTarget configuredTarget = getConfiguredTarget("//java/test:my");
    SkylarkProviders provider = configuredTarget.getProvider(SkylarkProviders.class);
    SkylarkClassObject skylarkClassObject = provider.getDeclaredProvider(new SkylarkKey(Label.parseAbsolute("//java/test:extension.bzl"), "result"));
    assertThat(Iterables.transform((List<?>) skylarkClassObject.getValue("processor_classpath"), new Function<Object, String>() {

        @Nullable
        @Override
        public String apply(@Nullable Object o) {
            return ((Artifact) o).getFilename();
        }
    })).containsExactly("libplugin.jar", "libplugin_dep.jar");
    assertThat((List<?>) skylarkClassObject.getValue("processor_classnames")).containsExactly("com.google.process.stuff");
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) SkylarkKey(com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) List(java.util.List) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Aggregations

SkylarkKey (com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey)4 Test (org.junit.Test)4 AnalysisResult (com.google.devtools.build.lib.analysis.BuildView.AnalysisResult)2 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)2 SkylarkProviders (com.google.devtools.build.lib.analysis.SkylarkProviders)2 Key (com.google.devtools.build.lib.packages.ClassObjectConstructor.Key)2 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)2 List (java.util.List)1 Nullable (javax.annotation.Nullable)1