Improve error message for duplicate keys in config.py

This commit is contained in:
Florian Bruhin 2017-09-24 19:42:39 +02:00
parent fb9fca2477
commit 40f0f75ad5
2 changed files with 14 additions and 1 deletions

View File

@ -212,7 +212,11 @@ class ConfigAPI:
def bind(self, key, command, mode='normal', *, force=False):
with self._handle_error('binding', key):
self._keyconfig.bind(key, command, mode=mode, force=force)
try:
self._keyconfig.bind(key, command, mode=mode, force=force)
except configexc.DuplicateKeyError as e:
raise configexc.KeybindingError('{} - use force=True to '
'override!'.format(e))
def unbind(self, key, mode='normal'):
with self._handle_error('unbinding', key):

View File

@ -364,6 +364,15 @@ class TestConfigPy:
"config.bind(',f', 'foo')")
confpy.read()
def test_bind_duplicate_key(self, confpy):
"""Make sure we get a nice error message on duplicate key bindings."""
confpy.write("config.bind('H', 'message-info back')")
api = confpy.read(error=True)
error = api.errors[0]
expected = "Duplicate key H - use force=True to override!"
assert str(error.exception) == expected
@pytest.mark.parametrize('line, key, mode', [
('config.unbind("o")', 'o', 'normal'),
('config.unbind("y", mode="prompt")', 'y', 'prompt'),