use of com.google.devtools.build.lib.analysis.ConfiguredTarget in project bazel by bazelbuild.
the class SkylarkIntegrationTest method rulesReturningDeclaredProviders.
@Test
public void rulesReturningDeclaredProviders() throws Exception {
scratch.file("test/extension.bzl", "my_provider = provider()", "def _impl(ctx):", " return [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.analysis.ConfiguredTarget in project bazel by bazelbuild.
the class SkyframeExecutorTestUtils method getAllExistingConfiguredTargets.
/**
* Returns all configured targets currently in the graph.
*/
public static Iterable<ConfiguredTarget> getAllExistingConfiguredTargets(SkyframeExecutor skyframeExecutor) {
Collection<SkyValue> values = Maps.filterKeys(skyframeExecutor.getEvaluatorForTesting().getValues(), SkyFunctions.isSkyFunction(SkyFunctions.CONFIGURED_TARGET)).values();
List<ConfiguredTarget> cts = Lists.newArrayList();
for (SkyValue value : values) {
if (value != null) {
cts.add(((ConfiguredTargetValue) value).getConfiguredTarget());
}
}
return cts;
}
use of com.google.devtools.build.lib.analysis.ConfiguredTarget in project bazel by bazelbuild.
the class SkylarkAspectsTest method labelKeyedStringDictAllowsAspects.
@Test
public void labelKeyedStringDictAllowsAspects() throws Exception {
scratch.file("test/aspect.bzl", "def _aspect_impl(target, ctx):", " return struct(aspect_data=target.label.name)", "", "def _rule_impl(ctx):", " return struct(", " data=','.join(['{}:{}'.format(dep.aspect_data, val)", " for dep, val in ctx.attr.attr.items()]))", "", "MyAspect = aspect(", " implementation=_aspect_impl,", ")", "my_rule = rule(", " implementation=_rule_impl,", " attrs = { 'attr' : ", " attr.label_keyed_string_dict(aspects = [MyAspect]) },", ")");
scratch.file("test/BUILD", "load('/test/aspect', 'my_rule')", "java_library(", " name = 'yyy',", ")", "my_rule(", " name = 'xxx',", " attr = {':yyy': 'zzz'},", ")");
AnalysisResult analysisResult = update("//test:xxx");
ConfiguredTarget target = analysisResult.getTargetsToBuild().iterator().next();
SkylarkProviders skylarkProviders = target.getProvider(SkylarkProviders.class);
Object value = skylarkProviders.getValue("data");
assertThat(value).isEqualTo("yyy:zzz");
}
use of com.google.devtools.build.lib.analysis.ConfiguredTarget in project bazel by bazelbuild.
the class SkylarkAspectsTest method aspectOutputsToBinDirectory.
@Test
public void aspectOutputsToBinDirectory() throws Exception {
scratch.file("foo/extension.bzl", "def _aspect_impl(target, ctx):", " file = ctx.new_file('aspect-output-' + target.label.name)", " ctx.file_action(file, 'data')", " return struct(aspect_file = file)", "my_aspect = aspect(_aspect_impl)", "def _rule_impl(ctx):", " pass", "rule_bin_out = rule(_rule_impl, output_to_genfiles=False)", "rule_gen_out = rule(_rule_impl, output_to_genfiles=True)", "def _main_rule_impl(ctx):", " s = depset()", " for d in ctx.attr.deps:", " s = s | depset([d.aspect_file])", " return struct(aspect_files = s)", "main_rule = rule(_main_rule_impl,", " attrs = { 'deps' : attr.label_list(aspects = [my_aspect]) },", ")");
scratch.file("foo/BUILD", "load('extension', 'rule_bin_out', 'rule_gen_out', 'main_rule')", "rule_bin_out(name = 'rbin')", "rule_gen_out(name = 'rgen')", "main_rule(name = 'main', deps = [':rbin', ':rgen'])");
AnalysisResult analysisResult = update(ImmutableList.<String>of(), "//foo:main");
ConfiguredTarget target = analysisResult.getTargetsToBuild().iterator().next();
NestedSet<Artifact> aspectFiles = ((SkylarkNestedSet) target.getProvider(SkylarkProviders.class).getValue("aspect_files")).getSet(Artifact.class);
assertThat(transform(aspectFiles, new Function<Artifact, String>() {
@Override
public String apply(Artifact artifact) {
return artifact.getFilename();
}
})).containsExactly("aspect-output-rbin", "aspect-output-rgen");
for (Artifact aspectFile : aspectFiles) {
String rootPath = aspectFile.getRoot().getExecPath().toString();
assertWithMessage("Artifact %s should not be in genfiles", aspectFile).that(rootPath).doesNotContain("genfiles");
assertWithMessage("Artifact %s should be in bin", aspectFile).that(rootPath).endsWith("bin");
}
}
use of com.google.devtools.build.lib.analysis.ConfiguredTarget in project bazel by bazelbuild.
the class SkylarkAspectsTest method aspectOnLabelAttr.
@Test
public void aspectOnLabelAttr() throws Exception {
scratch.file("test/aspect.bzl", "def _aspect_impl(target, ctx):", " return struct(aspect_data='foo')", "", "def _rule_impl(ctx):", " return struct(data=ctx.attr.attr.aspect_data)", "", "MyAspect = aspect(", " implementation=_aspect_impl,", ")", "my_rule = rule(", " implementation=_rule_impl,", " attrs = { 'attr' : ", " attr.label(aspects = [MyAspect]) },", ")");
scratch.file("test/BUILD", "load('/test/aspect', 'my_rule')", "java_library(", " name = 'yyy',", ")", "my_rule(", " name = 'xxx',", " attr = ':yyy',", ")");
AnalysisResult analysisResult = update("//test:xxx");
ConfiguredTarget target = analysisResult.getTargetsToBuild().iterator().next();
SkylarkProviders skylarkProviders = target.getProvider(SkylarkProviders.class);
Object value = skylarkProviders.getValue("data");
assertThat(value).isEqualTo("foo");
}
Aggregations