mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-14 13:00:10 -06:00
Merge pull request #646 from pre-commit/apply_language_to_local_hooks
Apply interpreter version defaulting to local hooks too
This commit is contained in:
@@ -6,7 +6,6 @@ from cached_property import cached_property
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.clientlib import load_manifest
|
||||
from pre_commit.languages.all import languages
|
||||
|
||||
|
||||
class Manifest(object):
|
||||
@@ -19,10 +18,4 @@ class Manifest(object):
|
||||
|
||||
@cached_property
|
||||
def hooks(self):
|
||||
ret = {}
|
||||
for hook in self.manifest_contents:
|
||||
if hook['language_version'] == 'default':
|
||||
language = languages[hook['language']]
|
||||
hook['language_version'] = language.get_default_version()
|
||||
ret[hook['id']] = hook
|
||||
return ret
|
||||
return {hook['id']: hook for hook in self.manifest_contents}
|
||||
|
||||
@@ -102,20 +102,27 @@ def _install_all(venvs, repo_url, store):
|
||||
_write_state(cmd_runner, venv, state)
|
||||
|
||||
|
||||
def _validate_minimum_version(hook):
|
||||
hook_version = pkg_resources.parse_version(
|
||||
hook['minimum_pre_commit_version'],
|
||||
)
|
||||
if hook_version > C.VERSION_PARSED:
|
||||
def _hook(*hook_dicts):
|
||||
ret, rest = dict(hook_dicts[0]), hook_dicts[1:]
|
||||
for dct in rest:
|
||||
ret.update(dct)
|
||||
|
||||
version = pkg_resources.parse_version(ret['minimum_pre_commit_version'])
|
||||
if version > C.VERSION_PARSED:
|
||||
logger.error(
|
||||
'The hook `{}` requires pre-commit version {} but '
|
||||
'version {} is installed. '
|
||||
'The hook `{}` requires pre-commit version {} but version {} '
|
||||
'is installed. '
|
||||
'Perhaps run `pip install --upgrade pre-commit`.'.format(
|
||||
hook['id'], hook_version, C.VERSION_PARSED,
|
||||
ret['id'], version, C.VERSION_PARSED,
|
||||
),
|
||||
)
|
||||
exit(1)
|
||||
return hook
|
||||
|
||||
if ret['language_version'] == 'default':
|
||||
language = languages[ret['language']]
|
||||
ret['language_version'] = language.get_default_version()
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
class Repository(object):
|
||||
@@ -161,10 +168,8 @@ class Repository(object):
|
||||
)
|
||||
exit(1)
|
||||
|
||||
_validate_minimum_version(self.manifest.hooks[hook['id']])
|
||||
|
||||
return tuple(
|
||||
(hook['id'], dict(self.manifest.hooks[hook['id']], **hook))
|
||||
(hook['id'], _hook(self.manifest.hooks[hook['id']], hook))
|
||||
for hook in self.repo_config['hooks']
|
||||
)
|
||||
|
||||
@@ -215,16 +220,14 @@ class LocalRepository(Repository):
|
||||
|
||||
@cached_property
|
||||
def hooks(self):
|
||||
def _from_manifest_dct(dct):
|
||||
dct = validate(dct, MANIFEST_HOOK_DICT)
|
||||
dct = apply_defaults(dct, MANIFEST_HOOK_DICT)
|
||||
dct = _hook(dct)
|
||||
return dct
|
||||
|
||||
return tuple(
|
||||
(
|
||||
hook['id'],
|
||||
_validate_minimum_version(
|
||||
apply_defaults(
|
||||
validate(hook, MANIFEST_HOOK_DICT),
|
||||
MANIFEST_HOOK_DICT,
|
||||
),
|
||||
),
|
||||
)
|
||||
(hook['id'], _from_manifest_dct(hook))
|
||||
for hook in self.repo_config['hooks']
|
||||
)
|
||||
|
||||
|
||||
@@ -58,13 +58,3 @@ def test_hooks(manifest):
|
||||
'types': ['file'],
|
||||
'exclude_types': [],
|
||||
}
|
||||
|
||||
|
||||
def test_default_python_language_version(store, tempdir_factory):
|
||||
path = make_repo(tempdir_factory, 'python_hooks_repo')
|
||||
repo_path = store.clone(path, git.head_sha(path))
|
||||
manifest = Manifest(repo_path)
|
||||
|
||||
# This assertion is difficult as it is version dependent, just assert
|
||||
# that it is *something*
|
||||
assert manifest.hooks['foo']['language_version'] != 'default'
|
||||
|
||||
@@ -71,6 +71,16 @@ def test_python_hook(tempdir_factory, store):
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
def test_python_hook_default_version(tempdir_factory, store):
|
||||
# make sure that this continues to work for platforms where default
|
||||
# language detection does not work
|
||||
with mock.patch.object(
|
||||
python, 'get_default_version', return_value='default',
|
||||
):
|
||||
test_python_hook(tempdir_factory, store)
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
def test_python_hook_args_with_spaces(tempdir_factory, store):
|
||||
_test_hook_repo(
|
||||
@@ -690,6 +700,8 @@ def test_local_python_repo(store):
|
||||
config = {'repo': 'local', 'hooks': hooks}
|
||||
repo = Repository.create(config, store)
|
||||
(_, hook), = repo.hooks
|
||||
# language_version should have been adjusted to the interpreter version
|
||||
assert hook['language_version'] != 'default'
|
||||
ret = repo.run_hook(hook, ('filename',))
|
||||
assert ret[0] == 0
|
||||
assert _norm_out(ret[1]) == b"['filename']\nHello World\n"
|
||||
|
||||
Reference in New Issue
Block a user