Search in sources :

Example 46 with Attribute

use of com.google.devtools.build.lib.packages.Attribute in project bazel by bazelbuild.

the class ConfiguredAttributeMapperTest method testLabelVisitation.

/**
   * Tests that label visitation only travels down configuration-appropriate paths.
   */
@Test
public void testLabelVisitation() throws Exception {
    writeConfigRules();
    scratch.file("a/BUILD", "sh_binary(", "    name = 'bin',", "    srcs = ['bin.sh'],", "    deps = select({", "        '//conditions:a': [':adep'],", "        '//conditions:b': [':bdep'],", "        '" + BuildType.Selector.DEFAULT_CONDITION_KEY + "': [':defaultdep'],", "    }))", "sh_library(", "    name = 'adep',", "    srcs = ['adep.sh'])", "sh_library(", "    name = 'bdep',", "    srcs = ['bdep.sh'])", "sh_library(", "    name = 'defaultdep',", "    srcs = ['defaultdep.sh'])");
    final List<Label> visitedLabels = new ArrayList<>();
    AttributeMap.AcceptsLabelAttribute testVisitor = new AttributeMap.AcceptsLabelAttribute() {

        @Override
        public void acceptLabelAttribute(Label label, Attribute attribute) {
            if (label.toString().contains("//a:")) {
                // Ignore implicit common dependencies.
                visitedLabels.add(label);
            }
        }
    };
    final Label binSrc = Label.parseAbsolute("//a:bin.sh");
    useConfiguration("--define", "mode=a");
    getMapper("//a:bin").visitLabels(testVisitor);
    assertThat(visitedLabels).containsExactlyElementsIn(ImmutableList.of(binSrc, Label.parseAbsolute("//a:adep")));
    visitedLabels.clear();
    useConfiguration("--define", "mode=b");
    getMapper("//a:bin").visitLabels(testVisitor);
    assertThat(visitedLabels).containsExactlyElementsIn(ImmutableList.of(binSrc, Label.parseAbsolute("//a:bdep")));
    visitedLabels.clear();
    useConfiguration("--define", "mode=c");
    getMapper("//a:bin").visitLabels(testVisitor);
    assertThat(visitedLabels).containsExactlyElementsIn(ImmutableList.of(binSrc, Label.parseAbsolute("//a:defaultdep")));
}
Also used : AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) Attribute(com.google.devtools.build.lib.packages.Attribute) ArrayList(java.util.ArrayList) Label(com.google.devtools.build.lib.cmdline.Label) Test(org.junit.Test)

Example 47 with Attribute

use of com.google.devtools.build.lib.packages.Attribute in project bazel by bazelbuild.

the class DependencyResolverTest method hasAllAttributesAspect.

@Test
public void hasAllAttributesAspect() throws Exception {
    setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.SimpleRule());
    pkg("a", "simple(name='a', foo=[':b'])", "simple(name='b', foo=[])");
    OrderedSetMultimap<Attribute, Dependency> map = dependentNodeMap("//a:a", TestAspects.ALL_ATTRIBUTES_ASPECT);
    assertDep(map, "foo", "//a:b", new AspectDescriptor(TestAspects.ALL_ATTRIBUTES_ASPECT));
}
Also used : Attribute(com.google.devtools.build.lib.packages.Attribute) AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor) TestAspects(com.google.devtools.build.lib.analysis.util.TestAspects) Test(org.junit.Test)

Example 48 with Attribute

use of com.google.devtools.build.lib.packages.Attribute in project bazel by bazelbuild.

the class DependencyResolverTest method assertDep.

@SafeVarargs
private final Dependency assertDep(OrderedSetMultimap<Attribute, Dependency> dependentNodeMap, String attrName, String dep, AspectDescriptor... aspects) {
    Attribute attr = null;
    for (Attribute candidate : dependentNodeMap.keySet()) {
        if (candidate.getName().equals(attrName)) {
            attr = candidate;
            break;
        }
    }
    assertNotNull("Attribute '" + attrName + "' not found", attr);
    Dependency dependency = null;
    for (Dependency candidate : dependentNodeMap.get(attr)) {
        if (candidate.getLabel().toString().equals(dep)) {
            dependency = candidate;
            break;
        }
    }
    assertNotNull("Dependency '" + dep + "' on attribute '" + attrName + "' not found", dependency);
    assertThat(dependency.getAspects().getAllAspects()).containsExactly((Object[]) aspects);
    return dependency;
}
Also used : Attribute(com.google.devtools.build.lib.packages.Attribute)

Example 49 with Attribute

use of com.google.devtools.build.lib.packages.Attribute in project bazel by bazelbuild.

the class DependencyResolverTest method hasAspectsRequiredByRule.

@Test
public void hasAspectsRequiredByRule() throws Exception {
    setRulesAvailableInTests(new AspectRequiringRule(), new TestAspects.BaseRule());
    pkg("a", "aspect(name='a', foo=[':b'])", "aspect(name='b', foo=[])");
    OrderedSetMultimap<Attribute, Dependency> map = dependentNodeMap("//a:a", null);
    assertDep(map, "foo", "//a:b", new AspectDescriptor(TestAspects.SIMPLE_ASPECT));
}
Also used : Attribute(com.google.devtools.build.lib.packages.Attribute) AspectRequiringRule(com.google.devtools.build.lib.analysis.util.TestAspects.AspectRequiringRule) AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor) TestAspects(com.google.devtools.build.lib.analysis.util.TestAspects) Test(org.junit.Test)

Example 50 with Attribute

use of com.google.devtools.build.lib.packages.Attribute in project bazel by bazelbuild.

the class SkylarkRepositoryContextTest method buildRuleClass.

protected static RuleClass buildRuleClass(Attribute... attributes) {
    RuleClass.Builder ruleClassBuilder = new RuleClass.Builder("test", RuleClassType.WORKSPACE, true);
    for (Attribute attr : attributes) {
        ruleClassBuilder.addOrOverrideAttribute(attr);
    }
    ruleClassBuilder.setWorkspaceOnly();
    ruleClassBuilder.setConfiguredTargetFunction(new BuiltinFunction("test") {
    });
    return ruleClassBuilder.build();
}
Also used : BuiltinFunction(com.google.devtools.build.lib.syntax.BuiltinFunction) Attribute(com.google.devtools.build.lib.packages.Attribute) RuleClass(com.google.devtools.build.lib.packages.RuleClass)

Aggregations

Attribute (com.google.devtools.build.lib.packages.Attribute)76 Test (org.junit.Test)37 Label (com.google.devtools.build.lib.cmdline.Label)20 Rule (com.google.devtools.build.lib.packages.Rule)20 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)7 AttributeMap (com.google.devtools.build.lib.packages.AttributeMap)7 Target (com.google.devtools.build.lib.packages.Target)7 ArrayList (java.util.ArrayList)7 LinkedHashMap (java.util.LinkedHashMap)7 ImmutableList (com.google.common.collect.ImmutableList)6 ImmutableMap (com.google.common.collect.ImmutableMap)6 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)6 ConfigMatchingProvider (com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider)6 RuleClass (com.google.devtools.build.lib.packages.RuleClass)6 SkyKey (com.google.devtools.build.skyframe.SkyKey)6 LinkedHashSet (java.util.LinkedHashSet)6 Nullable (javax.annotation.Nullable)6 AspectDescriptor (com.google.devtools.build.lib.packages.AspectDescriptor)5 Map (java.util.Map)5 Dependency (com.google.devtools.build.lib.analysis.Dependency)4