diff --git a/novaclient/utils.py b/novaclient/utils.py index bf76a7316..d65afd481 100644 --- a/novaclient/utils.py +++ b/novaclient/utils.py @@ -161,7 +161,10 @@ def print_list(objs, fields, formatters={}, sortby_index=0): row.append(data) pt.add_row(row) - print pt.get_string(sortby=sortby) + if sortby is not None: + print pt.get_string(sortby=sortby) + else: + print pt.get_string() def print_dict(d, dict_property="Property"): diff --git a/tests/test_utils.py b/tests/test_utils.py index c619fe14d..7a0949ed2 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,3 +1,7 @@ +import StringIO +import sys + +import mock from novaclient import exceptions from novaclient import utils @@ -99,3 +103,63 @@ class FindResourceTestCase(test_utils.TestCase): display_manager = FakeDisplayManager(None) output = utils.find_resource(display_manager, 'entity_three') self.assertEqual(output, display_manager.get('4242')) + + +class _FakeResult(object): + def __init__(self, name, value): + self.name = name + self.value = value + + +class PrintResultTestCase(test_utils.TestCase): + @mock.patch('sys.stdout', StringIO.StringIO()) + def test_print_list_sort_by_str(self): + objs = [_FakeResult("k1", 1), + _FakeResult("k3", 2), + _FakeResult("k2", 3)] + + utils.print_list(objs, ["Name", "Value"], sortby_index=0) + + self.assertEqual(sys.stdout.getvalue(), + '+------+-------+\n' + '| Name | Value |\n' + '+------+-------+\n' + '| k1 | 1 |\n' + '| k2 | 3 |\n' + '| k3 | 2 |\n' + '+------+-------+\n') + + @mock.patch('sys.stdout', StringIO.StringIO()) + def test_print_list_sort_by_integer(self): + objs = [_FakeResult("k1", 1), + _FakeResult("k3", 2), + _FakeResult("k2", 3)] + + utils.print_list(objs, ["Name", "Value"], sortby_index=1) + + self.assertEqual(sys.stdout.getvalue(), + '+------+-------+\n' + '| Name | Value |\n' + '+------+-------+\n' + '| k1 | 1 |\n' + '| k3 | 2 |\n' + '| k2 | 3 |\n' + '+------+-------+\n') + + # without sorting + @mock.patch('sys.stdout', StringIO.StringIO()) + def test_print_list_sort_by_none(self): + objs = [_FakeResult("k1", 1), + _FakeResult("k3", 3), + _FakeResult("k2", 2)] + + utils.print_list(objs, ["Name", "Value"], sortby_index=None) + + self.assertEqual(sys.stdout.getvalue(), + '+------+-------+\n' + '| Name | Value |\n' + '+------+-------+\n' + '| k1 | 1 |\n' + '| k3 | 3 |\n' + '| k2 | 2 |\n' + '+------+-------+\n')