Skip to content

Commit bf5792e

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

File tree

5 files changed

+31
-46
lines changed

5 files changed

+31
-46
lines changed

pre_commit/clientlib.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ def _make_argparser(filenames_help):
3535
cfgv.Required('id', cfgv.check_string),
3636
cfgv.Required('name', cfgv.check_string),
3737
cfgv.Required('entry', cfgv.check_string),
38-
cfgv.Required(
39-
'language',
40-
cfgv.check_and(cfgv.check_string, cfgv.check_one_of(all_languages)),
41-
),
38+
cfgv.Required('language', cfgv.check_one_of(all_languages)),
4239

4340
cfgv.Optional(
4441
'files', cfgv.check_and(cfgv.check_string, cfgv.check_regex), '',
@@ -59,7 +56,7 @@ def _make_argparser(filenames_help):
5956
cfgv.Optional('language_version', cfgv.check_string, 'default'),
6057
cfgv.Optional('log_file', cfgv.check_string, ''),
6158
cfgv.Optional('minimum_pre_commit_version', cfgv.check_string, '0'),
62-
cfgv.Optional('stages', cfgv.check_array(cfgv.check_string), []),
59+
cfgv.Optional('stages', cfgv.check_array(cfgv.check_one_of(C.STAGES)), []),
6360
cfgv.Optional('verbose', cfgv.check_bool, False),
6461
)
6562
MANIFEST_SCHEMA = cfgv.Array(MANIFEST_HOOK_DICT)

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
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ def __init__(self, stream):
180180
def get_bytes(self):
181181
"""Get the output as-if no encoding occurred"""
182182
data = self._stream.data.getvalue()
183-
self._stream.data.truncate(0)
183+
self._stream.data.seek(0)
184+
self._stream.data.truncate()
184185
return data
185186

186187
def get(self):

0 commit comments

Comments
 (0)