Encode the text before print it to console
Closes-Bug: #1250490 Change-Id: I516806a0f8a136bb66c64dcdcd07cee6d297e619
This commit is contained in:
@@ -12,6 +12,11 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import mock
|
||||||
|
import six
|
||||||
|
|
||||||
from keystoneclient import exceptions
|
from keystoneclient import exceptions
|
||||||
from keystoneclient.tests import utils as test_utils
|
from keystoneclient.tests import utils as test_utils
|
||||||
from keystoneclient import utils
|
from keystoneclient import utils
|
||||||
@@ -91,3 +96,49 @@ class FindResourceTestCase(test_utils.TestCase):
|
|||||||
utils.find_resource,
|
utils.find_resource,
|
||||||
self.manager,
|
self.manager,
|
||||||
9999)
|
9999)
|
||||||
|
|
||||||
|
|
||||||
|
class FakeObject(object):
|
||||||
|
def __init__(self, name):
|
||||||
|
self.name = name
|
||||||
|
|
||||||
|
|
||||||
|
class PrintTestCase(test_utils.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
super(PrintTestCase, self).setUp()
|
||||||
|
self.old_stdout = sys.stdout
|
||||||
|
self.stdout = six.moves.cStringIO()
|
||||||
|
sys.stdout = self.stdout
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
super(PrintTestCase, self).tearDown()
|
||||||
|
sys.stdout = self.old_stdout
|
||||||
|
self.stdout = None
|
||||||
|
|
||||||
|
def test_print_list_unicode(self):
|
||||||
|
name = u'\u540d\u5b57'
|
||||||
|
objs = [FakeObject(name)]
|
||||||
|
# NOTE(Jeffrey4l) If the text's encode is proper, this method will not
|
||||||
|
# raise UnicodeEncodeError exceptions
|
||||||
|
utils.print_list(objs, ['name'])
|
||||||
|
self.assertIn(name, self.stdout.getvalue().decode('utf8'))
|
||||||
|
|
||||||
|
@mock.patch('keystoneclient.openstack.common.strutils.safe_encode')
|
||||||
|
def test_print_list_unicode_without_encode(self, safe_encode_mock):
|
||||||
|
safe_encode_mock.side_effect = lambda x, *args, **kwargs: x
|
||||||
|
|
||||||
|
name = u'\u540d\u5b57'
|
||||||
|
objs = [FakeObject(name)]
|
||||||
|
self.assertRaises(UnicodeEncodeError, utils.print_list, objs, ['name'])
|
||||||
|
|
||||||
|
def test_print_dict_unicode(self):
|
||||||
|
name = u'\u540d\u5b57'
|
||||||
|
utils.print_dict({'name': name})
|
||||||
|
self.assertIn(name, self.stdout.getvalue().decode('utf8'))
|
||||||
|
|
||||||
|
@mock.patch('keystoneclient.openstack.common.strutils.safe_encode')
|
||||||
|
def test_print_dict_unicode_without_encode(self, safe_encode_mock):
|
||||||
|
safe_encode_mock.side_effect = lambda x, *args, **kwargs: x
|
||||||
|
|
||||||
|
name = u'\u540d\u5b57'
|
||||||
|
self.assertRaises(UnicodeEncodeError, utils.print_dict, {'name': name})
|
||||||
|
@@ -20,6 +20,7 @@ import prettytable
|
|||||||
import six
|
import six
|
||||||
|
|
||||||
from keystoneclient import exceptions
|
from keystoneclient import exceptions
|
||||||
|
from keystoneclient.openstack.common import strutils
|
||||||
|
|
||||||
|
|
||||||
# Decorator for cli-args
|
# Decorator for cli-args
|
||||||
@@ -56,7 +57,7 @@ def print_list(objs, fields, formatters={}, order_by=None):
|
|||||||
|
|
||||||
if order_by is None:
|
if order_by is None:
|
||||||
order_by = fields[0]
|
order_by = fields[0]
|
||||||
print(pt.get_string(sortby=order_by))
|
print(strutils.safe_encode(pt.get_string(sortby=order_by)))
|
||||||
|
|
||||||
|
|
||||||
def _word_wrap(string, max_length=0):
|
def _word_wrap(string, max_length=0):
|
||||||
@@ -80,7 +81,7 @@ def print_dict(d, wrap=0):
|
|||||||
value = ''
|
value = ''
|
||||||
value = _word_wrap(value, max_length=wrap)
|
value = _word_wrap(value, max_length=wrap)
|
||||||
pt.add_row([prop, value])
|
pt.add_row([prop, value])
|
||||||
print(pt.get_string(sortby='Property'))
|
print(strutils.safe_encode(pt.get_string(sortby='Property')))
|
||||||
|
|
||||||
|
|
||||||
def find_resource(manager, name_or_id):
|
def find_resource(manager, name_or_id):
|
||||||
|
Reference in New Issue
Block a user