use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.
the class SkylarkAspectsTest method aspectParametersBadValue.
@Test
public void aspectParametersBadValue() throws Exception {
scratch.file("test/aspect.bzl", "def _impl(target, ctx):", " return struct()", "def _rule_impl(ctx):", " return struct()", "MyAspectBadValue = aspect(", " implementation=_impl,", " attrs = { 'my_attr' : attr.string(values=['a']) },", ")", "my_rule = rule(", " implementation=_rule_impl,", " attrs = { 'deps' : attr.label_list(aspects=[MyAspectBadValue]),", " 'my_attr' : attr.string() },", ")");
scratch.file("test/BUILD", "load('//test:aspect.bzl', 'my_rule')", "my_rule(name = 'xxx', my_attr='b')");
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("ERROR /workspace/test/BUILD:2:1: //test:xxx: invalid value in 'my_attr' " + "attribute: has to be one of 'a' instead of 'b'");
}
use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult 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.BuildView.AnalysisResult in project bazel by bazelbuild.
the class SkylarkAspectsTest method aspectOnAspectLinearDuplicates.
/**
* Linear with duplicates.
* r2_1 depends on r0 with aspect a2.
* r1 depends on r2_1 with aspect a1.
* r2 depends on r1 with aspect a2.
*
* a2 is not interested in a1.
* There should be just one instance of aspect a2 on r0, and is should *not* see a1.
*/
@Test
public void aspectOnAspectLinearDuplicates() throws Exception {
scratch.file("test/aspect.bzl", "a1p = provider()", "def _a1_impl(target,ctx):", " return struct(a1p = 'a1p')", "a1 = aspect(_a1_impl, attr_aspects = ['dep'], provides = ['a1p'])", "a2p = provider()", "def _a2_impl(target,ctx):", " value = []", " if hasattr(ctx.rule.attr.dep, 'a2p'):", " value += ctx.rule.attr.dep.a2p.value", " if hasattr(target, 'a1p'):", " value.append(str(target.label) + str(ctx.aspect_ids) + '=yes')", " else:", " value.append(str(target.label) + str(ctx.aspect_ids) + '=no')", " return struct(a2p = a2p(value = value))", "a2 = aspect(_a2_impl, attr_aspects = ['dep'], required_aspect_providers = [])", "def _r1_impl(ctx):", " pass", "def _r2_impl(ctx):", " return struct(result = ctx.attr.dep.a2p.value)", "r1 = rule(_r1_impl, attrs = { 'dep' : attr.label(aspects = [a1])})", "r2 = rule(_r2_impl, attrs = { 'dep' : attr.label(aspects = [a2])})");
scratch.file("test/BUILD", "load(':aspect.bzl', 'r1', 'r2')", "r1(name = 'r0')", "r2(name = 'r2_1', dep = ':r0')", "r1(name = 'r1', dep = ':r2_1')", "r2(name = 'r2', dep = ':r1')");
AnalysisResult analysisResult = update("//test:r2");
ConfiguredTarget target = Iterables.getOnlyElement(analysisResult.getTargetsToBuild());
SkylarkList result = (SkylarkList) target.get("result");
// "yes" means that aspect a2 sees a1's providers.
assertThat(result).containsExactly("//test:r0[\"//test:aspect.bzl%a2\"]=no", "//test:r1[\"//test:aspect.bzl%a2\"]=no", "//test:r2_1[\"//test:aspect.bzl%a2\"]=no");
}
use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.
the class SkylarkAspectsTest method multipleAspects.
@Test
public void multipleAspects() throws Exception {
scratch.file("test/aspect.bzl", "def _aspect_impl(target,ctx):", " return struct()", "my_aspect = aspect(implementation = _aspect_impl)", "def _dummy_impl(ctx):", " pass", "r1 = rule(_dummy_impl, ", " attrs = { 'deps' : attr.label_list(aspects = [my_aspect, my_aspect]) })");
scratch.file("test/BUILD", "load(':aspect.bzl', 'r1')", "r1(name = 't1')");
reporter.removeHandler(failFastHandler);
try {
AnalysisResult result = update("//test:r1");
assertThat(keepGoing()).isTrue();
assertThat(result.hasError()).isTrue();
} catch (TargetParsingException | ViewCreationFailedException expected) {
// expected.
}
assertContainsEvent("aspect //test:aspect.bzl%my_aspect added more than once");
}
use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.
the class SkylarkAspectsTest method topLevelAspectDoesNotExistNoBuildFile.
@Test
public void topLevelAspectDoesNotExistNoBuildFile() throws Exception {
scratch.file("test/BUILD", "java_library(name = 'xxx')");
reporter.removeHandler(failFastHandler);
try {
AnalysisResult result = update(ImmutableList.of("foo/aspect.bzl%MyAspect"), "//test:xxx");
assertThat(keepGoing()).isTrue();
assertThat(result.hasError()).isTrue();
} catch (ViewCreationFailedException e) {
// expect to fail.
}
assertContainsEvent("Every .bzl file must have a corresponding package, but 'foo' does not have one. " + "Please create a BUILD file in the same or any parent directory. " + "Note that this BUILD file does not need to do anything except exist.");
}
Aggregations