Skip to content

Commit 217d31e

Browse files
committed
Add a check and test to the real top level and improve the warning message
1 parent fd9d9d2 commit 217d31e

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

pre_commit/clientlib.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,10 @@ def _entry(modname):
149149

150150
def warn_on_unknown_keys_at_top_level(extra, orig_keys):
151151
logger.warning(
152-
'Your pre-commit-config contain these extra keys: {}. '
153-
'while the only valid keys are: {}.'.format(
154-
', '.join(extra),
155-
', '.join(sorted(orig_keys)),
152+
'Unexpected config key(s): {}'.format(
153+
', '.join(sorted(extra)),
156154
),
157-
),
155+
)
158156

159157

160158
_meta = (
@@ -236,7 +234,7 @@ def warn_on_unknown_keys_at_top_level(extra, orig_keys):
236234

237235
MigrateShaToRev(),
238236
cfgv.WarnAdditionalKeys(
239-
{'repo', 'rev', 'hooks'},
237+
('repo', 'rev', 'hooks'),
240238
warn_on_unknown_keys_at_top_level,
241239
),
242240
)
@@ -264,6 +262,10 @@ def warn_on_unknown_keys_at_top_level(extra, orig_keys):
264262
cfgv.check_and(cfgv.check_string, check_min_version),
265263
'0',
266264
),
265+
cfgv.WarnAdditionalKeys(
266+
('repos',),
267+
warn_on_unknown_keys_at_top_level,
268+
),
267269
)
268270

269271

tests/clientlib_test.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def test_validate_config_old_list_format_ok(tmpdir):
118118
assert not validate_config_main((f.strpath,))
119119

120120

121-
def test_validate_warn_on_unknown_keys_at_top_level(tmpdir, caplog):
121+
def test_validate_warn_on_unknown_keys_at_repo_level(tmpdir, caplog):
122122
f = tmpdir.join('cfg.yaml')
123123
f.write(
124124
'- repo: https://gitlab.com/pycqa/flake8\n'
@@ -133,8 +133,29 @@ def test_validate_warn_on_unknown_keys_at_top_level(tmpdir, caplog):
133133
(
134134
'pre_commit',
135135
logging.WARNING,
136-
'Your pre-commit-config contain these extra keys: args. '
137-
'while the only valid keys are: hooks, repo, rev.',
136+
'Unexpected config key(s): args',
137+
),
138+
]
139+
140+
141+
def test_validate_warn_on_unknown_keys_at_top_level(tmpdir, caplog):
142+
f = tmpdir.join('cfg.yaml')
143+
f.write(
144+
'repos:\n'
145+
'- repo: https://gitlab.com/pycqa/flake8\n'
146+
' rev: 3.7.7\n'
147+
' hooks:\n'
148+
' - id: flake8\n'
149+
'foo:\n'
150+
' id: 1.0.0\n',
151+
)
152+
ret_val = validate_config_main((f.strpath,))
153+
assert not ret_val
154+
assert caplog.record_tuples == [
155+
(
156+
'pre_commit',
157+
logging.WARNING,
158+
'Unexpected config key(s): foo',
138159
),
139160
]
140161

0 commit comments

Comments
 (0)