From 47d7c1c090fdbc0dabd95b043a82eabdc201dead Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Tue, 9 May 2017 18:30:05 +0200 Subject: [PATCH] Handle multi-line default values in olso-config-generator The oslo.config file format permits multi-line values for options, however all the lines except for the first one need to be indented. Right now oslo-config-generator fails to take that into account, and instead outputs the formatted value as is, adding a comment character only to the first line, and not commenting out or indenting the subsequent lines. This patch tries to fix that by adding a comment character and some indentation to every newline in the formatted value. Change-Id: I33381c8cfb59901ce6dfc07bd662f2edb56531dd Closes-bug: #1689594 --- oslo_config/generator.py | 2 +- oslo_config/tests/test_generator.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/oslo_config/generator.py b/oslo_config/generator.py index 119fd95..d38ba7f 100644 --- a/oslo_config/generator.py +++ b/oslo_config/generator.py @@ -311,7 +311,7 @@ class _OptFormatter(object): defaults = _format_defaults(opt) for default_str in defaults: if default_str: - default_str = ' ' + default_str + default_str = ' ' + default_str.replace('\n', '\n# ') if minimal: lines.append('%s =%s\n' % (opt.dest, default_str)) else: diff --git a/oslo_config/tests/test_generator.py b/oslo_config/tests/test_generator.py index 71dc9be..3406b63 100644 --- a/oslo_config/tests/test_generator.py +++ b/oslo_config/tests/test_generator.py @@ -122,6 +122,9 @@ class GeneratorTestCase(base.BaseTestCase): 'str_opt_with_space': cfg.StrOpt('str_opt', default=' foo bar ', help='a string with spaces'), + 'str_opt_multiline': cfg.StrOpt('str_opt', + default='foo\nbar\nbaz', + help='a string with newlines'), 'bool_opt': cfg.BoolOpt('bool_opt', default=False, help='a boolean'), @@ -528,6 +531,19 @@ class GeneratorTestCase(base.BaseTestCase): # a string with spaces (string value) #str_opt = " foo bar " +''')), + ('str_opt_multiline', + dict(opts=[('test', [(None, [opts['str_opt_multiline']])])], + expected='''[DEFAULT] + +# +# From test +# + +# a string with newlines (string value) +#str_opt = foo +# bar +# baz ''')), ('bool_opt', dict(opts=[('test', [(None, [opts['bool_opt']])])],