Search in sources :

Example 21 with AnalysisResult

use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.

the class SkylarkAspectsTest method aspectAdvertisingProviders.

@Test
public void aspectAdvertisingProviders() throws Exception {
    scratch.file("test/aspect.bzl", "def _impl(target, ctx):", "   return struct()", "my_aspect = aspect(_impl, provides = ['foo'])", "a_dict = { 'foo' : attr.label_list(aspects = [my_aspect]) }");
    scratch.file("test/BUILD", "java_library(name = 'xxx',)");
    reporter.removeHandler(failFastHandler);
    try {
        AnalysisResult analysisResult = update(ImmutableList.of("//test:aspect.bzl%my_aspect"), "//test:xxx");
        assertThat(keepGoing()).isTrue();
        assertThat(analysisResult.hasError()).isTrue();
    } catch (ViewCreationFailedException e) {
    // expect exception
    }
    assertContainsEvent("Aspect '//test:aspect.bzl%my_aspect', applied to '//test:xxx', " + "does not provide advertised provider 'foo'");
}
Also used : ViewCreationFailedException(com.google.devtools.build.lib.analysis.ViewCreationFailedException) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Test(org.junit.Test)

Example 22 with AnalysisResult

use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.

the class SkylarkAspectsTest method aspectWithDeclaredProvidersInAStruct.

@Test
public void aspectWithDeclaredProvidersInAStruct() throws Exception {
    scratch.file("test/aspect.bzl", "foo = provider()", "bar = provider()", "def _impl(target, ctx):", "   return struct(foobar='foobar', providers=[foo(), bar()])", "MyAspect = aspect(implementation=_impl)");
    scratch.file("test/BUILD", "java_library(name = 'xxx',)");
    AnalysisResult analysisResult = update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:xxx");
    assertThat(getLabelsToBuild(analysisResult)).containsExactly("//test:xxx");
    assertThat(getAspectDescriptions(analysisResult)).containsExactly("//test:aspect.bzl%MyAspect(//test:xxx)");
    List<Key> providers = getDeclaredProviderKeys(analysisResult);
    assertThat((providers.get(0))).isEqualTo(new SkylarkKey(Label.parseAbsolute("//test:aspect.bzl"), "foo"));
    assertThat((providers.get(1))).isEqualTo(new SkylarkKey(Label.parseAbsolute("//test:aspect.bzl"), "bar"));
}
Also used : SkylarkKey(com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) SkylarkKey(com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey) Key(com.google.devtools.build.lib.packages.ClassObjectConstructor.Key) Test(org.junit.Test)

Example 23 with AnalysisResult

use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.

the class SkylarkAspectsTest method aspectOnAspectDiamond.

/**
   * Diamond case.
   * rule r1 depends or r0 with aspect a1.
   * rule r2 depends or r0 with aspect a2.
   * rule rcollect depends on r1, r2 with aspect a3.
   *
   * Aspect a3 should be applied twice to target r0: once in [a1, a3] sequence
   * and once in [a2, a3] sequence.
   */
@Test
public void aspectOnAspectDiamond() throws Exception {
    scratch.file("test/aspect.bzl", "def _a1_impl(target,ctx):", "  return struct(a1p = 'text from a1')", "a1 = aspect(_a1_impl, attr_aspects = ['deps'], provides = ['a1p'])", "", "def _a2_impl(target,ctx):", "  return struct(a2p = 'text from a2')", "a2 = aspect(_a2_impl, attr_aspects = ['deps'], provides = ['a2p'])", "", "def _a3_impl(target,ctx):", "  value = []", "  f = ctx.new_file('a3.out')", "  ctx.file_action(f, 'text')", "  for dep in ctx.rule.attr.deps:", "     if hasattr(dep, 'a3p'):", "         value += dep.a3p", "  s = str(target.label) + str(ctx.aspect_ids) + '='", "  if hasattr(target, 'a1p'):", "     s += 'a1p'", "  if hasattr(target, 'a2p'):", "     s += 'a2p'", "  value.append(s)", "  return struct(a3p = value)", "a3 = aspect(_a3_impl, attr_aspects = ['deps'],", "            required_aspect_providers = [['a1p'], ['a2p']])", "def _r1_impl(ctx):", "  pass", "def _rcollect_impl(ctx):", "  value = []", "  for dep in ctx.attr.deps:", "     if hasattr(dep, 'a3p'):", "         value += dep.a3p", "  return struct(result = value)", "r1 = rule(_r1_impl, attrs = { 'deps' : attr.label_list(aspects = [a1])})", "r2 = rule(_r1_impl, attrs = { 'deps' : attr.label_list(aspects = [a2])})", "rcollect = rule(_rcollect_impl, attrs = { 'deps' : attr.label_list(aspects = [a3])})");
    scratch.file("test/BUILD", "load(':aspect.bzl', 'r1', 'r2', 'rcollect')", "r1(name = 'r0')", "r1(name = 'r1', deps = [':r0'])", "r2(name = 'r2', deps = [':r0'])", "rcollect(name = 'rcollect', deps = [':r1', ':r2'])");
    AnalysisResult analysisResult = update("//test:rcollect");
    ConfiguredTarget target = Iterables.getOnlyElement(analysisResult.getTargetsToBuild());
    SkylarkList result = (SkylarkList) target.get("result");
    assertThat(result).containsExactly("//test:r0[\"//test:aspect.bzl%a1\", \"//test:aspect.bzl%a3\"]=a1p", "//test:r1[\"//test:aspect.bzl%a3\"]=", "//test:r0[\"//test:aspect.bzl%a2\", \"//test:aspect.bzl%a3\"]=a2p", "//test:r2[\"//test:aspect.bzl%a3\"]=");
}
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 24 with AnalysisResult

use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.

the class SkylarkAspectsTest method aspectParametersUncovered.

@Test
public void aspectParametersUncovered() throws Exception {
    scratch.file("test/aspect.bzl", "def _impl(target, ctx):", "   return struct()", "def _rule_impl(ctx):", "   return struct()", "MyAspectUncovered = aspect(", "    implementation=_impl,", "    attrs = { 'my_attr' : attr.string(values=['aaa']) },", ")", "my_rule = rule(", "    implementation=_rule_impl,", "    attrs = { 'deps' : attr.label_list(aspects=[MyAspectUncovered]) },", ")");
    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("Aspect //test:aspect.bzl%MyAspectUncovered requires rule my_rule to specify attribute " + "'my_attr' with type string.");
}
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 25 with AnalysisResult

use of com.google.devtools.build.lib.analysis.BuildView.AnalysisResult in project bazel by bazelbuild.

the class SkylarkAspectsTest method aspectsFromSkylarkRules.

@Test
public void aspectsFromSkylarkRules() throws Exception {
    scratch.file("test/aspect.bzl", "def _aspect_impl(target, ctx):", "   s = depset([target.label])", "   for i in ctx.rule.attr.deps:", "       s += i.target_labels", "   return struct(target_labels = s)", "", "def _rule_impl(ctx):", "   s = depset([])", "   for i in ctx.attr.attr:", "       s += i.target_labels", "   return struct(rule_deps = s)", "", "MyAspect = aspect(", "   implementation=_aspect_impl,", "   attr_aspects=['deps'],", ")", "my_rule = rule(", "   implementation=_rule_impl,", "   attrs = { 'attr' : ", "             attr.label_list(mandatory=True, allow_files=True, 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");
    assertThat(getLabelsToBuild(analysisResult)).containsExactly("//test:xxx");
    ConfiguredTarget target = analysisResult.getTargetsToBuild().iterator().next();
    SkylarkProviders skylarkProviders = target.getProvider(SkylarkProviders.class);
    assertThat(skylarkProviders).isNotNull();
    Object names = skylarkProviders.getValue("rule_deps");
    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:yyy");
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) Label(com.google.devtools.build.lib.cmdline.Label) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Nullable(javax.annotation.Nullable) 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