Allow multiline descriptions for RuleDefaults

Multiline descriptions are not displayed properly in the generated
sample file because of the wrapper used.

This patch changes the formatting of help text by wrapping each
line.

Change-Id: Ic19a06189caa0002458881c543c63c7540e301de
This commit is contained in:
Sujitha 2017-03-03 18:56:06 +00:00
parent 3c3261c072
commit 8ca44b38bd
2 changed files with 46 additions and 4 deletions

View File

@ -85,10 +85,19 @@ def _format_help_text(description):
if not description:
return '#'
return textwrap.fill(description, 70, initial_indent='# ',
subsequent_indent='# ',
break_long_words=False,
replace_whitespace=False)
lines = description.splitlines()
formatted_lines = []
# wrap each line to support multi line descriptions
for line in lines:
if not line:
formatted_lines.append('#')
else:
formatted_lines.append(textwrap.fill(line, 70,
initial_indent='# ',
subsequent_indent='# ',
break_long_words=False,
replace_whitespace=False))
return "\n".join(formatted_lines)
def _format_rule_default_yaml(default, include_help=True):

View File

@ -141,6 +141,39 @@ class GenerateSampleTestCase(base.PolicyBaseTestCase):
self.assertEqual(expected, stdout.getvalue())
def test_empty_line_formatting(self):
rule = [policy.RuleDefault('admin', 'is_admin:True',
description='Check Summary \n'
'\n'
'This is a description to '
'check that empty line has '
'no white spaces.')]
ext = stevedore.extension.Extension(name='check_rule',
entry_point=None,
plugin=None, obj=rule)
test_mgr = stevedore.named.NamedExtensionManager.make_test_instance(
extensions=[ext], namespace=['check_rule'])
# no whitespace on empty line
expected = '''# Check Summary
#
# This is a description to check that empty line has no white spaces.
"admin": "is_admin:True"
'''
output_file = self.get_config_file_fullname('policy.yaml')
with mock.patch('stevedore.named.NamedExtensionManager',
return_value=test_mgr) as mock_ext_mgr:
generator._generate_sample(['check_rule'], output_file)
mock_ext_mgr.assert_called_once_with(
'oslo.policy.policies', names=['check_rule'],
on_load_failure_callback=generator.on_load_failure_callback,
invoke_on_load=True)
with open(output_file, 'r') as written_file:
written_policy = written_file.read()
self.assertEqual(expected, written_policy)
class GeneratorRaiseErrorTestCase(testtools.TestCase):
def test_generator_raises_error(self):