Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pre_commit/clientlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,12 @@ def check(self, dct: dict[str, Any]) -> None:
if self.key not in dct:
return

val = dct[self.key]
cfgv.check_array(cfgv.check_any)(val)
with cfgv.validate_context(f'At key: {self.key}'):
val = dct[self.key]
cfgv.check_array(cfgv.check_any)(val)

val = [transform_stage(v) for v in val]
cfgv.check_array(cfgv.check_one_of(STAGES))(val)
val = [transform_stage(v) for v in val]
cfgv.check_array(cfgv.check_one_of(STAGES))(val)

def apply_default(self, dct: dict[str, Any]) -> None:
if self.key not in dct:
Expand Down
21 changes: 21 additions & 0 deletions tests/clientlib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,27 @@ def test_validate_optional_sensible_regex_at_top_level(caplog, regex, warning):
assert caplog.record_tuples == [('pre_commit', logging.WARNING, warning)]


def test_invalid_stages_error():
cfg = {'repos': [sample_local_config()]}
cfg['repos'][0]['hooks'][0]['stages'] = ['invalid']

with pytest.raises(cfgv.ValidationError) as excinfo:
cfgv.validate(cfg, CONFIG_SCHEMA)

assert str(excinfo.value) == (
'\n'
'==> At Config()\n'
'==> At key: repos\n'
"==> At Repository(repo='local')\n"
'==> At key: hooks\n'
"==> At Hook(id='do_not_commit')\n"
# this line was missing due to the custom validator
'==> At key: stages\n'
'==> At index 0\n'
"=====> Expected one of commit-msg, manual, post-checkout, post-commit, post-merge, post-rewrite, pre-commit, pre-merge-commit, pre-push, pre-rebase, prepare-commit-msg but got: 'invalid'" # noqa: E501
)


def test_warning_for_deprecated_stages(caplog):
config_obj = sample_local_config()
config_obj['hooks'][0]['stages'] = ['commit', 'push']
Expand Down