# HG changeset patch # User Boris Feld # Date 2017-07-12 21:36:10 # Node ID d74141ccfd8baf382fbdd1ca6aa53b2f6b414f04 # Parent ab493a8353d129af3d09e3bf9a37c740636ee217 configitems: handle case were the default value is not static In some case, the default of one value is derived from other value. We add a way to register them anyway and an associated devel-warning. The registration is very naive for the moment. We might be able to have a better way for registering each of these cases but it could be done later. diff --git a/mercurial/configitems.py b/mercurial/configitems.py --- a/mercurial/configitems.py +++ b/mercurial/configitems.py @@ -51,6 +51,9 @@ def _register(configtable, *args, **kwar raise error.ProgrammingError(msg % (item.section, item.name)) section[item.name] = item +# special value for case where the default is derived from other values +dynamicdefault = object() + # Registering actual config items def getitemregister(configtable): diff --git a/mercurial/ui.py b/mercurial/ui.py --- a/mercurial/ui.py +++ b/mercurial/ui.py @@ -457,11 +457,17 @@ class ui(object): if default is _unset: if item is None: value = default + elif item.default is configitems.dynamicdefault: + value = None + msg = "config item requires an explicit default value: '%s.%s'" + msg %= (section, name) + self.develwarn(msg, 2, 'warn-config-default') elif callable(item.default): value = item.default() else: value = item.default - elif item is not None: + elif (item is not None + and item.default is not configitems.dynamicdefault): msg = ("specifying a default value for a registered " "config item: '%s.%s' '%s'") msg %= (section, name, default) diff --git a/tests/test-devel-warnings.t b/tests/test-devel-warnings.t --- a/tests/test-devel-warnings.t +++ b/tests/test-devel-warnings.t @@ -218,7 +218,7 @@ Test warning on config option access and $ cat << EOF > ${TESTTMP}/buggyconfig.py > """A small extension that tests our developer warnings for config""" > - > from mercurial import registrar + > from mercurial import registrar, configitems > > cmdtable = {} > command = registrar.command(cmdtable) @@ -227,6 +227,7 @@ Test warning on config option access and > configitem = registrar.configitem(configtable) > > configitem('test', 'some', default='foo') + > configitem('test', 'dynamic', default=configitems.dynamicdefault) > # overwrite a core config > configitem('ui', 'quiet', default=False) > configitem('ui', 'interactive', default=None) @@ -236,6 +237,8 @@ Test warning on config option access and > repo.ui.config('ui', 'quiet', False) > repo.ui.config('ui', 'interactive', None) > repo.ui.config('test', 'some', 'foo') + > repo.ui.config('test', 'dynamic', 'some-required-default') + > repo.ui.config('test', 'dynamic') > EOF $ hg --config "extensions.buggyconfig=${TESTTMP}/buggyconfig.py" buggyconfig @@ -244,5 +247,6 @@ Test warning on config option access and devel-warn: specifying a default value for a registered config item: 'ui.quiet' 'False' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob) devel-warn: specifying a default value for a registered config item: 'ui.interactive' 'None' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob) devel-warn: specifying a default value for a registered config item: 'test.some' 'foo' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob) + devel-warn: config item requires an explicit default value: 'test.dynamic' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob) $ cd ..