generator: format string default value for List type properly

List type allows to pass string values as default values, which are then
handled by splitting on commas. For formatting matters, if the default
value is of string type, there is no special conversion needed, and we
can reuse the value as-is.

Change-Id: Ie9ecdfc064e886f6138b1fa9e839d8f95a7df339
Closes-Bug: #1586366
This commit is contained in:
Ihar Hrachyshka 2016-05-27 14:12:37 +02:00
parent 90e81843dd
commit 75e1c30663
2 changed files with 31 additions and 0 deletions

View File

@ -1005,6 +1005,35 @@ class GeneratorAdditionalTestCase(base.BaseTestCase):
actual = f.read()
self.assertEqual(expected, actual)
def _test_output_default_list_opt_with_string_value(self, default):
opt = cfg.ListOpt('list_opt', help='a list', default=default)
config = [("namespace1", [
("alpha", [opt])])]
groups = generator._get_groups(config)
fd, tmp_file = tempfile.mkstemp()
f = open(tmp_file, 'w+')
formatter = generator._OptFormatter(output_file=f)
expected = '''[alpha]
#
# From namespace1
#
# a list (list value)
#list_opt = %(default)s
''' % {'default': default}
generator._output_opts(formatter, 'alpha', groups.pop('alpha'))
f.close()
content = open(tmp_file).read()
self.assertEqual(expected, content)
def test_output_default_list_opt_with_string_value_multiple_entries(self):
self._test_output_default_list_opt_with_string_value('foo,bar')
def test_output_default_list_opt_with_string_value_single_entry(self):
self._test_output_default_list_opt_with_string_value('foo')
class GeneratorMutableOptionTestCase(base.BaseTestCase):

View File

@ -448,6 +448,8 @@ class List(ConfigType):
)
def _formatter(self, value):
if isinstance(value, six.string_types):
return value
return ','.join(value)