use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.
the class SkylarkAspectsTest method aspectPropagating.
@Test
public void aspectPropagating() throws Exception {
scratch.file("test/aspect.bzl", "def _impl(target, ctx):", " s = depset([target.label])", " c = depset([ctx.rule.kind])", " for i in ctx.rule.attr.deps:", " s += i.target_labels", " c += i.rule_kinds", " return struct(target_labels = s, rule_kinds = c)", "", "MyAspect = aspect(", " implementation=_impl,", " attr_aspects=['deps'],", ")");
scratch.file("test/BUILD", "java_library(", " name = 'yyy',", ")", "java_library(", " name = 'xxx',", " srcs = ['A.java'],", " deps = [':yyy'],", ")");
AnalysisResult analysisResult = update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:xxx");
assertThat(getLabelsToBuild(analysisResult)).containsExactly("//test:xxx");
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 o.toString();
}
})).containsExactly("//test:xxx", "//test:yyy");
Object ruleKinds = skylarkProviders.getValue("rule_kinds");
assertThat(ruleKinds).isInstanceOf(SkylarkNestedSet.class);
assertThat(((SkylarkNestedSet) ruleKinds).toCollection()).containsExactly("java_library");
}
use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.
the class SkylarkAspectsTest method aspectDescriptions.
@Test
public void aspectDescriptions() throws Exception {
scratch.file("test/aspect.bzl", "def _a_impl(target,ctx):", " s = str(target.label) + str(ctx.aspect_ids) + '='", " value = []", " if ctx.rule.attr.dep:", " d = ctx.rule.attr.dep", " this_id = ctx.aspect_ids[len(ctx.aspect_ids) - 1]", " s += str(d.label) + str(d.my_ids) + ',' + str(this_id in d.my_ids)", " value += ctx.rule.attr.dep.ap", " else:", " s += 'None'", " value.append(s)", " return struct(ap = value, my_ids = ctx.aspect_ids)", "a = aspect(_a_impl, attr_aspects = ['dep'])", "def _r_impl(ctx):", " if not ctx.attr.dep:", " return struct(result = [])", " return struct(result = ctx.attr.dep.ap)", "r = rule(_r_impl, attrs = { 'dep' : attr.label(aspects = [a])})");
scratch.file("test/BUILD", "load(':aspect.bzl', 'r')", "r(name = 'r0')", "r(name = 'r1', dep = ':r0')", "r(name = 'r2', dep = ':r1')");
AnalysisResult analysisResult = update("//test:r2");
ConfiguredTarget target = Iterables.getOnlyElement(analysisResult.getTargetsToBuild());
SkylarkList<?> result = (SkylarkList<?>) target.get("result");
assertThat(result).containsExactly("//test:r0[\"//test:aspect.bzl%a\"]=None", "//test:r1[\"//test:aspect.bzl%a\"]=//test:r0[\"//test:aspect.bzl%a\"],True");
}
use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.
the class SkylarkAspectsTest method topLevelAspectDoesNotExist2.
@Test
public void topLevelAspectDoesNotExist2() throws Exception {
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("Extension file not found. Unable to load file '//test:aspect.bzl': " + "file doesn't exist or isn't a file");
}
use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult 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.BuildView.AnalysisResult in project bazel by bazelbuild.
the class SkylarkAspectsTest method aspectFailingExecution.
@Test
public void aspectFailingExecution() throws Exception {
scratch.file("test/aspect.bzl", "def _impl(target, ctx):", " return 1/0", "", "MyAspect = aspect(implementation=_impl)");
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("ERROR /workspace/test/BUILD:1:1: in " + "//test:aspect.bzl%MyAspect aspect on java_library rule //test:xxx: \n" + "Traceback (most recent call last):" + LINE_SEPARATOR + "\tFile \"/workspace/test/BUILD\", line 1" + LINE_SEPARATOR + "\t\t//test:aspect.bzl%MyAspect(...)" + LINE_SEPARATOR + "\tFile \"/workspace/test/aspect.bzl\", line 2, in _impl" + LINE_SEPARATOR + "\t\t1 / 0" + LINE_SEPARATOR + "integer division by zero");
}
Aggregations