Merge "Expose min and max to IntOpt"

This commit is contained in:
Jenkins 2015-07-09 18:11:55 +00:00 committed by Gerrit Code Review
commit 781868f2da
6 changed files with 43 additions and 2 deletions

@ -1029,8 +1029,9 @@ class IntOpt(Opt):
`Kept for backward-compatibility with options not using Opt directly`.
"""
def __init__(self, name, **kwargs):
super(IntOpt, self).__init__(name, type=types.Integer(), **kwargs)
def __init__(self, name, min=None, max=None, **kwargs):
super(IntOpt, self).__init__(name, type=types.Integer(min, max),
**kwargs)
class FloatOpt(Opt):

@ -120,6 +120,12 @@ class _OptFormatter(object):
help_text = u'(%s)' % opt_type
lines = self._format_help(help_text)
if getattr(opt.type, 'min', None):
lines.append('# Minimum value: %d\n' % opt.type.min)
if getattr(opt.type, 'max', None):
lines.append('# Maximum value: %d\n' % opt.type.max)
if getattr(opt.type, 'choices', None):
choices_text = ', '.join([self._get_choice_text(choice)
for choice in opt.type.choices])

@ -977,6 +977,19 @@ class ConfigFileOptsTestCase(BaseTestCase):
self.assertTrue(hasattr(self.conf, 'foo'))
self.assertEqual(self.conf.foo, 666)
def test_conf_file_int_min_max(self):
self.conf.register_opt(cfg.IntOpt('foo', min=1, max=5))
paths = self.create_tempfiles([('test',
'[DEFAULT]\n'
'foo = 10\n')])
self.conf(['--config-file', paths[0]])
self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo')
def test_conf_file_int_min_greater_max(self):
self.assertRaises(ValueError, cfg.IntOpt, 'foo', min=5, max=1)
def test_conf_file_int_use_dname(self):
self._do_dname_test_use(cfg.IntOpt, '66', 66)

@ -79,6 +79,8 @@ class GeneratorTestCase(base.BaseTestCase):
help='a boolean'),
'int_opt': cfg.IntOpt('int_opt',
default=10,
min=1,
max=20,
help='an integer'),
'float_opt': cfg.FloatOpt('float_opt',
default=0.1,
@ -400,6 +402,8 @@ class GeneratorTestCase(base.BaseTestCase):
#
# an integer (integer value)
# Minimum value: 1
# Maximum value: 20
#int_opt = 10
''')),
('float_opt',

@ -982,6 +982,19 @@ class ConfigFileOptsTestCase(BaseTestCase):
self.assertTrue(hasattr(self.conf, 'foo'))
self.assertEqual(self.conf.foo, 666)
def test_conf_file_int_min_max(self):
self.conf.register_opt(cfg.IntOpt('foo', min=1, max=5))
paths = self.create_tempfiles([('test',
'[DEFAULT]\n'
'foo = 10\n')])
self.conf(['--config-file', paths[0]])
self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo')
def test_conf_file_int_min_greater_max(self):
self.assertRaises(ValueError, cfg.IntOpt, 'foo', min=5, max=1)
def test_conf_file_int_use_dname(self):
self._do_dname_test_use(cfg.IntOpt, '66', 66)

@ -81,6 +81,8 @@ class GeneratorTestCase(base.BaseTestCase):
help='a boolean'),
'int_opt': cfg.IntOpt('int_opt',
default=10,
min=1,
max=20,
help='an integer'),
'float_opt': cfg.FloatOpt('float_opt',
default=0.1,
@ -413,6 +415,8 @@ class GeneratorTestCase(base.BaseTestCase):
#
# an integer (integer value)
# Minimum value: 1
# Maximum value: 20
#int_opt = 10
''')),
('float_opt',