Implement default_stages

This commit is contained in:
Anthony Sottile
2019-01-06 09:54:55 -08:00
parent 9c6a1d80d6
commit bd65d8947f
4 changed files with 29 additions and 3 deletions

View File

@@ -224,6 +224,11 @@ CONFIG_SCHEMA = cfgv.Map(
cfgv.OptionalRecurse(
'default_language_version', DEFAULT_LANGUAGE_VERSION, {},
),
cfgv.Optional(
'default_stages',
cfgv.check_array(cfgv.check_one_of(C.STAGES)),
C.STAGES,
),
cfgv.Optional('exclude', cfgv.check_regex, '^$'),
cfgv.Optional('fail_fast', cfgv.check_bool, False),
)

View File

@@ -252,7 +252,7 @@ def run(config_file, store, args, environ=os.environ):
hook
for hook in all_hooks(config, store)
if not args.hook or hook.id == args.hook or hook.alias == args.hook
if not hook.stages or args.hook_stage in hook.stages
if args.hook_stage in hook.stages
]
if args.hook and not hooks:

View File

@@ -135,6 +135,9 @@ def _hook(*hook_dicts, **kwargs):
if ret['language_version'] == C.DEFAULT:
ret['language_version'] = languages[lang].get_default_version()
if not ret['stages']:
ret['stages'] = root_config['default_stages']
return ret

View File

@@ -715,6 +715,7 @@ def test_local_python_repo(store, local_python_config):
def test_default_language_version(store, local_python_config):
config = {
'default_language_version': {'python': 'fake'},
'default_stages': ['commit'],
'repos': [local_python_config],
}
@@ -728,6 +729,23 @@ def test_default_language_version(store, local_python_config):
assert hook.language_version == 'fake2'
def test_default_stages(store, local_python_config):
config = {
'default_language_version': {'python': C.DEFAULT},
'default_stages': ['commit'],
'repos': [local_python_config],
}
# `stages` was not set, should default
hook, = all_hooks(config, store)
assert hook.stages == ['commit']
# `stages` is set, should not default
config['repos'][0]['hooks'][0]['stages'] = ['push']
hook, = all_hooks(config, store)
assert hook.stages == ['push']
def test_hook_id_not_present(tempdir_factory, store, fake_log_handler):
path = make_repo(tempdir_factory, 'script_hooks_repo')
config = make_config_from_repo(path)
@@ -786,13 +804,13 @@ def test_manifest_hooks(tempdir_factory, store):
files='',
id='bash_hook',
language='script',
language_version=C.DEFAULT,
language_version='default',
log_file='',
minimum_pre_commit_version='0',
name='Bash hook',
pass_filenames=True,
require_serial=False,
stages=[],
stages=('commit', 'commit-msg', 'manual', 'push'),
types=['file'],
verbose=False,
)