Merge "Allow generator as input to utils.print_list."

This commit is contained in:
Jenkins 2013-05-16 18:06:14 +00:00 committed by Gerrit Code Review
commit 8044dc7478
2 changed files with 52 additions and 1 deletions
cinderclient
tests

@ -158,7 +158,7 @@ def print_list(objs, fields, formatters={}):
row.append(data)
pt.add_row(row)
if len(objs) > 0:
if len(pt._rows) > 0:
print strutils.safe_encode(pt.get_string(sortby=fields[0]))

@ -1,3 +1,6 @@
import collections
import StringIO
import sys
from cinderclient import exceptions
from cinderclient import utils
@ -73,3 +76,51 @@ class FindResourceTestCase(test_utils.TestCase):
def test_find_by_str_displayname(self):
output = utils.find_resource(self.manager, 'entity_three')
self.assertEqual(output, self.manager.get('4242'))
class CaptureStdout(object):
"""Context manager for capturing stdout from statments in its's block"""
def __enter__(self):
self.real_stdout = sys.stdout
self.stringio = StringIO.StringIO()
sys.stdout = self.stringio
return self
def __exit__(self, *args):
sys.stdout = self.real_stdout
self.stringio.seek(0)
self.read = self.stringio.read
class PrintListTestCase(test_utils.TestCase):
def test_print_list_with_list(self):
Row = collections.namedtuple('Row', ['a', 'b'])
to_print = [Row(a=1, b=2), Row(a=3, b=4)]
with CaptureStdout() as cso:
utils.print_list(to_print, ['a', 'b'])
self.assertEqual(cso.read(), """\
+---+---+
| a | b |
+---+---+
| 1 | 2 |
| 3 | 4 |
+---+---+
""")
def test_print_list_with_generator(self):
Row = collections.namedtuple('Row', ['a', 'b'])
def gen_rows():
for row in [Row(a=1, b=2), Row(a=3, b=4)]:
yield row
with CaptureStdout() as cso:
utils.print_list(gen_rows(), ['a', 'b'])
self.assertEqual(cso.read(), """\
+---+---+
| a | b |
+---+---+
| 1 | 2 |
| 3 | 4 |
+---+---+
""")