Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression in table outputter due to unicode types #47674

Merged
merged 3 commits into from
May 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions salt/output/table_out.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
# Import Salt libs
import salt.output
import salt.utils.color
import salt.utils.data
import salt.utils.locales

# Import 3rd-party libs
Expand All @@ -62,9 +63,9 @@ class TableDisplay(object):
'''

_JUSTIFY_MAP = {
'center': str.center,
'right': str.rjust,
'left': str.ljust
'center': six.text_type.center,
'right': six.text_type.rjust,
'left': six.text_type.ljust
}

def __init__(self,
Expand Down Expand Up @@ -147,7 +148,7 @@ def row_wrapper(row):
for item in row
]
rows = []
for item in map(None, *new_rows):
for item in map(lambda *args: args, *new_rows):
if isinstance(item, (tuple, list)):
rows.append([substr or '' for substr in item])
else:
Expand All @@ -159,7 +160,7 @@ def row_wrapper(row):
for row in rows
]

columns = map(None, *reduce(operator.add, logical_rows))
columns = map(lambda *args: args, *reduce(operator.add, logical_rows))

max_widths = [
max([len(six.text_type(item)) for item in column])
Expand Down Expand Up @@ -363,7 +364,7 @@ def output(ret, **kwargs):
)
)

return '\n'.join(table.display(ret,
return '\n'.join(table.display(salt.utils.data.decode(ret),
base_indent,
out,
rows_key=rows_key,
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/output/test_table_out.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
'''
unittests for table outputter
'''

# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals

# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase

# Import Salt Libs
import salt.output.table_out as table_out
import salt.utils.stringutils


class TableTestCase(TestCase, LoaderModuleMockMixin):
'''
Test cases for salt.output.table_out
'''
def setup_loader_modules(self):
return {table_out: {}}

# The test data should include unicode chars, and in Python 2 there should
# be an example both of an encoded str type and an actual unicode type.
# Since unicode_literals is imported, we will achieve the former using
# salt.utils.stringutils.to_str and the latter by simply using a string
# literal.
data = [
{'Food': salt.utils.stringutils.to_str('яйца, бекон, колбаса и спам'),
'Price': 5.99},
{'Food': 'спам, спам, спам, яйца и спам',
'Price': 3.99},
]

def test_output(self):
ret = table_out.output(self.data)
self.assertEqual(
ret,
(' -----------------------------------------\n'
' | Food | Price |\n'
' -----------------------------------------\n'
' | яйца, бекон, колбаса и спам | 5.99 |\n'
' -----------------------------------------\n'
' | спам, спам, спам, яйца и спам | 3.99 |\n'
' -----------------------------------------')
)