use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult 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");
}
use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.
the class SkylarkAspectsTest method getConfiguredTargetForAspectFragment.
private ConfiguredTarget getConfiguredTargetForAspectFragment(String fullFieldName, String fragments, String hostFragments, String ruleFragments, String ruleHostFragments) throws Exception {
scratch.file("test/aspect.bzl", "def _aspect_impl(target, ctx):", " return struct(result = str(" + fullFieldName + "))", "", "def _rule_impl(ctx):", " return struct(stuff = '...')", "", "MyAspect = aspect(", " implementation=_aspect_impl,", " attr_aspects=['deps'],", " fragments=[" + fragments + "],", " host_fragments=[" + hostFragments + "],", ")", "my_rule = rule(", " implementation=_rule_impl,", " attrs = { 'attr' : ", " attr.label_list(mandatory=True, allow_files=True, aspects = [MyAspect]) },", " fragments=[" + ruleFragments + "],", " host_fragments=[" + ruleHostFragments + "],", ")");
scratch.file("test/BUILD", "load('/test/aspect', 'my_rule')", "exports_files(['zzz'])", "my_rule(", " name = 'yyy',", " attr = ['zzz'],", ")", "my_rule(", " name = 'xxx',", " attr = ['yyy'],", ")");
AnalysisResult result = update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:xxx");
if (result.hasError()) {
assertThat(keepGoing()).isTrue();
throw new ViewCreationFailedException("Analysis failed");
}
return getConfiguredTarget("//test:xxx");
}
use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.
the class SkylarkAspectsTest method aspectParametersBadDefault.
@Test
public void aspectParametersBadDefault() throws Exception {
scratch.file("test/aspect.bzl", "def _impl(target, ctx):", " return struct()", "def _rule_impl(ctx):", " return struct()", "MyAspectBadDefault = aspect(", " implementation=_impl,", " attrs = { 'my_attr' : attr.string(values=['a'], default='b') },", ")", "my_rule = rule(", " implementation=_rule_impl,", " attrs = { 'deps' : attr.label_list(aspects=[MyAspectBadDefault]) },", ")");
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("ERROR /workspace/test/aspect.bzl:5:22: " + "Aspect parameter attribute 'my_attr' has a bad default value: 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 aspectsDoNotAttachToFiles.
@Test
public void aspectsDoNotAttachToFiles() throws Exception {
FileSystemUtils.appendIsoLatin1(scratch.resolve("WORKSPACE"), "bind(name = 'yyy', actual = '//test:zzz.jar')");
scratch.file("test/aspect.bzl", "def _impl(target, ctx):", " return struct()", "", "MyAspect = aspect(", " implementation=_impl,", " attr_aspects=['deps'],", ")");
scratch.file("test/zzz.jar");
scratch.file("test/BUILD", "exports_files(['zzz.jar'])", "java_library(", " name = 'xxx',", " srcs = ['A.java'],", " deps = ['//external:yyy'],", ")");
reporter.removeHandler(failFastHandler);
try {
AnalysisResult result = update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:xxx");
assertThat(keepGoing()).isTrue();
assertThat(result.hasError()).isTrue();
} catch (ViewCreationFailedException expected) {
assertThat(expected.getMessage()).contains("Analysis of aspect '/test/aspect%MyAspect of //test:xxx' failed");
}
assertContainsEvent("//test:aspect.bzl%MyAspect is attached to source file zzz.jar but " + "aspects must be attached to rules");
}
use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.
the class SkylarkAspectsTest method topLevelAspectDoesNotExist.
@Test
public void topLevelAspectDoesNotExist() throws Exception {
scratch.file("test/aspect.bzl", "");
scratch.file("test/BUILD", "java_library(name = 'xxx')");
reporter.removeHandler(failFastHandler);
try {
AnalysisResult result = update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:xxx");
assertThat(keepGoing()).isTrue();
assertThat(result.hasError()).isTrue();
} catch (ViewCreationFailedException e) {
// expect to fail.
}
assertContainsEvent("MyAspect from //test:aspect.bzl is not an aspect");
}
Aggregations