Bug 1257326 - Add a parameter to restrict accepted origins for an option in python configure. r=glandium

MozReview-Commit-ID: 4Cme7fvl1fN
This commit is contained in:
Chris Manchester 2016-05-12 11:55:58 -07:00
Родитель 2bc5c65931
Коммит a0e5625c9a
3 изменённых файлов: 42 добавлений и 1 удалений

Просмотреть файл

@ -16,6 +16,11 @@ class HelpFormatter(object):
def add(self, option):
assert isinstance(option, Option)
if option.possible_origins == ('implied',):
# Don't display help if our option can only be implied.
return
# TODO: improve formatting
target = self.options if option.name else self.env
opt = option.option

Просмотреть файл

@ -117,13 +117,16 @@ class Option(object):
or '--without-', the implied default is a NegativeOptionValue.
- `choices` restricts the set of values that can be given to the option.
- `help` is the option description for use in the --help output.
- `possible_origins` is a tuple of strings that are origins accepted for
this option. Example origins are 'mozconfig', 'implied', and 'environment'.
'''
__slots__ = (
'id', 'prefix', 'name', 'env', 'nargs', 'default', 'choices', 'help',
'possible_origins',
)
def __init__(self, name=None, env=None, nargs=None, default=None,
choices=None, help=None):
possible_origins=None, choices=None, help=None):
if not name and not env:
raise InvalidOptionError(
'At least an option name or an environment variable name must '
@ -158,6 +161,10 @@ class Option(object):
'choices must be a tuple of strings')
if not help:
raise InvalidOptionError('A help string must be provided')
if possible_origins and not istupleofstrings(possible_origins):
raise InvalidOptionError(
'possible_origins must be a tuple of strings')
self.possible_origins = possible_origins
if name:
prefix, name, values = self.split_option(name)
@ -300,6 +307,11 @@ class Option(object):
if not option:
return self.default
if self.possible_origins and origin not in self.possible_origins:
raise InvalidOptionError(
'%s can not be set by %s. Values are accepted from: %s' %
(option, origin, ', '.join(self.possible_origins)))
prefix, name, values = self.split_option(option)
option = self._join_option(prefix, name)

Просмотреть файл

@ -785,6 +785,30 @@ class TestCommandLineHelper(unittest.TestCase):
self.assertEqual('--with-foo=a,b', cm.exception.old_arg)
self.assertEqual('command-line', cm.exception.old_origin)
def test_possible_origins(self):
with self.assertRaises(InvalidOptionError):
Option('--foo', possible_origins='command-line')
helper = CommandLineHelper({'BAZ': '1'}, ['cmd', '--foo', '--bar'])
foo = Option('--foo',
possible_origins=('command-line',))
value, option = helper.handle(foo)
self.assertEquals(PositiveOptionValue(), value)
self.assertEquals('command-line', value.origin)
self.assertEquals('--foo', option)
bar = Option('--bar',
possible_origins=('mozconfig',))
with self.assertRaisesRegexp(InvalidOptionError,
"--bar can not be set by command-line. Values are accepted from: mozconfig"):
helper.handle(bar)
baz = Option(env='BAZ',
possible_origins=('implied',))
with self.assertRaisesRegexp(InvalidOptionError,
"BAZ=1 can not be set by environment. Values are accepted from: implied"):
helper.handle(baz)
if __name__ == '__main__':
main()