use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.
the class SkylarkAspectsTest method aspectAdvertisingProviders.
@Test
public void aspectAdvertisingProviders() throws Exception {
scratch.file("test/aspect.bzl", "def _impl(target, ctx):", " return struct()", "my_aspect = aspect(_impl, provides = ['foo'])", "a_dict = { 'foo' : attr.label_list(aspects = [my_aspect]) }");
scratch.file("test/BUILD", "java_library(name = 'xxx',)");
reporter.removeHandler(failFastHandler);
try {
AnalysisResult analysisResult = update(ImmutableList.of("//test:aspect.bzl%my_aspect"), "//test:xxx");
assertThat(keepGoing()).isTrue();
assertThat(analysisResult.hasError()).isTrue();
} catch (ViewCreationFailedException e) {
// expect exception
}
assertContainsEvent("Aspect '//test:aspect.bzl%my_aspect', applied to '//test:xxx', " + "does not provide advertised provider 'foo'");
}
use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult 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.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.
the class SkylarkAspectsTest method aspectOnAspectDiamond.
/**
* Diamond case.
* rule r1 depends or r0 with aspect a1.
* rule r2 depends or r0 with aspect a2.
* rule rcollect depends on r1, r2 with aspect a3.
*
* Aspect a3 should be applied twice to target r0: once in [a1, a3] sequence
* and once in [a2, a3] sequence.
*/
@Test
public void aspectOnAspectDiamond() throws Exception {
scratch.file("test/aspect.bzl", "def _a1_impl(target,ctx):", " return struct(a1p = 'text from a1')", "a1 = aspect(_a1_impl, attr_aspects = ['deps'], provides = ['a1p'])", "", "def _a2_impl(target,ctx):", " return struct(a2p = 'text from a2')", "a2 = aspect(_a2_impl, attr_aspects = ['deps'], provides = ['a2p'])", "", "def _a3_impl(target,ctx):", " value = []", " f = ctx.new_file('a3.out')", " ctx.file_action(f, 'text')", " for dep in ctx.rule.attr.deps:", " if hasattr(dep, 'a3p'):", " value += dep.a3p", " s = str(target.label) + str(ctx.aspect_ids) + '='", " if hasattr(target, 'a1p'):", " s += 'a1p'", " if hasattr(target, 'a2p'):", " s += 'a2p'", " value.append(s)", " return struct(a3p = value)", "a3 = aspect(_a3_impl, attr_aspects = ['deps'],", " required_aspect_providers = [['a1p'], ['a2p']])", "def _r1_impl(ctx):", " pass", "def _rcollect_impl(ctx):", " value = []", " for dep in ctx.attr.deps:", " if hasattr(dep, 'a3p'):", " value += dep.a3p", " return struct(result = value)", "r1 = rule(_r1_impl, attrs = { 'deps' : attr.label_list(aspects = [a1])})", "r2 = rule(_r1_impl, attrs = { 'deps' : attr.label_list(aspects = [a2])})", "rcollect = rule(_rcollect_impl, attrs = { 'deps' : attr.label_list(aspects = [a3])})");
scratch.file("test/BUILD", "load(':aspect.bzl', 'r1', 'r2', 'rcollect')", "r1(name = 'r0')", "r1(name = 'r1', deps = [':r0'])", "r2(name = 'r2', deps = [':r0'])", "rcollect(name = 'rcollect', deps = [':r1', ':r2'])");
AnalysisResult analysisResult = update("//test:rcollect");
ConfiguredTarget target = Iterables.getOnlyElement(analysisResult.getTargetsToBuild());
SkylarkList result = (SkylarkList) target.get("result");
assertThat(result).containsExactly("//test:r0[\"//test:aspect.bzl%a1\", \"//test:aspect.bzl%a3\"]=a1p", "//test:r1[\"//test:aspect.bzl%a3\"]=", "//test:r0[\"//test:aspect.bzl%a2\", \"//test:aspect.bzl%a3\"]=a2p", "//test:r2[\"//test:aspect.bzl%a3\"]=");
}
use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.
the class SkylarkAspectsTest method aspectParametersUncovered.
@Test
public void aspectParametersUncovered() throws Exception {
scratch.file("test/aspect.bzl", "def _impl(target, ctx):", " return struct()", "def _rule_impl(ctx):", " return struct()", "MyAspectUncovered = aspect(", " implementation=_impl,", " attrs = { 'my_attr' : attr.string(values=['aaa']) },", ")", "my_rule = rule(", " implementation=_rule_impl,", " attrs = { 'deps' : attr.label_list(aspects=[MyAspectUncovered]) },", ")");
scratch.file("test/BUILD", "load('//test:aspect.bzl', 'my_rule')", "my_rule(name = 'xxx')");
reporter.removeHandler(failFastHandler);
try {
AnalysisResult result = update(ImmutableList.<String>of(), "//test:xxx");
assertThat(keepGoing()).isTrue();
assertThat(result.hasError()).isTrue();
} catch (Exception e) {
// expect to fail.
}
assertContainsEvent("Aspect //test:aspect.bzl%MyAspectUncovered requires rule my_rule to specify attribute " + "'my_attr' with type string.");
}
use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.
the class SkylarkAspectsTest method aspectsFromSkylarkRules.
@Test
public void aspectsFromSkylarkRules() throws Exception {
scratch.file("test/aspect.bzl", "def _aspect_impl(target, ctx):", " s = depset([target.label])", " for i in ctx.rule.attr.deps:", " s += i.target_labels", " return struct(target_labels = s)", "", "def _rule_impl(ctx):", " s = depset([])", " for i in ctx.attr.attr:", " s += i.target_labels", " return struct(rule_deps = s)", "", "MyAspect = aspect(", " implementation=_aspect_impl,", " attr_aspects=['deps'],", ")", "my_rule = rule(", " implementation=_rule_impl,", " attrs = { 'attr' : ", " attr.label_list(mandatory=True, allow_files=True, 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");
assertThat(getLabelsToBuild(analysisResult)).containsExactly("//test:xxx");
ConfiguredTarget target = analysisResult.getTargetsToBuild().iterator().next();
SkylarkProviders skylarkProviders = target.getProvider(SkylarkProviders.class);
assertThat(skylarkProviders).isNotNull();
Object names = skylarkProviders.getValue("rule_deps");
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 o.toString();
}
})).containsExactly("//test:yyy");
}
Aggregations