Fix wrong check with non-None value when format group

This patch is same to the bug-fix 'Fix can't generate config sample
with non-None value'[1].

There is pitfall in statement like:
  if getattr(opt.type, 'min', None)
The check returns False When opt.type.min equals 0,
We should check this with None.

This patch will fix it and add unit tests for the change.

[1]https://review.openstack.org/#/c/267566/2

Change-Id: Iec6d8d70c67f641ae53976bde13697ce0f4982c1
This commit is contained in:
Chaozhe.Chen 2016-02-16 15:56:34 +08:00
parent 1482e5c601
commit 3dfd4a4e65
2 changed files with 46 additions and 2 deletions

View File

@ -108,9 +108,9 @@ def _format_group(app, namespace, group_name, group_obj, opt_list):
if default:
default = '``' + default + '``'
yield _indent(':Default: %s' % default)
if getattr(opt.type, 'min', None):
if getattr(opt.type, 'min', None) is not None:
yield _indent(':Minimum Value: %s' % opt.type.min)
if getattr(opt.type, 'max', None):
if getattr(opt.type, 'max', None) is not None:
yield _indent(':Maximum Value: %s' % opt.type.max)
if getattr(opt.type, 'choices', None):
choices_text = ', '.join([_get_choice_text(choice)

View File

@ -91,6 +91,28 @@ class FormatGroupTest(base.BaseTestCase):
''').lstrip(), results)
def test_with_min_0(self):
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name=None,
group_obj=None,
opt_list=[
cfg.IntOpt('opt_name',
min=0),
],
)))
self.assertEqual(textwrap.dedent('''
.. oslo.config:group:: DEFAULT
.. oslo.config:option:: opt_name
:Type: integer
:Default: ``<None>``
:Minimum Value: 0
''').lstrip(), results)
def test_with_max(self):
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
@ -113,6 +135,28 @@ class FormatGroupTest(base.BaseTestCase):
''').lstrip(), results)
def test_with_max_0(self):
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name=None,
group_obj=None,
opt_list=[
cfg.IntOpt('opt_name',
max=0),
],
)))
self.assertEqual(textwrap.dedent('''
.. oslo.config:group:: DEFAULT
.. oslo.config:option:: opt_name
:Type: integer
:Default: ``<None>``
:Maximum Value: 0
''').lstrip(), results)
def test_with_choices(self):
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),