Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_configure_parser():
p = configure_parser('foo', log_params=False)
assert p.description == 'foo'
with pytest.raises(SystemExit):
p.parse_args(['--version'])
with pytest.raises(SystemExit):
p.parse_args(['-h'])
with pytest.raises(configargparse.ArgumentError):
p.parse_args(['--log-file', 'foo.log'])
c = p.parse_args([])
assert c.configuration is None
def test_error_handler():
with mock.patch('piwheels.terminal.logging') as logging:
assert error_handler(SystemExit, 0, None) == 0
assert error_handler(KeyboardInterrupt, 'Ctrl+C pressed', None) == 2
assert logging.critical.call_count == 0
assert error_handler(configargparse.ArgumentError, 'foo', None) == 2
assert logging.critical.call_args_list == [
mock.call('foo'),
mock.call('Try the --help option for more information.'),
]
logging.reset_mock()
assert error_handler(IOError, 'File not found', None) == 1
assert logging.critical.call_args == mock.call('File not found')
logging.reset_mock()
with mock.patch('traceback.format_exception') as fmt_exc:
fmt_exc.side_effect = lambda t, v, tb: [v]
assert error_handler(ValueError, 'Foo%bar', None) == 1
assert logging.critical.call_args == mock.call('Foo%%bar')
if action.choices is not None and value not in action.choices:
args = {"value" : value,
"choices" : ", ".join(map(repr, action.choices))
}
msg = _argparse.argparse._("invalid choice: %(value)r (choose from %(choices)s)")
# If the value is a string, then strip whitespace and try a case insensitive search.
if type(value) is str:
new_value = value.replace(" ", "").upper()
choices = [x.replace(" ", "").upper() for x in action.choices]
# Check whether we now have a match.
if new_value not in choices:
raise _argparse.ArgumentError(action, msg % args)
else:
raise _argparse.ArgumentError(action, msg % args)
def _check_value(action, value):
"""Helper function to overload argparse's choice checker."""
if action.choices is not None and value not in action.choices:
args = {"value" : value,
"choices" : ", ".join(map(repr, action.choices))
}
msg = _argparse.argparse._("invalid choice: %(value)r (choose from %(choices)s)")
# If the value is a string, then strip whitespace and try a case insensitive search.
if type(value) is str:
new_value = value.replace(" ", "").upper()
choices = [x.replace(" ", "").upper() for x in action.choices]
# Check whether we now have a match.
if new_value not in choices:
raise _argparse.ArgumentError(action, msg % args)
else:
raise _argparse.ArgumentError(action, msg % args)
def error(self, message):
raise configargparse.ArgumentError(None, message)
def __init__(self):
self._config = OrderedDict({
SystemExit: (None, self.exc_value),
KeyboardInterrupt: (None, 2),
IOError: (self.exc_message, 1),
configargparse.ArgumentError: (
lambda exc_type, exc_value, exc_tb:
[exc_value, 'Try the --help option for more information.'], 2
),