Skip to content

Commit d4ffa5b

Browse files
committed
fix: Add missing warning for regular expression with [\\/]
test: Test case parameters for said regular expression refactor: For-loop for regex warnings instead of multiple if statements resolves pre-commit#2151
1 parent a737d5f commit d4ffa5b

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

pre_commit/clientlib.py

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,13 @@ def check(self, dct: Dict[str, Any]) -> None:
144144
f"regex, not a glob -- matching '/*' probably isn't what you "
145145
f'want here',
146146
)
147-
if r'[\/]' in dct.get(self.key, ''):
148-
logger.warning(
149-
fr'pre-commit normalizes slashes in the {self.key!r} field '
150-
fr'in hook {dct.get("id")!r} to forward slashes, so you '
151-
fr'can use / instead of [\/]',
152-
)
153-
if r'[/\\]' in dct.get(self.key, ''):
154-
logger.warning(
155-
fr'pre-commit normalizes slashes in the {self.key!r} field '
156-
fr'in hook {dct.get("id")!r} to forward slashes, so you '
157-
fr'can use / instead of [/\\]',
158-
)
147+
for fwd_slash_re in [r'[\\/]', r'[\/]', r'[/\\]']:
148+
if fwd_slash_re in dct.get(self.key, ''):
149+
logger.warning(
150+
fr'pre-commit normalizes slashes in the {self.key!r} '
151+
fr'field in hook {dct.get("id")!r} to forward slashes, '
152+
fr'so you can use / instead of {fwd_slash_re}',
153+
)
159154

160155

161156
class OptionalSensibleRegexAtTop(cfgv.OptionalNoDefault):
@@ -167,18 +162,13 @@ def check(self, dct: Dict[str, Any]) -> None:
167162
f'The top-level {self.key!r} field is a regex, not a glob -- '
168163
f"matching '/*' probably isn't what you want here",
169164
)
170-
if r'[\/]' in dct.get(self.key, ''):
171-
logger.warning(
172-
fr'pre-commit normalizes the slashes in the top-level '
173-
fr'{self.key!r} field to forward slashes, so you can use / '
174-
fr'instead of [\/]',
175-
)
176-
if r'[/\\]' in dct.get(self.key, ''):
177-
logger.warning(
178-
fr'pre-commit normalizes the slashes in the top-level '
179-
fr'{self.key!r} field to forward slashes, so you can use / '
180-
fr'instead of [/\\]',
181-
)
165+
for fwd_slash_re in [r'[\\/]', r'[\/]', r'[/\\]']:
166+
if fwd_slash_re in dct.get(self.key, ''):
167+
logger.warning(
168+
fr'pre-commit normalizes the slashes in the top-level '
169+
fr'{self.key!r} field to forward slashes, so you '
170+
fr'can use / instead of {fwd_slash_re}',
171+
)
182172

183173

184174
class MigrateShaToRev:

tests/clientlib_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ def test_warn_mutable_rev_conditional():
265265
r"pre-commit normalizes slashes in the 'files' field in hook "
266266
r"'flake8' to forward slashes, so you can use / instead of [/\\]",
267267
),
268+
(
269+
r'dir[\\/].*\.py',
270+
r"pre-commit normalizes slashes in the 'files' field in hook "
271+
r"'flake8' to forward slashes, so you can use / instead of [\\/]",
272+
),
268273
),
269274
)
270275
def test_validate_optional_sensible_regex_at_hook(caplog, regex, warning):
@@ -295,6 +300,11 @@ def test_validate_optional_sensible_regex_at_hook(caplog, regex, warning):
295300
r"pre-commit normalizes the slashes in the top-level 'files' "
296301
r'field to forward slashes, so you can use / instead of [/\\]',
297302
),
303+
(
304+
r'dir[\\/].*\.py',
305+
r"pre-commit normalizes the slashes in the top-level 'files' "
306+
r'field to forward slashes, so you can use / instead of [\\/]',
307+
),
298308
),
299309
)
300310
def test_validate_optional_sensible_regex_at_top_level(caplog, regex, warning):

0 commit comments

Comments
 (0)