Search in sources :

Example 1 with ConversionException

use of com.google.devtools.build.lib.syntax.Type.ConversionException in project bazel by bazelbuild.

the class BuildTypeTest method testLabelKeyedStringDictConvertingMapWithMultipleEquivalentKeysShouldFail.

@Test
public void testLabelKeyedStringDictConvertingMapWithMultipleEquivalentKeysShouldFail() throws Exception {
    Label context = Label.parseAbsolute("//current/package:this");
    Map<String, String> input = new ImmutableMap.Builder<String, String>().put(":reference", "value1").put("//current/package:reference", "value2").build();
    try {
        BuildType.LABEL_KEYED_STRING_DICT.convert(input, null, context);
        fail("Expected a conversion exception to be thrown.");
    } catch (ConversionException expected) {
        assertThat(expected).hasMessage("duplicate labels: //current/package:reference " + "(as [\":reference\", \"//current/package:reference\"])");
    }
}
Also used : ConversionException(com.google.devtools.build.lib.syntax.Type.ConversionException) Label(com.google.devtools.build.lib.cmdline.Label) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 2 with ConversionException

use of com.google.devtools.build.lib.syntax.Type.ConversionException in project bazel by bazelbuild.

the class RuleClass method populateDefinedRuleAttributeValues.

/**
   * Populates the attributes table of the new {@link Rule} with the values in the {@code
   * attributeValues} map.
   *
   * <p>Handles the special cases of the attribute named {@code "name"} and attributes with value
   * {@link Runtime#NONE}.
   *
   * <p>Returns a bitset {@code b} where {@code b.get(i)} is {@code true} if this method set a
   * value for the attribute with index {@code i} in this {@link RuleClass}. Errors are reported
   * on {@code eventHandler}.
   */
private BitSet populateDefinedRuleAttributeValues(Rule rule, AttributeValuesMap attributeValues, EventHandler eventHandler) {
    BitSet definedAttrIndices = new BitSet();
    for (String attributeName : attributeValues.getAttributeNames()) {
        Object attributeValue = attributeValues.getAttributeValue(attributeName);
        // Ignore all None values.
        if (attributeValue == Runtime.NONE) {
            continue;
        }
        // Check that the attribute's name belongs to a valid attribute for this rule class.
        Integer attrIndex = getAttributeIndex(attributeName);
        if (attrIndex == null) {
            rule.reportError(String.format("%s: no such attribute '%s' in '%s' rule", rule.getLabel(), attributeName, name), eventHandler);
            continue;
        }
        Attribute attr = getAttribute(attrIndex);
        // Convert the build-lang value to a native value, if necessary.
        Object nativeAttributeValue;
        if (attributeValues.valuesAreBuildLanguageTyped()) {
            try {
                nativeAttributeValue = convertFromBuildLangType(rule, attr, attributeValue);
            } catch (ConversionException e) {
                rule.reportError(String.format("%s: %s", rule.getLabel(), e.getMessage()), eventHandler);
                continue;
            }
        } else {
            nativeAttributeValue = attributeValue;
        }
        boolean explicit = attributeValues.isAttributeExplicitlySpecified(attributeName);
        setRuleAttributeValue(rule, eventHandler, attr, nativeAttributeValue, explicit);
        definedAttrIndices.set(attrIndex);
    }
    return definedAttrIndices;
}
Also used : ConversionException(com.google.devtools.build.lib.syntax.Type.ConversionException) BitSet(java.util.BitSet)

Example 3 with ConversionException

use of com.google.devtools.build.lib.syntax.Type.ConversionException in project bazel by bazelbuild.

the class AspectFunction method loadSkylarkAspect.

/**
   * Load Skylark aspect from an extension file. Is to be called from a SkyFunction.
   *
   * @return {@code null} if dependencies cannot be satisfied.
   */
@Nullable
static SkylarkAspect loadSkylarkAspect(Environment env, Label extensionLabel, String skylarkValueName) throws AspectCreationException, InterruptedException {
    SkyKey importFileKey = SkylarkImportLookupValue.key(extensionLabel, false);
    try {
        SkylarkImportLookupValue skylarkImportLookupValue = (SkylarkImportLookupValue) env.getValueOrThrow(importFileKey, SkylarkImportFailedException.class);
        if (skylarkImportLookupValue == null) {
            return null;
        }
        Object skylarkValue = skylarkImportLookupValue.getEnvironmentExtension().get(skylarkValueName);
        if (!(skylarkValue instanceof SkylarkAspect)) {
            throw new ConversionException(skylarkValueName + " from " + extensionLabel.toString() + " is not an aspect");
        }
        return (SkylarkAspect) skylarkValue;
    } catch (SkylarkImportFailedException | ConversionException e) {
        env.getListener().handle(Event.error(e.getMessage()));
        throw new AspectCreationException(e.getMessage());
    }
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) ConversionException(com.google.devtools.build.lib.syntax.Type.ConversionException) SkylarkAspect(com.google.devtools.build.lib.packages.SkylarkAspect) SkylarkImportFailedException(com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException) Nullable(javax.annotation.Nullable)

Example 4 with ConversionException

use of com.google.devtools.build.lib.syntax.Type.ConversionException in project bazel by bazelbuild.

the class BuildTypeTest method testLabelKeyedStringDictConvertingMapWithMultipleSetsOfEquivalentKeysShouldFail.

@Test
public void testLabelKeyedStringDictConvertingMapWithMultipleSetsOfEquivalentKeysShouldFail() throws Exception {
    Label context = Label.parseAbsolute("//current/rule:sibling");
    Map<String, String> input = new ImmutableMap.Builder<String, String>().put(":rule", "first set").put("//current/rule:rule", "also first set").put("//other/package:package", "interrupting rule").put("//other/package", "interrupting rule's friend").put("//current/rule", "part of first set but non-contiguous in iteration order").put("//not/involved/in/any:collisions", "same value").put("//also/not/involved/in/any:collisions", "same value").build();
    try {
        BuildType.LABEL_KEYED_STRING_DICT.convert(input, null, context);
        fail("Expected a conversion exception to be thrown.");
    } catch (ConversionException expected) {
        assertThat(expected).hasMessage("duplicate labels: //current/rule:rule " + "(as [\":rule\", \"//current/rule:rule\", \"//current/rule\"]), " + "//other/package:package " + "(as [\"//other/package:package\", \"//other/package\"])");
    }
}
Also used : ConversionException(com.google.devtools.build.lib.syntax.Type.ConversionException) Label(com.google.devtools.build.lib.cmdline.Label) Test(org.junit.Test)

Example 5 with ConversionException

use of com.google.devtools.build.lib.syntax.Type.ConversionException in project bazel by bazelbuild.

the class BuildTypeTest method testConvertDoesNotAcceptSelectables.

/**
   * Tests that {@link com.google.devtools.build.lib.syntax.Type#convert} fails on selector inputs.
   */
@Test
public void testConvertDoesNotAcceptSelectables() throws Exception {
    Object selectableInput = SelectorList.of(new SelectorValue(ImmutableMap.of("//conditions:a", Arrays.asList("//a:a1", "//a:a2")), ""));
    try {
        BuildType.LABEL_LIST.convert(selectableInput, null, currentRule);
        fail("Expected conversion to fail on a selectable input");
    } catch (ConversionException e) {
        assertThat(e.getMessage()).contains("expected value of type 'list(label)'");
    }
}
Also used : ConversionException(com.google.devtools.build.lib.syntax.Type.ConversionException) SelectorValue(com.google.devtools.build.lib.syntax.SelectorValue) Test(org.junit.Test)

Aggregations

ConversionException (com.google.devtools.build.lib.syntax.Type.ConversionException)6 Test (org.junit.Test)4 Label (com.google.devtools.build.lib.cmdline.Label)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 SkylarkAspect (com.google.devtools.build.lib.packages.SkylarkAspect)1 SkylarkImportFailedException (com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException)1 SelectorValue (com.google.devtools.build.lib.syntax.SelectorValue)1 SkyKey (com.google.devtools.build.skyframe.SkyKey)1 BitSet (java.util.BitSet)1 Nullable (javax.annotation.Nullable)1