Fix precommit issues

This commit is contained in:
Josh Abrams 2022-11-12 02:26:00 -05:00
parent 189490a938
commit 0d40562dcb
4 changed files with 35 additions and 15 deletions

View file

@ -100,6 +100,7 @@ def _ns(
rewrite_command=rewrite_command, rewrite_command=rewrite_command,
files=(), files=(),
hook=None, hook=None,
tags=(),
verbose=False, verbose=False,
show_diff_on_failure=False, show_diff_on_failure=False,
) )

View file

@ -11,6 +11,7 @@ import time
import unicodedata import unicodedata
from typing import Any from typing import Any
from typing import Collection from typing import Collection
from typing import Iterable
from typing import MutableMapping from typing import MutableMapping
from typing import Sequence 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. # be explicit, other git errors don't mean it has an unstaged config.
return retcode == 1 return retcode == 1
def _hook_should_run(args: argparse.Namespace, hook: Hook) -> bool: def _hook_should_run(args: argparse.Namespace, hook: Hook) -> bool:
if args.hook_stage not in hook.stages: if args.hook_stage not in hook.stages:
return False return False
if args.tags: if args.tags:
return len(set(hook.tags) & set(args.tags)) > 0 return len(set(hook.tags) & set(args.tags)) > 0
return ( return (
not args.hook not args.hook or
or hook.id == args.hook hook.id == args.hook or
or hook.alias == args.hook 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 ( return (
hook.id in skips hook.id in skips or
or hook.alias in skips hook.alias in skips or
or len(set(hook.tags) & set(skips)) > 0 len(set(hook.tags) & set(skips)) > 0
) )
def run( def run(
config_file: str, config_file: str,
store: Store, store: Store,
@ -438,7 +442,8 @@ def run(
return 1 return 1
if args.tags and not hooks: if args.tags and not hooks:
output.write_line( 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 return 1

View file

@ -4,4 +4,4 @@
language: script language: script
files: '' files: ''
tags: tags:
- foo - foo

View file

@ -144,14 +144,21 @@ def _do_run(cap_out, store, repo, args, environ={}, config_file=C.CONFIG_FILE):
def _test_run( def _test_run(
cap_out, store, repo, opts, expected_outputs, expected_ret, stage, 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: if stage:
stage_a_file() stage_a_file()
if environ_override is None: if environ_override is None:
environ_override = {} environ_override = {}
args = run_opts(**opts) 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) assert ret == expected_ret, (ret, expected_ret, printed)
for expected_output_part in expected_outputs: for expected_output_part in expected_outputs:
@ -369,13 +376,19 @@ def test_show_diff_on_failure(
), ),
( (
{'tags': ['bar', 'baz']}, {'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, 1,
True, True,
), ),
( (
{'tags': ['bar', 'baz'], 'hook_stage': 'push'}, {'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, 1,
True, True,
), ),
@ -633,6 +646,7 @@ def test_skip_aliased_hook(cap_out, store, aliased_repo):
for msg in (b'Bash hook', b'Skipped'): for msg in (b'Bash hook', b'Skipped'):
assert printed.count(msg) == 1 assert printed.count(msg) == 1
def test_skip_tag(cap_out, store, repo_with_passing_hook): def test_skip_tag(cap_out, store, repo_with_passing_hook):
_test_run( _test_run(
cap_out, cap_out,
@ -642,7 +656,7 @@ def test_skip_tag(cap_out, store, repo_with_passing_hook):
(b'Bash hook', b'Skipped'), (b'Bash hook', b'Skipped'),
0, 0,
True, True,
environ_override={'SKIP': 'foo'} environ_override={'SKIP': 'foo'},
) )