Merge "Fix output of "tuskar plan-list --verbose""

This commit is contained in:
Jenkins 2015-07-02 16:13:35 +00:00 committed by Gerrit Code Review
commit 9e051c620a
2 changed files with 56 additions and 1 deletions

View File

@ -12,15 +12,43 @@
from __future__ import print_function
import pprint
import sys
import textwrap
import prettytable
import six
def value_formatter(value, width=70):
# Most values can be pretty printed for a reasonable output
if not isinstance(value, six.string_types):
return pprint.pformat(value, width=width)
# pprint doesn't touch strings, so we do them manually
# First join lines that are not indented:
joined = []
parts = []
for l in value.splitlines():
if l[0] in u' \t\r\n':
# break here
joined.append(' '.join(parts))
parts = []
parts.append(l.rstrip())
if parts:
joined.append(' '.join(parts))
result = []
for line in joined:
result.append(textwrap.fill(line, width=width))
return u"\n".join(result)
def attributes_formatter(attributes):
"""Given a simple dict format the keyvalue pairs with one on each line.
"""
return u"".join(u"{0}={1}\n".format(k, v) for k, v in
return u"".join(u"{0}={1}\n".format(k, value_formatter(v)) for k, v in
sorted(attributes.items()))

View File

@ -271,6 +271,33 @@ class PlansShellTest(BasePlansShellTest):
self.shell.do_plan_show(self.tuskar, args, outfile=self.outfile)
mock_find_resource.assert_called_with(self.tuskar.plans, '5')
@mock.patch('tuskarclient.common.utils.find_resource')
def test_print_plan_wrap(self, mock_find_resource):
mock_find_resource.return_value = mock_plan()
mock_find_resource.return_value.parameters.append(
{'name': 'foo',
'value': 'This is a really long parameter value with '
'multiple lines to test the output wrapping.\n'
'Indents is assumed to be code:\n'
' {\n'
' "like": "this"\n'
' }\n'}
)
args = empty_args()
args.plan = '5'
args.verbose = True
self.shell.do_plan_show(self.tuskar, args, outfile=self.outfile)
output = self.outfile.getvalue()
# Lines should not be way to long:
self.assertTrue(all(len(line) < 100 for line in output.splitlines()))
# The lines are rewraped:
self.assertIn("wrapping. Indents", output)
# But not if the start with an indent:
self.assertIn(" {", output)
@mock.patch('tuskarclient.common.utils.find_resource')
def test_print_plan_detail(self, mock_find_resource):
mock_find_resource.return_value = mock_plan()