Skip to content

Commit

Permalink
Merge pull request #2154 from kuviokelluja/master
Browse files Browse the repository at this point in the history
fix: Add missing warning for regular expression with [\\/]
  • Loading branch information
asottile committed Dec 5, 2021
2 parents a737d5f + b5088ce commit 097f2c8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
38 changes: 14 additions & 24 deletions pre_commit/clientlib.py
Expand Up @@ -144,18 +144,13 @@ 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 [/\\]',
)
for fwd_slash_re in (r'[\\/]', r'[\/]', r'[/\\]'):
if fwd_slash_re in dct.get(self.key, ''):
logger.warning(
fr'pre-commit normalizes slashes in the {self.key!r} '
fr'field in hook {dct.get("id")!r} to forward slashes, '
fr'so you can use / instead of {fwd_slash_re}',
)


class OptionalSensibleRegexAtTop(cfgv.OptionalNoDefault):
Expand All @@ -167,18 +162,13 @@ 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 [/\\]',
)
for fwd_slash_re in (r'[\\/]', r'[\/]', r'[/\\]'):
if fwd_slash_re 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 '
fr'can use / instead of {fwd_slash_re}',
)


class MigrateShaToRev:
Expand Down
10 changes: 10 additions & 0 deletions tests/clientlib_test.py
Expand Up @@ -265,6 +265,11 @@ def test_warn_mutable_rev_conditional():
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):
Expand Down Expand Up @@ -295,6 +300,11 @@ def test_validate_optional_sensible_regex_at_hook(caplog, regex, warning):
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):
Expand Down

0 comments on commit 097f2c8

Please sign in to comment.