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

Add warning for regular expression with [\/] (#2043) #2053

Merged
merged 1 commit into from Sep 22, 2021
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
24 changes: 24 additions & 0 deletions pre_commit/clientlib.py
Expand Up @@ -143,6 +143,18 @@ def check(self, dct: Dict[str, Any]) -> None:
f"regex, not a glob -- matching '/*' probably isn't what you "
f'want here',
)
if r'[\/]' in dct.get(self.key, ''):
logger.warning(
fr'pre-commit normalizes slashes in the {self.key!r} field '
fr'in hook {dct.get("id")!r} to forward slashes, so you '
fr'can use / instead of [\/]',
)
if r'[/\\]' in dct.get(self.key, ''):
logger.warning(
fr'pre-commit normalizes slashes in the {self.key!r} field '
fr'in hook {dct.get("id")!r} to forward slashes, so you '
fr'can use / instead of [/\\]',
)


class OptionalSensibleRegexAtTop(cfgv.OptionalNoDefault):
Expand All @@ -154,6 +166,18 @@ def check(self, dct: Dict[str, Any]) -> None:
f'The top-level {self.key!r} field is a regex, not a glob -- '
f"matching '/*' probably isn't what you want here",
)
if r'[\/]' in dct.get(self.key, ''):
logger.warning(
fr'pre-commit normalizes the slashes in the top-level '
fr'{self.key!r} field to forward slashes, so you can use / '
fr'instead of [\/]',
)
if r'[/\\]' in dct.get(self.key, ''):
logger.warning(
fr'pre-commit normalizes the slashes in the top-level '
fr'{self.key!r} field to forward slashes, so you can use / '
fr'instead of [/\\]',
)


class MigrateShaToRev:
Expand Down
64 changes: 45 additions & 19 deletions tests/clientlib_test.py
Expand Up @@ -247,38 +247,64 @@ def test_warn_mutable_rev_conditional():
cfgv.validate(config_obj, CONFIG_REPO_DICT)


def test_validate_optional_sensible_regex_at_hook_level(caplog):
@pytest.mark.parametrize(
('regex', 'warning'),
(
(
r'dir/*.py',
"The 'files' field in hook 'flake8' is a regex, not a glob -- "
"matching '/*' probably isn't what you want here",
),
(
r'dir[\/].*\.py',
r"pre-commit normalizes slashes in the 'files' field in hook "
r"'flake8' to forward slashes, so you can use / instead of [\/]",
),
(
r'dir[/\\].*\.py',
r"pre-commit normalizes slashes in the 'files' field in hook "
r"'flake8' to forward slashes, so you can use / instead of [/\\]",
),
),
)
def test_validate_optional_sensible_regex_at_hook(caplog, regex, warning):
config_obj = {
'id': 'flake8',
'files': 'dir/*.py',
'files': regex,
}
cfgv.validate(config_obj, CONFIG_HOOK_DICT)

assert caplog.record_tuples == [
assert caplog.record_tuples == [('pre_commit', logging.WARNING, warning)]


@pytest.mark.parametrize(
('regex', 'warning'),
(
(
'pre_commit',
logging.WARNING,
"The 'files' field in hook 'flake8' is a regex, not a glob -- "
r'dir/*.py',
"The top-level 'files' field is a regex, not a glob -- "
"matching '/*' probably isn't what you want here",
),
]


def test_validate_optional_sensible_regex_at_top_level(caplog):
(
r'dir[\/].*\.py',
r"pre-commit normalizes the slashes in the top-level 'files' "
r'field to forward slashes, so you can use / instead of [\/]',
),
(
r'dir[/\\].*\.py',
r"pre-commit normalizes the slashes in the top-level 'files' "
r'field to forward slashes, so you can use / instead of [/\\]',
),
),
)
def test_validate_optional_sensible_regex_at_top_level(caplog, regex, warning):
config_obj = {
'files': 'dir/*.py',
'files': regex,
'repos': [],
}
cfgv.validate(config_obj, CONFIG_SCHEMA)

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


@pytest.mark.parametrize('fn', (validate_config_main, validate_manifest_main))
Expand Down