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

Skip to content

Commit 610716d

Browse files
paulhfischerasottile
authored andcommitted
added warning if globs are used instead of regex
1 parent 6e37f19 commit 610716d

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

pre_commit/clientlib.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ def validate_manifest_main(argv: Optional[Sequence[str]] = None) -> int:
112112
META = 'meta'
113113

114114

115+
class OptionalSensibleRegex(cfgv.OptionalNoDefault):
116+
def check(self, dct: Dict[str, Any]) -> None:
117+
super().check(dct)
118+
119+
if '/*' in dct.get(self.key, ''):
120+
logger.warning(
121+
f'The {self.key!r} field in hook {dct.get("id")!r} is a '
122+
f"regex, not a glob -- matching '/*' probably isn't what you "
123+
f'want here',
124+
)
125+
126+
115127
class MigrateShaToRev:
116128
key = 'rev'
117129

@@ -227,6 +239,8 @@ def warn_unknown_keys_repo(
227239
for item in MANIFEST_HOOK_DICT.items
228240
if item.key != 'id'
229241
),
242+
OptionalSensibleRegex('files', cfgv.check_string),
243+
OptionalSensibleRegex('exclude', cfgv.check_string),
230244
)
231245
CONFIG_REPO_DICT = cfgv.Map(
232246
'Repository', 'repo',

tests/clientlib_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,23 @@ def test_validate_warn_on_unknown_keys_at_top_level(tmpdir, caplog):
166166
]
167167

168168

169+
def test_validate_optional_sensible_regex(caplog):
170+
config_obj = {
171+
'id': 'flake8',
172+
'files': 'dir/*.py',
173+
}
174+
cfgv.validate(config_obj, CONFIG_HOOK_DICT)
175+
176+
assert caplog.record_tuples == [
177+
(
178+
'pre_commit',
179+
logging.WARNING,
180+
"The 'files' field in hook 'flake8' is a regex, not a glob -- "
181+
"matching '/*' probably isn't what you want here",
182+
),
183+
]
184+
185+
169186
@pytest.mark.parametrize('fn', (validate_config_main, validate_manifest_main))
170187
def test_mains_not_ok(tmpdir, fn):
171188
not_yaml = tmpdir.join('f.notyaml')

0 commit comments

Comments
 (0)