Add a check and test to the real top level and improve the warning message

This commit is contained in:
Yoav Caspi 2019-05-11 22:57:52 +03:00
parent fd9d9d276b
commit 217d31ec1c
2 changed files with 32 additions and 9 deletions

View file

@ -149,12 +149,10 @@ def _entry(modname):
def warn_on_unknown_keys_at_top_level(extra, orig_keys):
logger.warning(
'Your pre-commit-config contain these extra keys: {}. '
'while the only valid keys are: {}.'.format(
', '.join(extra),
', '.join(sorted(orig_keys)),
'Unexpected config key(s): {}'.format(
', '.join(sorted(extra)),
),
),
)
_meta = (
@ -236,7 +234,7 @@ CONFIG_REPO_DICT = cfgv.Map(
MigrateShaToRev(),
cfgv.WarnAdditionalKeys(
{'repo', 'rev', 'hooks'},
('repo', 'rev', 'hooks'),
warn_on_unknown_keys_at_top_level,
),
)
@ -264,6 +262,10 @@ CONFIG_SCHEMA = cfgv.Map(
cfgv.check_and(cfgv.check_string, check_min_version),
'0',
),
cfgv.WarnAdditionalKeys(
('repos',),
warn_on_unknown_keys_at_top_level,
),
)

View file

@ -118,7 +118,7 @@ def test_validate_config_old_list_format_ok(tmpdir):
assert not validate_config_main((f.strpath,))
def test_validate_warn_on_unknown_keys_at_top_level(tmpdir, caplog):
def test_validate_warn_on_unknown_keys_at_repo_level(tmpdir, caplog):
f = tmpdir.join('cfg.yaml')
f.write(
'- repo: https://gitlab.com/pycqa/flake8\n'
@ -133,8 +133,29 @@ def test_validate_warn_on_unknown_keys_at_top_level(tmpdir, caplog):
(
'pre_commit',
logging.WARNING,
'Your pre-commit-config contain these extra keys: args. '
'while the only valid keys are: hooks, repo, rev.',
'Unexpected config key(s): args',
),
]
def test_validate_warn_on_unknown_keys_at_top_level(tmpdir, caplog):
f = tmpdir.join('cfg.yaml')
f.write(
'repos:\n'
'- repo: https://gitlab.com/pycqa/flake8\n'
' rev: 3.7.7\n'
' hooks:\n'
' - id: flake8\n'
'foo:\n'
' id: 1.0.0\n',
)
ret_val = validate_config_main((f.strpath,))
assert not ret_val
assert caplog.record_tuples == [
(
'pre_commit',
logging.WARNING,
'Unexpected config key(s): foo',
),
]