Skip to content

Commit 6a0fe98

Browse files
committed
Apply interpreter version defaulting to local hooks too
1 parent 39d5205 commit 6a0fe98

File tree

4 files changed

+37
-39
lines changed

4 files changed

+37
-39
lines changed

pre_commit/manifest.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import pre_commit.constants as C
88
from pre_commit.clientlib import load_manifest
9-
from pre_commit.languages.all import languages
109

1110

1211
class Manifest(object):
@@ -19,10 +18,4 @@ def manifest_contents(self):
1918

2019
@cached_property
2120
def hooks(self):
22-
ret = {}
23-
for hook in self.manifest_contents:
24-
if hook['language_version'] == 'default':
25-
language = languages[hook['language']]
26-
hook['language_version'] = language.get_default_version()
27-
ret[hook['id']] = hook
28-
return ret
21+
return {hook['id']: hook for hook in self.manifest_contents}

pre_commit/repository.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,27 @@ def _need_installed():
102102
_write_state(cmd_runner, venv, state)
103103

104104

105-
def _validate_minimum_version(hook):
106-
hook_version = pkg_resources.parse_version(
107-
hook['minimum_pre_commit_version'],
108-
)
109-
if hook_version > C.VERSION_PARSED:
105+
def _hook(*hook_dicts):
106+
ret, rest = dict(hook_dicts[0]), hook_dicts[1:]
107+
for dct in rest:
108+
ret.update(dct)
109+
110+
version = pkg_resources.parse_version(ret['minimum_pre_commit_version'])
111+
if version > C.VERSION_PARSED:
110112
logger.error(
111-
'The hook `{}` requires pre-commit version {} but '
112-
'version {} is installed. '
113+
'The hook `{}` requires pre-commit version {} but version {} '
114+
'is installed. '
113115
'Perhaps run `pip install --upgrade pre-commit`.'.format(
114-
hook['id'], hook_version, C.VERSION_PARSED,
116+
ret['id'], version, C.VERSION_PARSED,
115117
),
116118
)
117119
exit(1)
118-
return hook
120+
121+
if ret['language_version'] == 'default':
122+
language = languages[ret['language']]
123+
ret['language_version'] = language.get_default_version()
124+
125+
return ret
119126

120127

121128
class Repository(object):
@@ -161,10 +168,8 @@ def hooks(self):
161168
)
162169
exit(1)
163170

164-
_validate_minimum_version(self.manifest.hooks[hook['id']])
165-
166171
return tuple(
167-
(hook['id'], dict(self.manifest.hooks[hook['id']], **hook))
172+
(hook['id'], _hook(self.manifest.hooks[hook['id']], hook))
168173
for hook in self.repo_config['hooks']
169174
)
170175

@@ -215,16 +220,14 @@ def manifest(self):
215220

216221
@cached_property
217222
def hooks(self):
223+
def _from_manifest_dct(dct):
224+
dct = validate(dct, MANIFEST_HOOK_DICT)
225+
dct = apply_defaults(dct, MANIFEST_HOOK_DICT)
226+
dct = _hook(dct)
227+
return dct
228+
218229
return tuple(
219-
(
220-
hook['id'],
221-
_validate_minimum_version(
222-
apply_defaults(
223-
validate(hook, MANIFEST_HOOK_DICT),
224-
MANIFEST_HOOK_DICT,
225-
),
226-
),
227-
)
230+
(hook['id'], _from_manifest_dct(hook))
228231
for hook in self.repo_config['hooks']
229232
)
230233

tests/manifest_test.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,3 @@ def test_hooks(manifest):
5858
'types': ['file'],
5959
'exclude_types': [],
6060
}
61-
62-
63-
def test_default_python_language_version(store, tempdir_factory):
64-
path = make_repo(tempdir_factory, 'python_hooks_repo')
65-
repo_path = store.clone(path, git.head_sha(path))
66-
manifest = Manifest(repo_path)
67-
68-
# This assertion is difficult as it is version dependent, just assert
69-
# that it is *something*
70-
assert manifest.hooks['foo']['language_version'] != 'default'

tests/repository_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ def test_python_hook(tempdir_factory, store):
7171
)
7272

7373

74+
@pytest.mark.integration
75+
def test_python_hook_default_version(tempdir_factory, store):
76+
# make sure that this continues to work for platforms where default
77+
# language detection does not work
78+
with mock.patch.object(
79+
python, 'get_default_version', return_value='default',
80+
):
81+
test_python_hook(tempdir_factory, store)
82+
83+
7484
@pytest.mark.integration
7585
def test_python_hook_args_with_spaces(tempdir_factory, store):
7686
_test_hook_repo(
@@ -690,6 +700,8 @@ def test_local_python_repo(store):
690700
config = {'repo': 'local', 'hooks': hooks}
691701
repo = Repository.create(config, store)
692702
(_, hook), = repo.hooks
703+
# language_version should have been adjusted to the interpreter version
704+
assert hook['language_version'] != 'default'
693705
ret = repo.run_hook(hook, ('filename',))
694706
assert ret[0] == 0
695707
assert _norm_out(ret[1]) == b"['filename']\nHello World\n"

0 commit comments

Comments
 (0)