Patchwork [3,of,6] configitems: add a devel warning for extensions items overiding core one

login
register
mail settings
Submitter Pierre-Yves David
Date June 22, 2017, 7:34 a.m.
Message ID <f69b5b8655172cc33f07.1498116872@nodosa.octopoid.net>
Download mbox | patch
Permalink /patch/21606/
State Changes Requested
Headers show

Comments

Pierre-Yves David - June 22, 2017, 7:34 a.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@octobus.net>
# Date 1497808374 -7200
#      Sun Jun 18 19:52:54 2017 +0200
# Node ID f69b5b8655172cc33f0765809c9c7620978910c0
# Parent  630aa32cf23b88fa9ab2d7ffac94ecea1307b4c0
# EXP-Topic config.register
# Available At https://www.mercurial-scm.org/repo/users/marmoute/mercurial/
#              hg pull https://www.mercurial-scm.org/repo/users/marmoute/mercurial/ -r f69b5b865517
configitems: add a devel warning for extensions items overiding core one

We do not want such case to pass silently. In the future we'll likely have
useful tool for an extension to alter the existing definition in core.

Patch

diff --git a/mercurial/configitems.py b/mercurial/configitems.py
--- a/mercurial/configitems.py
+++ b/mercurial/configitems.py
@@ -16,7 +16,15 @@  from . import (
 def loadconfigtable(ui, extname, configtable):
     """update config item known to the ui with the extension ones"""
     for section, items in  configtable.items():
-        ui._knownconfig.setdefault(section, {}).update(items)
+        knownitems = ui._knownconfig.setdefault(section, {})
+        knownkeys = set(knownitems)
+        newkeys = set(items)
+        for key in sorted(knownkeys & newkeys):
+            msg = "extension '%s' overwrite config item '%s.%s'"
+            msg %= (extname, section, key)
+            ui.develwarn(msg, config='warn-config')
+
+        knownitems.update(items)
 
 class configitem(object):
     """represent a known config item
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
@@ -203,14 +203,26 @@  Test warning on config option access and
   > cmdtable = {}
   > command = registrar.command(cmdtable)
   > 
+  > configtable = {}
+  > configitem = registrar.configitem(configtable)
+  > 
+  > configitem('test', 'some', default='foo')
+  > # overwrite a core config
+  > configitem('ui', 'quiet', default=False)
+  > configitem('ui', 'interactive', default=None)
+  > 
   > @command('buggyconfig')
   > def cmdbuggyconfig(ui, repo):
   >     repo.ui.config('ui', 'quiet', False)
   >     repo.ui.config('ui', 'interactive', None)
+  >     repo.ui.config('test', 'some', 'foo')
   > EOF
 
   $ hg --config "extensions.buggyconfig=${TESTTMP}/buggyconfig.py" buggyconfig
+  devel-warn: extension 'buggyconfig' overwrite config item 'ui.interactive' at: */mercurial/dispatch.py:* (_dispatch) (glob)
+  devel-warn: extension 'buggyconfig' overwrite config item 'ui.quiet' at: */mercurial/dispatch.py:* (_dispatch) (glob)
   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)
 
   $ cd ..