mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-20 01:24:42 +04:00
Merge pull request #886 from s0undt3ch/features/repo-alias
Allow aliasing a hook and calling it by it's alias
This commit is contained in:
commit
8c550d0157
4 changed files with 54 additions and 3 deletions
|
|
@ -36,6 +36,7 @@ MANIFEST_HOOK_DICT = cfgv.Map(
|
||||||
cfgv.Required('name', cfgv.check_string),
|
cfgv.Required('name', cfgv.check_string),
|
||||||
cfgv.Required('entry', cfgv.check_string),
|
cfgv.Required('entry', cfgv.check_string),
|
||||||
cfgv.Required('language', cfgv.check_one_of(all_languages)),
|
cfgv.Required('language', cfgv.check_one_of(all_languages)),
|
||||||
|
cfgv.Optional('alias', cfgv.check_string, ''),
|
||||||
|
|
||||||
cfgv.Optional(
|
cfgv.Optional(
|
||||||
'files', cfgv.check_and(cfgv.check_string, cfgv.check_regex), '',
|
'files', cfgv.check_and(cfgv.check_string, cfgv.check_regex), '',
|
||||||
|
|
|
||||||
|
|
@ -77,7 +77,7 @@ def _run_single_hook(filenames, hook, repo, args, skips, cols):
|
||||||
'replacement.'.format(hook['id'], repo.repo_config['repo']),
|
'replacement.'.format(hook['id'], repo.repo_config['repo']),
|
||||||
)
|
)
|
||||||
|
|
||||||
if hook['id'] in skips:
|
if hook['id'] in skips or hook['alias'] in skips:
|
||||||
output.write(get_hook_message(
|
output.write(get_hook_message(
|
||||||
_hook_msg_start(hook, args.verbose),
|
_hook_msg_start(hook, args.verbose),
|
||||||
end_msg=SKIPPED,
|
end_msg=SKIPPED,
|
||||||
|
|
@ -257,8 +257,15 @@ def run(config_file, store, args, environ=os.environ):
|
||||||
for repo in repositories(config, store):
|
for repo in repositories(config, store):
|
||||||
for _, hook in repo.hooks:
|
for _, hook in repo.hooks:
|
||||||
if (
|
if (
|
||||||
(not args.hook or hook['id'] == args.hook) and
|
(
|
||||||
(not hook['stages'] or args.hook_stage in hook['stages'])
|
not args.hook or
|
||||||
|
hook['id'] == args.hook or
|
||||||
|
hook['alias'] == args.hook
|
||||||
|
) and
|
||||||
|
(
|
||||||
|
not hook['stages'] or
|
||||||
|
args.hook_stage in hook['stages']
|
||||||
|
)
|
||||||
):
|
):
|
||||||
repo_hooks.append((repo, hook))
|
repo_hooks.append((repo, hook))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,18 @@ def repo_with_failing_hook(tempdir_factory):
|
||||||
yield git_path
|
yield git_path
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def aliased_repo(tempdir_factory):
|
||||||
|
git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
|
with cwd(git_path):
|
||||||
|
with modify_config() as config:
|
||||||
|
config['repos'][0]['hooks'].append(
|
||||||
|
{'id': 'bash_hook', 'alias': 'foo_bash'},
|
||||||
|
)
|
||||||
|
stage_a_file()
|
||||||
|
yield git_path
|
||||||
|
|
||||||
|
|
||||||
def stage_a_file(filename='foo.py'):
|
def stage_a_file(filename='foo.py'):
|
||||||
open(filename, 'a').close()
|
open(filename, 'a').close()
|
||||||
cmd_output('git', 'add', filename)
|
cmd_output('git', 'add', filename)
|
||||||
|
|
@ -388,6 +400,18 @@ def test_skip_hook(cap_out, store, repo_with_passing_hook):
|
||||||
assert msg in printed
|
assert msg in printed
|
||||||
|
|
||||||
|
|
||||||
|
def test_skip_aliased_hook(cap_out, store, aliased_repo):
|
||||||
|
ret, printed = _do_run(
|
||||||
|
cap_out, store, aliased_repo,
|
||||||
|
run_opts(hook='foo_bash'),
|
||||||
|
{'SKIP': 'foo_bash'},
|
||||||
|
)
|
||||||
|
assert ret == 0
|
||||||
|
# Only the aliased hook runs and is skipped
|
||||||
|
for msg in (b'Bash hook', b'Skipped'):
|
||||||
|
assert printed.count(msg) == 1
|
||||||
|
|
||||||
|
|
||||||
def test_hook_id_not_in_non_verbose_output(
|
def test_hook_id_not_in_non_verbose_output(
|
||||||
cap_out, store, repo_with_passing_hook,
|
cap_out, store, repo_with_passing_hook,
|
||||||
):
|
):
|
||||||
|
|
@ -416,6 +440,24 @@ def test_multiple_hooks_same_id(cap_out, store, repo_with_passing_hook):
|
||||||
assert output.count(b'Bash hook') == 2
|
assert output.count(b'Bash hook') == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_aliased_hook_run(cap_out, store, aliased_repo):
|
||||||
|
ret, output = _do_run(
|
||||||
|
cap_out, store, aliased_repo,
|
||||||
|
run_opts(verbose=True, hook='bash_hook'),
|
||||||
|
)
|
||||||
|
assert ret == 0
|
||||||
|
# Both hooks will run since they share the same ID
|
||||||
|
assert output.count(b'Bash hook') == 2
|
||||||
|
|
||||||
|
ret, output = _do_run(
|
||||||
|
cap_out, store, aliased_repo,
|
||||||
|
run_opts(verbose=True, hook='foo_bash'),
|
||||||
|
)
|
||||||
|
assert ret == 0
|
||||||
|
# Only the aliased hook runs
|
||||||
|
assert output.count(b'Bash hook') == 1
|
||||||
|
|
||||||
|
|
||||||
def test_non_ascii_hook_id(repo_with_passing_hook, tempdir_factory):
|
def test_non_ascii_hook_id(repo_with_passing_hook, tempdir_factory):
|
||||||
with cwd(repo_with_passing_hook):
|
with cwd(repo_with_passing_hook):
|
||||||
_, stdout, _ = cmd_output_mocked_pre_commit_home(
|
_, stdout, _ = cmd_output_mocked_pre_commit_home(
|
||||||
|
|
|
||||||
|
|
@ -831,6 +831,7 @@ def test_manifest_hooks(tempdir_factory, store):
|
||||||
'exclude': '^$',
|
'exclude': '^$',
|
||||||
'files': '',
|
'files': '',
|
||||||
'id': 'bash_hook',
|
'id': 'bash_hook',
|
||||||
|
'alias': '',
|
||||||
'language': 'script',
|
'language': 'script',
|
||||||
'language_version': 'default',
|
'language_version': 'default',
|
||||||
'log_file': '',
|
'log_file': '',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue