Handle None and hex values in string format

pull/8673/head
kegilbert 2018-11-13 15:56:20 -06:00
parent b53738750f
commit 7a436660e9
6 changed files with 97 additions and 23 deletions

View File

@ -21,6 +21,7 @@ from six import moves
import json
import six
import os
import re
from os.path import dirname, abspath, exists, join, isabs
import sys
from collections import namedtuple
@ -1072,20 +1073,63 @@ class Config(object):
accepted = param.accepted_values
value = param.value
if (min is not None or max is not None) and (accepted is not None):
err_msg += "\n%s has both a range and list of accepted values specified. Please only "\
"specify either value_min and/or value_max, or accepted_values."\
% param
else:
if (value < min or (value > max if max is not None else False)):
err_msg += "\nInvalid config range for %s, is not in the required range: [%s:%s]"\
% (param,
min if min is not None else "-inf",
max if max is not None else "inf")
# Config parameters that are only defined but do not have a default
# value should not be range limited
if value is not None:
if (min is not None or max is not None) and (accepted is not None):
err_msg += "\n%s has both a range and list of accepted values specified. Please only "\
"specify either value_min and/or value_max, or accepted_values"\
% param
else:
if re.match(r'^(0[xX])[A-Fa-f0-9]+$|^[0-9]+$', str(value)):
# Value is a hexadecimal or numerical string value
# Convert to a python integer and range check/compare to
# accepted list accordingly
if re.match(r'^(0[xX])[A-Fa-f0-9]+$', str(value)):
value = int(value, 16)
elif value is not None:
value = int(value)
if accepted and str(value) not in accepted:
err_msg += "\nInvalid config range for %s, is not an accepted value: %s"\
% (param, accepted)
if min is not None or max is not None:
# Numerical range check
# Convert hex strings to integers for range checks
if re.match(r'^(0[xX])[A-Fa-f0-9]+$', str(min)) if min is not None else False:
min = int(min, 16)
elif min is not None:
min = int(min)
if re.match(r'^(0[xX])[A-Fa-f0-9]+$', str(max)) if max is not None else False:
max = int(max, 16)
elif max is not None:
max = int(max)
if (value < min or (value > max if max is not None else False)):
err_msg += "\nInvalid config range for %s, is not in the required range: [%s:%s]"\
% (param,
min if min is not None else "-inf",
max if max is not None else "inf")
elif accepted is not None:
# Numerical accepted value check
integer_accepted_list = []
for acc in accepted.split(','):
if re.match(r'^(0[xX])[A-Fa-f0-9]+$', str(acc.replace(' ', ''))):
integer_accepted_list.append(int(acc, 16))
else:
integer_accepted_list.append(int(acc))
if value not in integer_accepted_list:
err_msg += "\nInvalid config range for %s, is not an accepted value: %s"\
% (param, accepted)
else:
if min is not None or max is not None:
err_msg += "\nInvalid config range settings for %s. Range specifiers are not "\
"applicable to non-decimal/hexadecimal string values" % param
if accepted is not None:
# Generate list of stripped words from string to not allow a value of "test" to pass
# on a list of accepted words consisting of "test1, test2, test3..."
accepted_list = [w.replace(' ', '') for w in accepted.split(',')]
if value not in accepted_list:
err_msg += "\nInvalid config range for %s, is not an accepted value: %s"\
% (param, accepted)
if (err_msg):
raise ConfigException(err_msg)

View File

@ -7,15 +7,32 @@
"accepted_values": "0, 5, 10"
},
"config2": {
"help": "The default value should pass as it is in the rage of accepted values",
"help": "The default value should pass as it is in the range of accepted values",
"value": 7,
"value_min": 0,
"value_max": 10
},
"config3": {
"help": "The default value should pass as it is in the rage of accepted values",
"help": "The default value should pass as it is in the range of accepted values",
"value": "foo",
"accepted_values": "foo, bar"
},
"config4": {
"help": "The default value should pass as it is in the range of accepted values",
"value": "0x1000",
"value_min": "0x10",
"value_max": "0x8000"
},
"config5": {
"help": "The default value should pass as it is in the range of accepted values",
"value": "0x2000",
"value_min": 0,
"value_max": "0x8000"
},
"config6": {
"help": "The default value should pass as it is in the list of accepted values",
"value": "0x8000",
"accepted_values": "0x1000, 0x8000, 0x12000"
}
}
}

View File

@ -2,6 +2,9 @@
"test_target": {
"lib1.config1": 5,
"lib1.config2": 7,
"lib1.config3": "foo"
"lib1.config3": "foo",
"lib1.config4": "0x1000",
"lib1.config5": "0x2000",
"lib1.config6": "0x8000"
}
}

View File

@ -2,23 +2,23 @@
"name": "lib1",
"config": {
"config1": {
"help": "The default value should fail as it is not in the rage of accepted values",
"help": "The default value should fail as it is not in the range of accepted values",
"value": 99,
"accepted_values": "0, 5, 10"
},
"config2": {
"help": "The default value should fail as it is not in the rage of accepted values",
"help": "The default value should fail as it is not in the range of accepted values",
"value": 100,
"value_min": 0,
"value_max": 10
},
"config3": {
"help": "The default value should fail as it is not in the rage of accepted values",
"help": "The default value should fail as it is not in the range of accepted values",
"value": 101,
"value_min": 102
},
"config4": {
"help": "The default value should fail as it is not in the rage of accepted values",
"help": "The default value should fail as it is not in the range of accepted values",
"value": 102,
"value_max": 101
},
@ -26,7 +26,17 @@
"help": "The default value should fail as it specified both a range and list of accepted values",
"value": 103,
"value_max": 104,
"accepted_values": "103"
"accepted_values": "103"
},
"config6": {
"help": "The default value should fail as it is not in the range of accepted values",
"value": "0x1000",
"value_max": "0x500"
},
"config7": {
"help": "The default value should fail as it is a non-decimal string with a max value",
"value": "test",
"value_max": "?"
}
}
}

View File

@ -1,5 +1,5 @@
{
"test_target": {
"exception_msg": "\nInvalid config range for lib1.config1 = 99 (macro name: \"MBED_CONF_LIB1_CONFIG1\"), is not an accepted value: 0, 5, 10\nInvalid config range for lib1.config2 = 100 (macro name: \"MBED_CONF_LIB1_CONFIG2\"), is not in the required range: [0:10]\nInvalid config range for lib1.config3 = 101 (macro name: \"MBED_CONF_LIB1_CONFIG3\"), is not in the required range: [102:inf]\nInvalid config range for lib1.config4 = 102 (macro name: \"MBED_CONF_LIB1_CONFIG4\"), is not in the required range: [-inf:101]\nlib1.config5 = 103 (macro name: \"MBED_CONF_LIB1_CONFIG5\") has both a range and list of accepted values specified. Please only specify either value_min and/or value_max, or accepted_values."
"exception_msg": "\nInvalid config range for lib1.config1 = 99 (macro name: \"MBED_CONF_LIB1_CONFIG1\"), is not an accepted value: 0, 5, 10\nInvalid config range for lib1.config2 = 100 (macro name: \"MBED_CONF_LIB1_CONFIG2\"), is not in the required range: [0:10]\nInvalid config range for lib1.config3 = 101 (macro name: \"MBED_CONF_LIB1_CONFIG3\"), is not in the required range: [102:inf]\nInvalid config range for lib1.config4 = 102 (macro name: \"MBED_CONF_LIB1_CONFIG4\"), is not in the required range: [-inf:101]\nlib1.config5 = 103 (macro name: \"MBED_CONF_LIB1_CONFIG5\") has both a range and list of accepted values specified. Please only specify either value_min and/or value_max, or accepted_values\nInvalid config range for lib1.config6 = 0x1000 (macro name: \"MBED_CONF_LIB1_CONFIG6\"), is not in the required range: [-inf:1280]\nInvalid config range settings for lib1.config7 = test (macro name: \"MBED_CONF_LIB1_CONFIG7\"). Range specifiers are not applicable to non-decimal/hexadecimal string values"
}
}

View File

@ -1,6 +1,6 @@
{
"test_target": {
"lib1.config1": 8,
"lib1.config1": 12,
"exception_msg": "Invalid config range for lib1.config1 = 12 (macro name: \"MBED_CONF_LIB1_CONFIG1\"), is not in the required range: [0:10]"
}
}