mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Merge pull request #306 from pre-commit/warn_on_hook_not_present_instead_of_keyerror
Produce a useful error message when hook id is not present. Resolves #194
This commit is contained in:
commit
6b005cff0d
2 changed files with 34 additions and 1 deletions
|
|
@ -61,7 +61,16 @@ class Repository(object):
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def hooks(self):
|
def hooks(self):
|
||||||
# TODO: merging in manifest dicts is a smell imo
|
for hook in self.repo_config['hooks']:
|
||||||
|
if hook['id'] not in self.manifest.hooks:
|
||||||
|
logger.error(
|
||||||
|
'`{0}` is not present in repository {1}. '
|
||||||
|
'Typo? Perhaps it is introduced in a newer version? '
|
||||||
|
'Often `pre-commit autoupdate` fixes this.'.format(
|
||||||
|
hook['id'], self.repo_config['repo'],
|
||||||
|
)
|
||||||
|
)
|
||||||
|
exit(1)
|
||||||
return tuple(
|
return tuple(
|
||||||
(hook['id'], dict(self.manifest.hooks[hook['id']], **hook))
|
(hook['id'], dict(self.manifest.hooks[hook['id']], **hook))
|
||||||
for hook in self.repo_config['hooks']
|
for hook in self.repo_config['hooks']
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ from __future__ import absolute_import
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import io
|
import io
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
import shutil
|
import shutil
|
||||||
|
|
@ -485,3 +486,26 @@ def test_local_repository():
|
||||||
with pytest.raises(NotImplementedError):
|
with pytest.raises(NotImplementedError):
|
||||||
local_repo.manifest
|
local_repo.manifest
|
||||||
assert len(local_repo.hooks) == 1
|
assert len(local_repo.hooks) == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.yield_fixture
|
||||||
|
def fake_log_handler():
|
||||||
|
handler = mock.Mock(level=logging.INFO)
|
||||||
|
logger = logging.getLogger('pre_commit')
|
||||||
|
logger.addHandler(handler)
|
||||||
|
yield handler
|
||||||
|
logger.removeHandler(handler)
|
||||||
|
|
||||||
|
|
||||||
|
def test_hook_id_not_present(tempdir_factory, store, fake_log_handler):
|
||||||
|
path = make_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
|
config = make_config_from_repo(path)
|
||||||
|
config['hooks'][0]['id'] = 'i-dont-exist'
|
||||||
|
repo = Repository.create(config, store)
|
||||||
|
with pytest.raises(SystemExit):
|
||||||
|
repo.install()
|
||||||
|
assert fake_log_handler.handle.call_args[0][0].msg == (
|
||||||
|
'`i-dont-exist` is not present in repository {0}. '
|
||||||
|
'Typo? Perhaps it is introduced in a newer version? '
|
||||||
|
'Often `pre-commit autoupdate` fixes this.'.format(path)
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue