Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6b005cf

Browse files
committed
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
2 parents 8c8bb2c + 603bf15 commit 6b005cf

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

pre_commit/repository.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,16 @@ def additional_dependencies(self):
6161

6262
@cached_property
6363
def hooks(self):
64-
# TODO: merging in manifest dicts is a smell imo
64+
for hook in self.repo_config['hooks']:
65+
if hook['id'] not in self.manifest.hooks:
66+
logger.error(
67+
'`{0}` is not present in repository {1}. '
68+
'Typo? Perhaps it is introduced in a newer version? '
69+
'Often `pre-commit autoupdate` fixes this.'.format(
70+
hook['id'], self.repo_config['repo'],
71+
)
72+
)
73+
exit(1)
6574
return tuple(
6675
(hook['id'], dict(self.manifest.hooks[hook['id']], **hook))
6776
for hook in self.repo_config['hooks']

tests/repository_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import unicode_literals
33

44
import io
5+
import logging
56
import os
67
import os.path
78
import shutil
@@ -485,3 +486,26 @@ def test_local_repository():
485486
with pytest.raises(NotImplementedError):
486487
local_repo.manifest
487488
assert len(local_repo.hooks) == 1
489+
490+
491+
@pytest.yield_fixture
492+
def fake_log_handler():
493+
handler = mock.Mock(level=logging.INFO)
494+
logger = logging.getLogger('pre_commit')
495+
logger.addHandler(handler)
496+
yield handler
497+
logger.removeHandler(handler)
498+
499+
500+
def test_hook_id_not_present(tempdir_factory, store, fake_log_handler):
501+
path = make_repo(tempdir_factory, 'script_hooks_repo')
502+
config = make_config_from_repo(path)
503+
config['hooks'][0]['id'] = 'i-dont-exist'
504+
repo = Repository.create(config, store)
505+
with pytest.raises(SystemExit):
506+
repo.install()
507+
assert fake_log_handler.handle.call_args[0][0].msg == (
508+
'`i-dont-exist` is not present in repository {0}. '
509+
'Typo? Perhaps it is introduced in a newer version? '
510+
'Often `pre-commit autoupdate` fixes this.'.format(path)
511+
)

0 commit comments

Comments
 (0)