Merge "Error handling for KeyValueAction class."

This commit is contained in:
Jenkins 2016-06-08 04:30:57 +00:00 committed by Gerrit Code Review
commit c272476b7b
3 changed files with 15 additions and 11 deletions

View File

@ -35,7 +35,9 @@ class KeyValueAction(argparse.Action):
if '=' in values: if '=' in values:
getattr(namespace, self.dest, {}).update([values.split('=', 1)]) getattr(namespace, self.dest, {}).update([values.split('=', 1)])
else: else:
getattr(namespace, self.dest, {}).pop(values, None) msg = _("Expected 'key=value' type, "
"but got: %s") % (str(values))
raise argparse.ArgumentTypeError(msg)
class MultiKeyValueAction(argparse.Action): class MultiKeyValueAction(argparse.Action):

View File

@ -49,16 +49,13 @@ class TestKeyValueAction(utils.TestCase):
self.assertDictEqual(expect, actual) self.assertDictEqual(expect, actual)
def test_error_values(self): def test_error_values(self):
results = self.parser.parse_args([ self.assertRaises(
argparse.ArgumentTypeError,
self.parser.parse_args,
[
'--property', 'red', '--property', 'red',
'--property', 'green=100%', ]
'--property', 'blue', )
])
actual = getattr(results, 'property', {})
# There should be no red or blue
expect = {'green': '100%', 'format': '#rgb'}
self.assertDictEqual(expect, actual)
class TestMultiKeyValueAction(utils.TestCase): class TestMultiKeyValueAction(utils.TestCase):

View File

@ -0,0 +1,5 @@
---
fixes:
- Raise ``ArgumentTypeError`` if the input arguments do not match the type
``key=value`` when we set properties.
[Bug `1589935 <https://bugs.launchpad.net/bugs/1589935>`_]