Merge pull request #720 from pre-commit/manual

Add a manual stage for cli-only interaction
This commit is contained in:
Anthony Sottile 2018-03-07 13:15:53 -08:00 committed by GitHub
commit 2483cbde36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 45 deletions

View file

@ -35,10 +35,7 @@ MANIFEST_HOOK_DICT = cfgv.Map(
cfgv.Required('id', cfgv.check_string), cfgv.Required('id', cfgv.check_string),
cfgv.Required('name', cfgv.check_string), cfgv.Required('name', cfgv.check_string),
cfgv.Required('entry', cfgv.check_string), cfgv.Required('entry', cfgv.check_string),
cfgv.Required( cfgv.Required('language', cfgv.check_one_of(all_languages)),
'language',
cfgv.check_and(cfgv.check_string, cfgv.check_one_of(all_languages)),
),
cfgv.Optional( cfgv.Optional(
'files', cfgv.check_and(cfgv.check_string, cfgv.check_regex), '', 'files', cfgv.check_and(cfgv.check_string, cfgv.check_regex), '',
@ -59,7 +56,7 @@ MANIFEST_HOOK_DICT = cfgv.Map(
cfgv.Optional('language_version', cfgv.check_string, 'default'), cfgv.Optional('language_version', cfgv.check_string, 'default'),
cfgv.Optional('log_file', cfgv.check_string, ''), cfgv.Optional('log_file', cfgv.check_string, ''),
cfgv.Optional('minimum_pre_commit_version', cfgv.check_string, '0'), cfgv.Optional('minimum_pre_commit_version', cfgv.check_string, '0'),
cfgv.Optional('stages', cfgv.check_array(cfgv.check_string), []), cfgv.Optional('stages', cfgv.check_array(cfgv.check_one_of(C.STAGES)), []),
cfgv.Optional('verbose', cfgv.check_bool, False), cfgv.Optional('verbose', cfgv.check_bool, False),
) )
MANIFEST_SCHEMA = cfgv.Array(MANIFEST_HOOK_DICT) MANIFEST_SCHEMA = cfgv.Array(MANIFEST_HOOK_DICT)

View file

@ -20,3 +20,6 @@ LOCAL_REPO_VERSION = '1'
VERSION = pkg_resources.get_distribution('pre-commit').version VERSION = pkg_resources.get_distribution('pre-commit').version
VERSION_PARSED = pkg_resources.parse_version(VERSION) VERSION_PARSED = pkg_resources.parse_version(VERSION)
# `manual` is not invoked by any installed git hook. See #719
STAGES = ('commit', 'commit-msg', 'manual', 'push')

View file

@ -70,9 +70,8 @@ def _add_run_options(parser):
help='Filename to check when running during `commit-msg`', help='Filename to check when running during `commit-msg`',
) )
parser.add_argument( parser.add_argument(
'--hook-stage', choices=('commit', 'push', 'commit-msg'), '--hook-stage', choices=C.STAGES, default='commit',
default='commit', help='The stage during which the hook is fired. One of %(choices)s',
help='The stage during which the hook is fired e.g. commit or push.',
) )
parser.add_argument( parser.add_argument(
'--show-diff-on-failure', action='store_true', '--show-diff-on-failure', action='store_true',

View file

@ -529,52 +529,37 @@ def test_lots_of_files(mock_out_store_directory, tempdir_factory):
) )
def test_push_hook(cap_out, repo_with_passing_hook, mock_out_store_directory): def test_stages(cap_out, repo_with_passing_hook, mock_out_store_directory):
config = OrderedDict(( config = OrderedDict((
('repo', 'local'), ('repo', 'local'),
( (
'hooks', ( 'hooks', tuple(
OrderedDict(( {
('id', 'flake8'), 'id': 'do-not-commit-{}'.format(i),
('name', 'hook 1'), 'name': 'hook {}'.format(i),
('entry', "'{}' -m flake8".format(sys.executable)), 'entry': 'DO NOT COMMIT',
('language', 'system'), 'language': 'pygrep',
('types', ['python']), 'stages': [stage],
('stages', ['commit']), }
)), for i, stage in enumerate(('commit', 'push', 'manual'), 1)
OrderedDict((
('id', 'do_not_commit'),
('name', 'hook 2'),
('entry', 'DO NOT COMMIT'),
('language', 'pygrep'),
('types', ['text']),
('stages', ['push']),
)),
), ),
), ),
)) ))
add_config_to_repo(repo_with_passing_hook, config) add_config_to_repo(repo_with_passing_hook, config)
open('dummy.py', 'a').close() stage_a_file()
cmd_output('git', 'add', 'dummy.py')
_test_run( def _run_for_stage(stage):
cap_out, args = run_opts(hook_stage=stage)
repo_with_passing_hook, ret, printed = _do_run(cap_out, repo_with_passing_hook, args)
{'hook_stage': 'commit'}, assert not ret, (ret, printed)
expected_outputs=[b'hook 1'], # this test should only run one hook
expected_ret=0, assert printed.count(b'hook ') == 1
stage=False, return printed
)
_test_run( assert _run_for_stage('commit').startswith(b'hook 1...')
cap_out, assert _run_for_stage('push').startswith(b'hook 2...')
repo_with_passing_hook, assert _run_for_stage('manual').startswith(b'hook 3...')
{'hook_stage': 'push'},
expected_outputs=[b'hook 2'],
expected_ret=0,
stage=False,
)
def test_commit_msg_hook(cap_out, commit_msg_repo, mock_out_store_directory): def test_commit_msg_hook(cap_out, commit_msg_repo, mock_out_store_directory):

View file

@ -180,7 +180,8 @@ class Fixture(object):
def get_bytes(self): def get_bytes(self):
"""Get the output as-if no encoding occurred""" """Get the output as-if no encoding occurred"""
data = self._stream.data.getvalue() data = self._stream.data.getvalue()
self._stream.data.truncate(0) self._stream.data.seek(0)
self._stream.data.truncate()
return data return data
def get(self): def get(self):