Skip to content

Commit cf12239

Browse files
author
Huanxuan Ao
committed
Error handling for KeyValueAction class.
The set --property command requires that the input match the "key=value" type, but if the type don't match, the return value will be None, and the command still can be implemented successfully, this may confuse the users. I think we should raise exception if the argument type don't match "key=value". So I make some changes in KeyValueAction class in this patch. Change-Id: I14e64922faa7e083bc8b5e7e1cac41ef8117c224 Closes-Bug: #1589935
1 parent 5293bb1 commit cf12239

3 files changed

Lines changed: 15 additions & 11 deletions

File tree

openstackclient/common/parseractions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ def __call__(self, parser, namespace, values, option_string=None):
3535
if '=' in values:
3636
getattr(namespace, self.dest, {}).update([values.split('=', 1)])
3737
else:
38-
getattr(namespace, self.dest, {}).pop(values, None)
38+
msg = _("Expected 'key=value' type, "
39+
"but got: %s") % (str(values))
40+
raise argparse.ArgumentTypeError(msg)
3941

4042

4143
class MultiKeyValueAction(argparse.Action):

openstackclient/tests/common/test_parseractions.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,13 @@ def test_good_values(self):
4949
self.assertDictEqual(expect, actual)
5050

5151
def test_error_values(self):
52-
results = self.parser.parse_args([
53-
'--property', 'red',
54-
'--property', 'green=100%',
55-
'--property', 'blue',
56-
])
57-
58-
actual = getattr(results, 'property', {})
59-
# There should be no red or blue
60-
expect = {'green': '100%', 'format': '#rgb'}
61-
self.assertDictEqual(expect, actual)
52+
self.assertRaises(
53+
argparse.ArgumentTypeError,
54+
self.parser.parse_args,
55+
[
56+
'--property', 'red',
57+
]
58+
)
6259

6360

6461
class TestMultiKeyValueAction(utils.TestCase):
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- Raise ``ArgumentTypeError`` if the input arguments do not match the type
4+
``key=value`` when we set properties.
5+
[Bug `1589935 <https://bugs.launchpad.net/bugs/1589935>`_]

0 commit comments

Comments
 (0)