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\"])");
}
}
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;
}
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());
}
}
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\"])");
}
}
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)'");
}
}
Aggregations