diff options
author | Matt Riedemann <mriedem@us.ibm.com> | 2015-10-07 12:46:55 -0700 |
---|---|---|
committer | Matt Riedemann <mriedem@us.ibm.com> | 2015-10-07 13:57:55 -0700 |
commit | f4b09ce6c59498a9892fffe9e91d0b41f863a9de (patch) | |
tree | 784834f34e6488080c2ad71ba53736b1185c4575 | |
parent | 075379920e481949a6ff2c4a8b3a9dbeb080ce56 (diff) | |
download | oslo-incubator-f4b09ce6c59498a9892fffe9e91d0b41f863a9de.tar.gz |
Sort keys in table output in print_dict
We should sort the keys on the print_dict output because (1)
it's prettier and (2) it makes the output deterministic so we
can reliably test it.
Change-Id: Ifaecb249a11705ce38516642469653f091463c7a
Partial-Bug: #1503841
-rw-r--r-- | openstack/common/cliutils.py | 2 | ||||
-rw-r--r-- | tests/unit/test_cliutils.py | 17 |
2 files changed, 18 insertions, 1 deletions
diff --git a/openstack/common/cliutils.py b/openstack/common/cliutils.py index de57f02c..c27a01ba 100644 --- a/openstack/common/cliutils.py +++ b/openstack/common/cliutils.py @@ -196,7 +196,7 @@ def print_dict(dct, dict_property="Property", wrap=0, dict_value='Value'): """ pt = prettytable.PrettyTable([dict_property, dict_value]) pt.align = 'l' - for k, v in six.iteritems(dct): + for k, v in sorted(dct.items()): # convert dict to str to check length if isinstance(v, dict): v = six.text_type(v) diff --git a/tests/unit/test_cliutils.py b/tests/unit/test_cliutils.py index 5a1b8a9e..0fe69fc7 100644 --- a/tests/unit/test_cliutils.py +++ b/tests/unit/test_cliutils.py @@ -609,6 +609,23 @@ class PrintResultStringTestCase(test_base.BaseTestCase): ''' self.assertEqual(expected, out) + def test_print_dict_string_sorted(self): + orig = sys.stdout + sys.stdout = six.StringIO() + cliutils.print_dict({"Foo": "k", "Bar": "Value"}) + out = sys.stdout.getvalue() + sys.stdout.close() + sys.stdout = orig + expected = '''\ ++----------+-------+ +| Property | Value | ++----------+-------+ +| Bar | Value | +| Foo | k | ++----------+-------+ +''' + self.assertEqual(expected, out) + class DecoratorsTestCase(test_base.BaseTestCase): |