Allow aliasing a hook and calling it by it's alias by s0undt3ch · Pull Request #886 · pre-commit/pre-commit · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pre_commit/clientlib.py
13 changes: 10 additions & 3 deletions pre_commit/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def _run_single_hook(filenames, hook, repo, args, skips, cols):
'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(
_hook_msg_start(hook, args.verbose),
end_msg=SKIPPED,
Expand Down Expand Up @@ -257,8 +257,15 @@ def run(config_file, store, args, environ=os.environ):
for repo in repositories(config, store):
for _, hook in repo.hooks:
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))

Expand Down
42 changes: 42 additions & 0 deletions tests/commands/run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ def repo_with_failing_hook(tempdir_factory):
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'):
open(filename, 'a').close()
cmd_output('git', 'add', filename)
Expand Down Expand Up @@ -388,6 +400,18 @@ def test_skip_hook(cap_out, store, repo_with_passing_hook):
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(
cap_out, store, repo_with_passing_hook,
):
Expand Down Expand Up @@ -416,6 +440,24 @@ def test_multiple_hooks_same_id(cap_out, store, repo_with_passing_hook):
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):
with cwd(repo_with_passing_hook):
_, stdout, _ = cmd_output_mocked_pre_commit_home(
Expand Down
1 change: 1 addition & 0 deletions tests/repository_test.py