Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e10b818

Browse files
authored
Merge pull request #1028 from yoavcaspi/warn_unknown_keys
Add warning to additional keys in config
2 parents a889f0b + 7a998a0 commit e10b818

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

pre_commit/clientlib.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import argparse
55
import functools
6+
import logging
67
import pipes
78
import sys
89

@@ -15,6 +16,8 @@
1516
from pre_commit.languages.all import all_languages
1617
from pre_commit.util import parse_version
1718

19+
logger = logging.getLogger('pre_commit')
20+
1821

1922
def check_type_tag(tag):
2023
if tag not in ALL_TAGS:
@@ -106,6 +109,8 @@ def validate_manifest_main(argv=None):
106109

107110

108111
class MigrateShaToRev(object):
112+
key = 'rev'
113+
109114
@staticmethod
110115
def _cond(key):
111116
return cfgv.Conditional(
@@ -144,6 +149,14 @@ def _entry(modname):
144149
)
145150

146151

152+
def warn_unknown_keys(extra, orig_keys):
153+
logger.warning(
154+
'Unexpected config key(s): {}'.format(
155+
', '.join(sorted(extra)),
156+
),
157+
)
158+
159+
147160
_meta = (
148161
(
149162
'check-hooks-apply', (
@@ -222,6 +235,10 @@ def _entry(modname):
222235
),
223236

224237
MigrateShaToRev(),
238+
cfgv.WarnAdditionalKeys(
239+
('repo', 'rev', 'hooks'),
240+
warn_unknown_keys,
241+
),
225242
)
226243
DEFAULT_LANGUAGE_VERSION = cfgv.Map(
227244
'DefaultLanguageVersion', None,
@@ -247,6 +264,17 @@ def _entry(modname):
247264
cfgv.check_and(cfgv.check_string, check_min_version),
248265
'0',
249266
),
267+
cfgv.WarnAdditionalKeys(
268+
(
269+
'repos',
270+
'default_language_version',
271+
'default_stages',
272+
'exclude',
273+
'fail_fast',
274+
'minimum_pre_commit_version',
275+
),
276+
warn_unknown_keys,
277+
),
250278
)
251279

252280

tests/clientlib_test.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import unicode_literals
22

3+
import logging
4+
35
import cfgv
46
import pytest
57

@@ -116,6 +118,48 @@ def test_validate_config_old_list_format_ok(tmpdir):
116118
assert not validate_config_main((f.strpath,))
117119

118120

121+
def test_validate_warn_on_unknown_keys_at_repo_level(tmpdir, caplog):
122+
f = tmpdir.join('cfg.yaml')
123+
f.write(
124+
'- repo: https://gitlab.com/pycqa/flake8\n'
125+
' rev: 3.7.7\n'
126+
' hooks:\n'
127+
' - id: flake8\n'
128+
' args: [--some-args]\n',
129+
)
130+
ret_val = validate_config_main((f.strpath,))
131+
assert not ret_val
132+
assert caplog.record_tuples == [
133+
(
134+
'pre_commit',
135+
logging.WARNING,
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',
159+
),
160+
]
161+
162+
119163
@pytest.mark.parametrize('fn', (validate_config_main, validate_manifest_main))
120164
def test_mains_not_ok(tmpdir, fn):
121165
not_yaml = tmpdir.join('f.notyaml')
@@ -261,3 +305,12 @@ def test_minimum_pre_commit_version_failing():
261305
def test_minimum_pre_commit_version_passing():
262306
cfg = {'repos': [], 'minimum_pre_commit_version': '0'}
263307
cfgv.validate(cfg, CONFIG_SCHEMA)
308+
309+
310+
@pytest.mark.parametrize('schema', (CONFIG_SCHEMA, CONFIG_REPO_DICT))
311+
def test_warn_additional(schema):
312+
allowed_keys = {item.key for item in schema.items if hasattr(item, 'key')}
313+
warn_additional, = [
314+
x for x in schema.items if isinstance(x, cfgv.WarnAdditionalKeys)
315+
]
316+
assert allowed_keys == set(warn_additional.keys)

0 commit comments

Comments
 (0)