Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement default_stages #909

Merged
merged 1 commit into from
Jan 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions pre_commit/clientlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ def _entry(modname):
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),
)
Expand Down
2 changes: 1 addition & 1 deletion pre_commit/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions pre_commit/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
22 changes: 20 additions & 2 deletions tests/repository_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
}

Expand All @@ -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)
Expand Down Expand Up @@ -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,
)