Skip to content

Commit 3815e2e

Browse files
authored
Merge pull request pre-commit#3576 from pre-commit/fix-stages-config-error
fix missing context in error for stages
2 parents 46297f7 + aa2961c commit 3815e2e

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

pre_commit/clientlib.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,12 @@ def check(self, dct: dict[str, Any]) -> None:
116116
if self.key not in dct:
117117
return
118118

119-
val = dct[self.key]
120-
cfgv.check_array(cfgv.check_any)(val)
119+
with cfgv.validate_context(f'At key: {self.key}'):
120+
val = dct[self.key]
121+
cfgv.check_array(cfgv.check_any)(val)
121122

122-
val = [transform_stage(v) for v in val]
123-
cfgv.check_array(cfgv.check_one_of(STAGES))(val)
123+
val = [transform_stage(v) for v in val]
124+
cfgv.check_array(cfgv.check_one_of(STAGES))(val)
124125

125126
def apply_default(self, dct: dict[str, Any]) -> None:
126127
if self.key not in dct:

tests/clientlib_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,27 @@ def test_validate_optional_sensible_regex_at_top_level(caplog, regex, warning):
309309
assert caplog.record_tuples == [('pre_commit', logging.WARNING, warning)]
310310

311311

312+
def test_invalid_stages_error():
313+
cfg = {'repos': [sample_local_config()]}
314+
cfg['repos'][0]['hooks'][0]['stages'] = ['invalid']
315+
316+
with pytest.raises(cfgv.ValidationError) as excinfo:
317+
cfgv.validate(cfg, CONFIG_SCHEMA)
318+
319+
assert str(excinfo.value) == (
320+
'\n'
321+
'==> At Config()\n'
322+
'==> At key: repos\n'
323+
"==> At Repository(repo='local')\n"
324+
'==> At key: hooks\n'
325+
"==> At Hook(id='do_not_commit')\n"
326+
# this line was missing due to the custom validator
327+
'==> At key: stages\n'
328+
'==> At index 0\n'
329+
"=====> 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
330+
)
331+
332+
312333
def test_warning_for_deprecated_stages(caplog):
313334
config_obj = sample_local_config()
314335
config_obj['hooks'][0]['stages'] = ['commit', 'push']

0 commit comments

Comments
 (0)