Search in sources :

Example 51 with AnalysisResult

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

Example 52 with AnalysisResult

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");
}
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 53 with AnalysisResult

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");
}
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 54 with AnalysisResult

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

Example 55 with AnalysisResult

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.");
}
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