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"));
}
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"));
}
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);
}
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");
}
Aggregations