Search in sources :

Example 6 with BuildLangTypedAttributeValuesMap

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

the class RuleFactoryTest method testBuildRuleFailsInWorkspaceFile.

@Test
public void testBuildRuleFailsInWorkspaceFile() throws Exception {
    Path myPkgPath = scratch.resolve("/foo/workspace/WORKSPACE");
    Package.Builder pkgBuilder = packageFactory.newPackageBuilder(Label.EXTERNAL_PACKAGE_IDENTIFIER, "TESTING").setFilename(myPkgPath).setMakeEnv(new MakeEnvironment.Builder());
    Map<String, Object> attributeValues = new HashMap<>();
    attributeValues.put("name", "foo");
    attributeValues.put("alwayslink", true);
    RuleClass ruleClass = provider.getRuleClassMap().get("cc_library");
    try {
        RuleFactory.createAndAddRule(pkgBuilder, ruleClass, new BuildLangTypedAttributeValuesMap(attributeValues), new Reporter(new EventBus()), /*ast=*/
        null, Location.fromFileAndOffsets(myPkgPath.asFragment(), 42, 42), /*env=*/
        null, new AttributeContainer(ruleClass));
        fail();
    } catch (RuleFactory.InvalidRuleException e) {
        assertThat(e.getMessage()).contains("cannot be in the WORKSPACE file");
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) HashMap(java.util.HashMap) Reporter(com.google.devtools.build.lib.events.Reporter) EventBus(com.google.common.eventbus.EventBus) BuildLangTypedAttributeValuesMap(com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap) Test(org.junit.Test)

Example 7 with BuildLangTypedAttributeValuesMap

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

the class RuleFactoryTest method testCreateRule.

@Test
public void testCreateRule() throws Exception {
    Path myPkgPath = scratch.resolve("/foo/workspace/mypkg/BUILD");
    Package.Builder pkgBuilder = packageFactory.newPackageBuilder(PackageIdentifier.createInMainRepo("mypkg"), "TESTING").setFilename(myPkgPath).setMakeEnv(new MakeEnvironment.Builder());
    Map<String, Object> attributeValues = new HashMap<>();
    attributeValues.put("name", "foo");
    attributeValues.put("alwayslink", true);
    RuleClass ruleClass = provider.getRuleClassMap().get("cc_library");
    Rule rule = RuleFactory.createAndAddRule(pkgBuilder, ruleClass, new BuildLangTypedAttributeValuesMap(attributeValues), new Reporter(new EventBus()), /*ast=*/
    null, LOCATION_42, /*env=*/
    null, new AttributeContainer(ruleClass));
    assertSame(rule, rule.getAssociatedRule());
    // pkg.getRules() = [rule]
    Package pkg = pkgBuilder.build();
    assertThat(Sets.newHashSet(pkg.getTargets(Rule.class))).hasSize(1);
    assertEquals(rule, pkg.getTargets(Rule.class).iterator().next());
    assertSame(rule, pkg.getTarget("foo"));
    assertEquals(Label.parseAbsolute("//mypkg:foo"), rule.getLabel());
    assertEquals("foo", rule.getName());
    assertEquals("cc_library", rule.getRuleClass());
    assertEquals("cc_library rule", rule.getTargetKind());
    assertEquals(42, rule.getLocation().getStartOffset());
    assertFalse(rule.containsErrors());
    // Attr with explicitly-supplied value:
    AttributeMap attributes = RawAttributeMapper.of(rule);
    assertTrue(attributes.get("alwayslink", Type.BOOLEAN));
    try {
        // type error: boolean, not string!
        attributes.get("alwayslink", Type.STRING);
        fail();
    } catch (Exception e) {
    /* Class of exception and error message are not specified by API. */
    }
    try {
        // no such attribute
        attributes.get("nosuchattr", Type.STRING);
        fail();
    } catch (Exception e) {
    /* Class of exception and error message are not specified by API. */
    }
    // Attrs with default values:
    // cc_library linkstatic default=0 according to build encyc.
    assertFalse(attributes.get("linkstatic", Type.BOOLEAN));
    assertFalse(attributes.get("testonly", Type.BOOLEAN));
    assertThat(attributes.get("srcs", BuildType.LABEL_LIST)).isEmpty();
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) HashMap(java.util.HashMap) Reporter(com.google.devtools.build.lib.events.Reporter) EventBus(com.google.common.eventbus.EventBus) BuildLangTypedAttributeValuesMap(com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap) Test(org.junit.Test)

Example 8 with BuildLangTypedAttributeValuesMap

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

the class RuleFactoryTest method testCreateWorkspaceRule.

@Test
public void testCreateWorkspaceRule() throws Exception {
    Path myPkgPath = scratch.resolve("/foo/workspace/WORKSPACE");
    Package.Builder pkgBuilder = packageFactory.newExternalPackageBuilder(myPkgPath, "TESTING");
    Map<String, Object> attributeValues = new HashMap<>();
    attributeValues.put("name", "foo");
    attributeValues.put("actual", "//foo:bar");
    RuleClass ruleClass = provider.getRuleClassMap().get("bind");
    Rule rule = RuleFactory.createAndAddRule(pkgBuilder, ruleClass, new BuildLangTypedAttributeValuesMap(attributeValues), new Reporter(new EventBus()), /*ast=*/
    null, Location.fromFileAndOffsets(myPkgPath.asFragment(), 42, 42), /*env=*/
    null, new AttributeContainer(ruleClass));
    assertFalse(rule.containsErrors());
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) HashMap(java.util.HashMap) Reporter(com.google.devtools.build.lib.events.Reporter) EventBus(com.google.common.eventbus.EventBus) BuildLangTypedAttributeValuesMap(com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap) Test(org.junit.Test)

Example 9 with BuildLangTypedAttributeValuesMap

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

the class RuleFactoryTest method testOutputFileNotEqualDot.

@Test
public void testOutputFileNotEqualDot() throws Exception {
    Path myPkgPath = scratch.resolve("/foo");
    Package.Builder pkgBuilder = packageFactory.newPackageBuilder(PackageIdentifier.createInMainRepo("mypkg"), "TESTING").setFilename(myPkgPath).setMakeEnv(new MakeEnvironment.Builder());
    Map<String, Object> attributeValues = new HashMap<>();
    attributeValues.put("outs", Lists.newArrayList("."));
    attributeValues.put("name", "some");
    RuleClass ruleClass = provider.getRuleClassMap().get("genrule");
    try {
        RuleFactory.createAndAddRule(pkgBuilder, ruleClass, new BuildLangTypedAttributeValuesMap(attributeValues), new Reporter(new EventBus()), /*ast=*/
        null, Location.fromFileAndOffsets(myPkgPath.asFragment(), 42, 42), /*env=*/
        null, new AttributeContainer(ruleClass));
        fail();
    } catch (RuleFactory.InvalidRuleException e) {
        assertTrue(e.getMessage(), e.getMessage().contains("output file name can't be equal '.'"));
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) HashMap(java.util.HashMap) Reporter(com.google.devtools.build.lib.events.Reporter) EventBus(com.google.common.eventbus.EventBus) BuildLangTypedAttributeValuesMap(com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap) Test(org.junit.Test)

Aggregations

BuildLangTypedAttributeValuesMap (com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap)9 EventBus (com.google.common.eventbus.EventBus)5 Reporter (com.google.devtools.build.lib.events.Reporter)5 Path (com.google.devtools.build.lib.vfs.Path)5 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 Label (com.google.devtools.build.lib.cmdline.Label)2 StoredEventHandler (com.google.devtools.build.lib.events.StoredEventHandler)2 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)1 Map (java.util.Map)1