From 75e1c306630378673861d52a34de51e869183062 Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Fri, 27 May 2016 14:12:37 +0200 Subject: [PATCH] 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 --- oslo_config/tests/test_generator.py | 29 +++++++++++++++++++++++++++++ oslo_config/types.py | 2 ++ 2 files changed, 31 insertions(+) diff --git a/oslo_config/tests/test_generator.py b/oslo_config/tests/test_generator.py index 06c2ace4..5eb46ed3 100644 --- a/oslo_config/tests/test_generator.py +++ b/oslo_config/tests/test_generator.py @@ -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): diff --git a/oslo_config/types.py b/oslo_config/types.py index a255088c..6ba09415 100644 --- a/oslo_config/types.py +++ b/oslo_config/types.py @@ -448,6 +448,8 @@ class List(ConfigType): ) def _formatter(self, value): + if isinstance(value, six.string_types): + return value return ','.join(value)