Search in sources :

Example 11 with AnalysisResult

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

Example 12 with AnalysisResult

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

Example 13 with AnalysisResult

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

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

Example 15 with AnalysisResult

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