diff --git a/pre_commit/commands/hook_impl.py b/pre_commit/commands/hook_impl.py index f5995e9a..54ab169d 100644 --- a/pre_commit/commands/hook_impl.py +++ b/pre_commit/commands/hook_impl.py @@ -100,6 +100,7 @@ def _ns( rewrite_command=rewrite_command, files=(), hook=None, + tags=(), verbose=False, show_diff_on_failure=False, ) diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py index a768cd4a..47ba34aa 100644 --- a/pre_commit/commands/run.py +++ b/pre_commit/commands/run.py @@ -11,6 +11,7 @@ import time import unicodedata from typing import Any from typing import Collection +from typing import Iterable from typing import MutableMapping from typing import Sequence @@ -323,26 +324,29 @@ def _has_unstaged_config(config_file: str) -> bool: # be explicit, other git errors don't mean it has an unstaged config. return retcode == 1 + def _hook_should_run(args: argparse.Namespace, hook: Hook) -> bool: if args.hook_stage not in hook.stages: return False if args.tags: return len(set(hook.tags) & set(args.tags)) > 0 - + return ( - not args.hook - or hook.id == args.hook - or hook.alias == args.hook + not args.hook or + hook.id == args.hook or + hook.alias == args.hook ) -def _hook_is_skipped(skips: Sequence[str], hook: Hook) -> bool: + +def _hook_is_skipped(skips: Iterable[str], hook: Hook) -> bool: return ( - hook.id in skips - or hook.alias in skips - or len(set(hook.tags) & set(skips)) > 0 + hook.id in skips or + hook.alias in skips or + len(set(hook.tags) & set(skips)) > 0 ) + def run( config_file: str, store: Store, @@ -438,7 +442,8 @@ def run( return 1 if args.tags and not hooks: output.write_line( - f'No hooks with tags matching `{args.tags}` in stage `{args.hook_stage}`' + f'No hooks with tags matching `{args.tags}` ' + f'in stage `{args.hook_stage}`', ) return 1 diff --git a/testing/resources/script_hooks_repo/.pre-commit-hooks.yaml b/testing/resources/script_hooks_repo/.pre-commit-hooks.yaml index ec1f3fb6..7519b09f 100644 --- a/testing/resources/script_hooks_repo/.pre-commit-hooks.yaml +++ b/testing/resources/script_hooks_repo/.pre-commit-hooks.yaml @@ -4,4 +4,4 @@ language: script files: '' tags: - - foo + - foo diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index 37fbef95..b73a6b5b 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -144,14 +144,21 @@ def _do_run(cap_out, store, repo, args, environ={}, config_file=C.CONFIG_FILE): def _test_run( cap_out, store, repo, opts, expected_outputs, expected_ret, stage, - config_file=C.CONFIG_FILE, environ_override=None + config_file=C.CONFIG_FILE, environ_override=None, ): if stage: stage_a_file() if environ_override is None: environ_override = {} args = run_opts(**opts) - ret, printed = _do_run(cap_out, store, repo, args, config_file=config_file, environ=environ_override) + ret, printed = _do_run( + cap_out, + store, + repo, + args, + config_file=config_file, + environ=environ_override, + ) assert ret == expected_ret, (ret, expected_ret, printed) for expected_output_part in expected_outputs: @@ -369,13 +376,19 @@ def test_show_diff_on_failure( ), ( {'tags': ['bar', 'baz']}, - (b'No hooks with tags matching `[\'bar\', \'baz\']` in stage `commit`',), + ( + b'No hooks with tags matching `[\'bar\', \'baz\']`', + b'in stage `commit`', + ), 1, True, ), ( {'tags': ['bar', 'baz'], 'hook_stage': 'push'}, - (b'No hooks with tags matching `[\'bar\', \'baz\']` in stage `push`',), + ( + b'No hooks with tags matching `[\'bar\', \'baz\']`', + b'in stage `push`', + ), 1, True, ), @@ -633,6 +646,7 @@ def test_skip_aliased_hook(cap_out, store, aliased_repo): for msg in (b'Bash hook', b'Skipped'): assert printed.count(msg) == 1 + def test_skip_tag(cap_out, store, repo_with_passing_hook): _test_run( cap_out, @@ -642,7 +656,7 @@ def test_skip_tag(cap_out, store, repo_with_passing_hook): (b'Bash hook', b'Skipped'), 0, True, - environ_override={'SKIP': 'foo'} + environ_override={'SKIP': 'foo'}, )