use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.
the class SkylarkAspectsTest method aspectsPropagatingForDefaultAndImplicit.
@Test
public void aspectsPropagatingForDefaultAndImplicit() throws Exception {
scratch.file("test/aspect.bzl", "def _impl(target, ctx):", " s = depset([target.label])", " c = depset([ctx.rule.kind])", " a = ctx.rule.attr", " if hasattr(a, '_stl') and a._stl:", " s += a._stl.target_labels", " c += a._stl.rule_kinds", " if hasattr(a, '_stl_default') and a._stl_default:", " s += a._stl_default.target_labels", " c += a._stl_default.rule_kinds", " return struct(target_labels = s, rule_kinds = c)", "", "def _rule_impl(ctx):", " pass", "", "my_rule = rule(implementation = _rule_impl,", " attrs = { '_stl' : attr.label(default = Label('//test:xxx')) },", ")", "MyAspect = aspect(", " implementation=_impl,", " attr_aspects=['_stl', '_stl_default'],", ")");
scratch.file("test/BUILD", "load('/test/aspect', 'my_rule')", "cc_library(", " name = 'xxx',", ")", "my_rule(", " name = 'yyy',", ")");
AnalysisResult analysisResult = update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:yyy");
AspectValue aspectValue = analysisResult.getAspects().iterator().next();
SkylarkProviders skylarkProviders = aspectValue.getConfiguredAspect().getProvider(SkylarkProviders.class);
assertThat(skylarkProviders).isNotNull();
Object names = skylarkProviders.getValue("target_labels");
assertThat(names).isInstanceOf(SkylarkNestedSet.class);
assertThat(transform(((SkylarkNestedSet) names).toCollection(), new Function<Object, String>() {
@Nullable
@Override
public String apply(Object o) {
assertThat(o).isInstanceOf(Label.class);
return ((Label) o).getName();
}
})).containsExactly("stl", "xxx", "yyy");
}
use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.
the class SkylarkAspectsTest method aspectsPropagatingToAllAttributes.
@Test
public void aspectsPropagatingToAllAttributes() throws Exception {
scratch.file("test/aspect.bzl", "def _impl(target, ctx):", " s = depset([target.label])", " if hasattr(ctx.rule.attr, 'runtime_deps'):", " for i in ctx.rule.attr.runtime_deps:", " s += i.target_labels", " return struct(target_labels = s)", "", "MyAspect = aspect(", " implementation=_impl,", " attrs = { '_tool' : attr.label(default = Label('//test:tool')) },", " attr_aspects=['*'],", ")");
scratch.file("test/BUILD", "java_library(", " name = 'tool',", ")", "java_library(", " name = 'bar',", " runtime_deps = [':tool'],", ")", "java_library(", " name = 'foo',", " runtime_deps = [':bar'],", ")");
AnalysisResult analysisResult = update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:foo");
AspectValue aspectValue = analysisResult.getAspects().iterator().next();
SkylarkProviders skylarkProviders = aspectValue.getConfiguredAspect().getProvider(SkylarkProviders.class);
assertThat(skylarkProviders).isNotNull();
Object names = skylarkProviders.getValue("target_labels");
assertThat(names).isInstanceOf(SkylarkNestedSet.class);
assertThat(transform(((SkylarkNestedSet) names).toCollection(), new Function<Object, String>() {
@Nullable
@Override
public String apply(Object o) {
assertThat(o).isInstanceOf(Label.class);
return ((Label) o).getName();
}
})).containsExactly("foo", "bar", "tool");
}
use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.
the class SkylarkJavaLiteProtoLibraryTest method getProvider.
private static <P extends TransitiveInfoProvider> P getProvider(Class<P> providerClass, ConfiguredTarget target) {
SkylarkProviders skylarkProviders = target.getProvider(SkylarkProviders.class);
JavaProvider javaProvider = (JavaProvider) skylarkProviders.getDeclaredProvider(JavaProvider.JAVA_PROVIDER.getKey());
return javaProvider.getProvider(providerClass);
}
use of com.google.devtools.build.lib.analysis.SkylarkProviders 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");
}
use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.
the class SkylarkIntegrationTest method testOutputGroupsAsDictionary.
@Test
public void testOutputGroupsAsDictionary() throws Exception {
scratch.file("test/skylark/extension.bzl", "def _impl(ctx):", " f = ctx.attr.dep.output_groups['_hidden_top_level" + INTERNAL_SUFFIX + "']", " has_key1 = '_hidden_top_level" + INTERNAL_SUFFIX + "' in ctx.attr.dep.output_groups", " has_key2 = 'foobar' in ctx.attr.dep.output_groups", " all_keys = [k for k in ctx.attr.dep.output_groups]", " return struct(result = f, ", " has_key1 = has_key1,", " has_key2 = has_key2,", " all_keys = all_keys,", " output_groups = { 'my_group' : f })", "my_rule = rule(implementation = _impl,", " attrs = { 'dep' : attr.label() })");
scratch.file("test/skylark/BUILD", "load('/test/skylark/extension', 'my_rule')", "cc_binary(name = 'lib', data = ['a.txt'])", "my_rule(name='my', dep = ':lib')");
NestedSet<Artifact> hiddenTopLevelArtifacts = getConfiguredTarget("//test/skylark:lib").getProvider(OutputGroupProvider.class).getOutputGroup(OutputGroupProvider.HIDDEN_TOP_LEVEL);
ConfiguredTarget myTarget = getConfiguredTarget("//test/skylark:my");
SkylarkProviders skylarkProviders = myTarget.getProvider(SkylarkProviders.class);
SkylarkNestedSet result = (SkylarkNestedSet) skylarkProviders.getValue("result");
assertThat(result.getSet(Artifact.class)).containsExactlyElementsIn(hiddenTopLevelArtifacts);
assertThat(myTarget.getProvider(OutputGroupProvider.class).getOutputGroup("my_group")).containsExactlyElementsIn(hiddenTopLevelArtifacts);
assertThat(skylarkProviders.getValue("has_key1")).isEqualTo(Boolean.TRUE);
assertThat(skylarkProviders.getValue("has_key2")).isEqualTo(Boolean.FALSE);
assertThat((SkylarkList) skylarkProviders.getValue("all_keys")).containsExactly("_hidden_top_level" + INTERNAL_SUFFIX, "compilation_prerequisites" + INTERNAL_SUFFIX, "files_to_compile" + INTERNAL_SUFFIX, "temp_files" + INTERNAL_SUFFIX);
}
Aggregations