From 9dd6ac634842b037c99d25b34431eaa2f4ed427c Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Fri, 27 May 2016 14:12:37 +0200 Subject: 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 (cherry picked from commit 75e1c306630378673861d52a34de51e869183062) --- 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 72ae1d3..4ea7100 100644 --- a/oslo_config/tests/test_generator.py +++ b/oslo_config/tests/test_generator.py @@ -939,6 +939,35 @@ class GeneratorAdditionalTestCase(base.BaseTestCase): content = open(tmp_file).read() self.assertEqual(expected, content) + 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 2464c5c..71d7817 100644 --- a/oslo_config/types.py +++ b/oslo_config/types.py @@ -447,6 +447,8 @@ class List(ConfigType): ) def _formatter(self, value): + if isinstance(value, six.string_types): + return value return ','.join(value) -- cgit v1.2.1