Search in sources :

Example 41 with AnalysisResult

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");
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) AspectValue(com.google.devtools.build.lib.skyframe.AspectValue) Label(com.google.devtools.build.lib.cmdline.Label) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Nullable(javax.annotation.Nullable) SkylarkNestedSet(com.google.devtools.build.lib.syntax.SkylarkNestedSet) Test(org.junit.Test)

Example 42 with AnalysisResult

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");
}
Also used : SkylarkList(com.google.devtools.build.lib.syntax.SkylarkList) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Test(org.junit.Test)

Example 43 with AnalysisResult

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");
}
Also used : ViewCreationFailedException(com.google.devtools.build.lib.analysis.ViewCreationFailedException) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Test(org.junit.Test)

Example 44 with AnalysisResult

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");
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) AspectValue(com.google.devtools.build.lib.skyframe.AspectValue) Label(com.google.devtools.build.lib.cmdline.Label) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Example 45 with AnalysisResult

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");
}
Also used : ViewCreationFailedException(com.google.devtools.build.lib.analysis.ViewCreationFailedException) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Test(org.junit.Test)

Aggregations

AnalysisResult (com.google.devtools.build.lib.analysis.BuildView.AnalysisResult)69 Test (org.junit.Test)63 ViewCreationFailedException (com.google.devtools.build.lib.analysis.ViewCreationFailedException)21 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)12 EventBus (com.google.common.eventbus.EventBus)8 AspectValue (com.google.devtools.build.lib.skyframe.AspectValue)7 SkylarkProviders (com.google.devtools.build.lib.analysis.SkylarkProviders)6 Artifact (com.google.devtools.build.lib.actions.Artifact)5 TargetParsingException (com.google.devtools.build.lib.cmdline.TargetParsingException)5 SkylarkList (com.google.devtools.build.lib.syntax.SkylarkList)5 Nullable (javax.annotation.Nullable)5 Label (com.google.devtools.build.lib.cmdline.Label)4 OutputGroupProvider (com.google.devtools.build.lib.analysis.OutputGroupProvider)3 SkylarkNestedSet (com.google.devtools.build.lib.syntax.SkylarkNestedSet)3 Key (com.google.devtools.build.lib.packages.ClassObjectConstructor.Key)2 SkylarkKey (com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey)2 Stopwatch (com.google.common.base.Stopwatch)1 BuildFailedException (com.google.devtools.build.lib.actions.BuildFailedException)1 AnalysisPhaseCompleteEvent (com.google.devtools.build.lib.analysis.AnalysisPhaseCompleteEvent)1 BuildInfoEvent (com.google.devtools.build.lib.analysis.BuildInfoEvent)1