diff --git a/oslo_config/generator.py b/oslo_config/generator.py index 749a556b..ef992fdd 100644 --- a/oslo_config/generator.py +++ b/oslo_config/generator.py @@ -177,6 +177,13 @@ class _OptFormatter(object): lines = ['# ' + help_text + '\n'] return lines + def _get_choice_text(self, choice): + if choice is None: + return '' + elif choice == '': + return "''" + return six.text_type(choice) + def format(self, opt): """Format a description of an option to the output file. @@ -195,8 +202,7 @@ class _OptFormatter(object): lines = self._format_help(help_text) if getattr(opt.type, 'choices', None): - choices_text = ', '.join([six.text_type(choice) - if choice else '' + choices_text = ', '.join([self._get_choice_text(choice) for choice in opt.type.choices]) lines.append('# Allowed values: %s\n' % choices_text) diff --git a/oslo_config/tests/test_cfg.py b/oslo_config/tests/test_cfg.py index 6accd82a..5518f538 100644 --- a/oslo_config/tests/test_cfg.py +++ b/oslo_config/tests/test_cfg.py @@ -3479,6 +3479,29 @@ class ChoicesTestCase(BaseTestCase): self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(self.conf.foo, 'bar1') + def test_conf_file_choice_empty_value(self): + self.conf.register_opt(cfg.StrOpt('foo', + choices=['', 'bar1', 'bar2'])) + + paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = \n')]) + + self.conf(['--config-file', paths[0]]) + + self.assertTrue(hasattr(self.conf, 'foo')) + self.assertEqual(self.conf.foo, '') + + def test_conf_file_choice_none_value(self): + self.conf.register_opt(cfg.StrOpt('foo', + default=None, + choices=[None, 'bar1', 'bar2'])) + + paths = self.create_tempfiles([('test', '[DEFAULT]\n''\n')]) + + self.conf(['--config-file', paths[0]]) + + self.assertTrue(hasattr(self.conf, 'foo')) + self.assertEqual(self.conf.foo, None) + def test_conf_file_bad_choice_value(self): self.conf.register_opt(cfg.StrOpt('foo', choices=['bar1', 'bar2'])) diff --git a/oslo_config/tests/test_generator.py b/oslo_config/tests/test_generator.py index 1c30ca3e..144a4f81 100644 --- a/oslo_config/tests/test_generator.py +++ b/oslo_config/tests/test_generator.py @@ -50,7 +50,7 @@ class GeneratorTestCase(base.BaseTestCase): 'laborum.'), 'choices_opt': cfg.StrOpt('choices_opt', default='a', - choices=(None, 'a', 'b', 'c'), + choices=(None, '', 'a', 'b', 'c'), help='a string with choices'), 'deprecated_opt': cfg.StrOpt('bar', deprecated_name='foobar', @@ -294,7 +294,7 @@ class GeneratorTestCase(base.BaseTestCase): # # a string with choices (string value) -# Allowed values: , a, b, c +# Allowed values: , '', a, b, c #choices_opt = a ''')), ('deprecated', diff --git a/tests/test_generator.py b/tests/test_generator.py index 3db32c98..d020233a 100644 --- a/tests/test_generator.py +++ b/tests/test_generator.py @@ -54,7 +54,7 @@ class GeneratorTestCase(base.BaseTestCase): 'laborum.'), 'choices_opt': cfg.StrOpt('choices_opt', default='a', - choices=(None, 'a', 'b', 'c'), + choices=(None, '', 'a', 'b', 'c'), help='a string with choices'), 'deprecated_opt': cfg.StrOpt('bar', deprecated_name='foobar', @@ -309,7 +309,7 @@ class GeneratorTestCase(base.BaseTestCase): # # a string with choices (string value) -# Allowed values: , a, b, c +# Allowed values: , '', a, b, c #choices_opt = a ''')), ('deprecated',