Merge "generator: format string default value for List type properly"

This commit is contained in:
Jenkins 2016-06-02 14:47:31 +00:00 committed by Gerrit Code Review
commit bb0f7e3880
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)