Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added warning if globs are used instead of regex #1707

Merged
merged 1 commit into from Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions pre_commit/clientlib.py
Expand Up @@ -112,6 +112,18 @@ def validate_manifest_main(argv: Optional[Sequence[str]] = None) -> int:
META = 'meta'


class OptionalSensibleRegex(cfgv.OptionalNoDefault):
def check(self, dct: Dict[str, Any]) -> None:
super().check(dct)

if '/*' in dct.get(self.key, ''):
logger.warning(
f'The {self.key!r} field in hook {dct.get("id")!r} is a '
f"regex, not a glob -- matching '/*' probably isn't what you "
f'want here',
)


class MigrateShaToRev:
key = 'rev'

Expand Down Expand Up @@ -227,6 +239,8 @@ def warn_unknown_keys_repo(
for item in MANIFEST_HOOK_DICT.items
if item.key != 'id'
),
OptionalSensibleRegex('files', cfgv.check_string),
OptionalSensibleRegex('exclude', cfgv.check_string),
)
CONFIG_REPO_DICT = cfgv.Map(
'Repository', 'repo',
Expand Down
17 changes: 17 additions & 0 deletions tests/clientlib_test.py
Expand Up @@ -166,6 +166,23 @@ def test_validate_warn_on_unknown_keys_at_top_level(tmpdir, caplog):
]


def test_validate_optional_sensible_regex(caplog):
config_obj = {
'id': 'flake8',
'files': 'dir/*.py',
}
cfgv.validate(config_obj, CONFIG_HOOK_DICT)

assert caplog.record_tuples == [
(
'pre_commit',
logging.WARNING,
"The 'files' field in hook 'flake8' is a regex, not a glob -- "
"matching '/*' probably isn't what you want here",
),
]


@pytest.mark.parametrize('fn', (validate_config_main, validate_manifest_main))
def test_mains_not_ok(tmpdir, fn):
not_yaml = tmpdir.join('f.notyaml')
Expand Down