Add a manual stage for cli-only interaction · precommit/pre-commit@bf5792e · GitHub
Skip to content

Commit bf5792e

Browse files
committed
Add a manual stage for cli-only interaction
1 parent 4088f55 commit bf5792e

5 files changed

Lines changed: 31 additions & 46 deletions

File tree

pre_commit/clientlib.py

Lines changed: 2 additions & 5 deletions

pre_commit/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@
2020

2121
VERSION = pkg_resources.get_distribution('pre-commit').version
2222
VERSION_PARSED = pkg_resources.parse_version(VERSION)
23+
24+
# `manual` is not invoked by any installed git hook. See #719
25+
STAGES = ('commit', 'commit-msg', 'manual', 'push')

pre_commit/main.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ def _add_run_options(parser):
7070
help='Filename to check when running during `commit-msg`',
7171
)
7272
parser.add_argument(
73-
'--hook-stage', choices=('commit', 'push', 'commit-msg'),
74-
default='commit',
75-
help='The stage during which the hook is fired e.g. commit or push.',
73+
'--hook-stage', choices=C.STAGES, default='commit',
74+
help='The stage during which the hook is fired. One of %(choices)s',
7675
)
7776
parser.add_argument(
7877
'--show-diff-on-failure', action='store_true',

tests/commands/run_test.py

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -529,52 +529,37 @@ def test_lots_of_files(mock_out_store_directory, tempdir_factory):
529529
)
530530

531531

532-
def test_push_hook(cap_out, repo_with_passing_hook, mock_out_store_directory):
532+
def test_stages(cap_out, repo_with_passing_hook, mock_out_store_directory):
533533
config = OrderedDict((
534534
('repo', 'local'),
535535
(
536-
'hooks', (
537-
OrderedDict((
538-
('id', 'flake8'),
539-
('name', 'hook 1'),
540-
('entry', "'{}' -m flake8".format(sys.executable)),
541-
('language', 'system'),
542-
('types', ['python']),
543-
('stages', ['commit']),
544-
)),
545-
OrderedDict((
546-
('id', 'do_not_commit'),
547-
('name', 'hook 2'),
548-
('entry', 'DO NOT COMMIT'),
549-
('language', 'pygrep'),
550-
('types', ['text']),
551-
('stages', ['push']),
552-
)),
536+
'hooks', tuple(
537+
{
538+
'id': 'do-not-commit-{}'.format(i),
539+
'name': 'hook {}'.format(i),
540+
'entry': 'DO NOT COMMIT',
541+
'language': 'pygrep',
542+
'stages': [stage],
543+
}
544+
for i, stage in enumerate(('commit', 'push', 'manual'), 1)
553545
),
554546
),
555547
))
556548
add_config_to_repo(repo_with_passing_hook, config)
557549

558-
open('dummy.py', 'a').close()
559-
cmd_output('git', 'add', 'dummy.py')
560-
561-
_test_run(
562-
cap_out,
563-
repo_with_passing_hook,
564-
{'hook_stage': 'commit'},
565-
expected_outputs=[b'hook 1'],
566-
expected_ret=0,
567-
stage=False,
568-
)
550+
stage_a_file()
569551

570-
_test_run(
571-
cap_out,
572-
repo_with_passing_hook,
573-
{'hook_stage': 'push'},
574-
expected_outputs=[b'hook 2'],
575-
expected_ret=0,
576-
stage=False,
577-
)
552+
def _run_for_stage(stage):
553+
args = run_opts(hook_stage=stage)
554+
ret, printed = _do_run(cap_out, repo_with_passing_hook, args)
555+
assert not ret, (ret, printed)
556+
# this test should only run one hook
557+
assert printed.count(b'hook ') == 1
558+
return printed
559+
560+
assert _run_for_stage('commit').startswith(b'hook 1...')
561+
assert _run_for_stage('push').startswith(b'hook 2...')
562+
assert _run_for_stage('manual').startswith(b'hook 3...')
578563

579564

580565
def test_commit_msg_hook(cap_out, commit_msg_repo, mock_out_store_directory):

tests/conftest.py

Lines changed: 2 additions & 1 deletion

0 commit comments

Comments
 (0)