Merge "Avoid 'NoneType' error when format conversion"

changes/84/440084/5
Jenkins 6 years ago committed by Gerrit Code Review
commit 62a17ed479
  1. 3
      osc_lib/tests/test_utils.py
  2. 7
      osc_lib/utils.py

@ -498,11 +498,13 @@ class TestFindResource(test_utils.TestCase):
utils.format_dict({'a': 'b', 'c': 'd', 'e': 'f'}))
self.assertEqual(expected,
utils.format_dict({'e': 'f', 'c': 'd', 'a': 'b'}))
self.assertEqual(None, utils.format_dict(None))
def test_format_list(self):
expected = 'a, b, c'
self.assertEqual(expected, utils.format_list(['a', 'b', 'c']))
self.assertEqual(expected, utils.format_list(['c', 'b', 'a']))
self.assertEqual(None, utils.format_list(None))
def test_format_list_of_dicts(self):
expected = "a='b', c='d'\ne='f'"
@ -512,6 +514,7 @@ class TestFindResource(test_utils.TestCase):
self.assertEqual(expected, utils.format_list_of_dicts(unsorted_data))
self.assertEqual('', utils.format_list_of_dicts([]))
self.assertEqual('', utils.format_list_of_dicts([{}]))
self.assertEqual(None, utils.format_list_of_dicts(None))
def test_format_list_separator(self):
expected = 'a\nb\nc'

@ -192,6 +192,9 @@ def format_dict(data):
:rtype: a string formatted to key='value'
"""
if data is None:
return None
output = ""
for s in sorted(data):
output = output + s + "='" + six.text_type(data[s]) + "', "
@ -205,6 +208,8 @@ def format_list(data, separator=', '):
:param separator: the separator to use between strings (default: ', ')
:rtype: a string formatted based on separator
"""
if data is None:
return None
return separator.join(sorted(data))
@ -215,6 +220,8 @@ def format_list_of_dicts(data):
:param data: a list of dicts
:rtype: a string formatted to key='value' with dicts separated by new line
"""
if data is None:
return None
return '\n'.join(format_dict(i) for i in data)

Loading…
Cancel
Save