Skip to content

Commit ef7b126

Browse files
authored
Merge pull request pre-commit#2053 from radek-sprta/master
Add warning for regular expression with [\/] (pre-commit#2043)
2 parents 530dbe6 + cef9c4a commit ef7b126

File tree

2 files changed

+69
-19
lines changed

2 files changed

+69
-19
lines changed

pre_commit/clientlib.py

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

147159

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

158182

159183
class MigrateShaToRev:

tests/clientlib_test.py

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -247,38 +247,64 @@ def test_warn_mutable_rev_conditional():
247247
cfgv.validate(config_obj, CONFIG_REPO_DICT)
248248

249249

250-
def test_validate_optional_sensible_regex_at_hook_level(caplog):
250+
@pytest.mark.parametrize(
251+
('regex', 'warning'),
252+
(
253+
(
254+
r'dir/*.py',
255+
"The 'files' field in hook 'flake8' is a regex, not a glob -- "
256+
"matching '/*' probably isn't what you want here",
257+
),
258+
(
259+
r'dir[\/].*\.py',
260+
r"pre-commit normalizes slashes in the 'files' field in hook "
261+
r"'flake8' to forward slashes, so you can use / instead of [\/]",
262+
),
263+
(
264+
r'dir[/\\].*\.py',
265+
r"pre-commit normalizes slashes in the 'files' field in hook "
266+
r"'flake8' to forward slashes, so you can use / instead of [/\\]",
267+
),
268+
),
269+
)
270+
def test_validate_optional_sensible_regex_at_hook(caplog, regex, warning):
251271
config_obj = {
252272
'id': 'flake8',
253-
'files': 'dir/*.py',
273+
'files': regex,
254274
}
255275
cfgv.validate(config_obj, CONFIG_HOOK_DICT)
256276

257-
assert caplog.record_tuples == [
277+
assert caplog.record_tuples == [('pre_commit', logging.WARNING, warning)]
278+
279+
280+
@pytest.mark.parametrize(
281+
('regex', 'warning'),
282+
(
258283
(
259-
'pre_commit',
260-
logging.WARNING,
261-
"The 'files' field in hook 'flake8' is a regex, not a glob -- "
284+
r'dir/*.py',
285+
"The top-level 'files' field is a regex, not a glob -- "
262286
"matching '/*' probably isn't what you want here",
263287
),
264-
]
265-
266-
267-
def test_validate_optional_sensible_regex_at_top_level(caplog):
288+
(
289+
r'dir[\/].*\.py',
290+
r"pre-commit normalizes the slashes in the top-level 'files' "
291+
r'field to forward slashes, so you can use / instead of [\/]',
292+
),
293+
(
294+
r'dir[/\\].*\.py',
295+
r"pre-commit normalizes the slashes in the top-level 'files' "
296+
r'field to forward slashes, so you can use / instead of [/\\]',
297+
),
298+
),
299+
)
300+
def test_validate_optional_sensible_regex_at_top_level(caplog, regex, warning):
268301
config_obj = {
269-
'files': 'dir/*.py',
302+
'files': regex,
270303
'repos': [],
271304
}
272305
cfgv.validate(config_obj, CONFIG_SCHEMA)
273306

274-
assert caplog.record_tuples == [
275-
(
276-
'pre_commit',
277-
logging.WARNING,
278-
"The top-level 'files' field is a regex, not a glob -- matching "
279-
"'/*' probably isn't what you want here",
280-
),
281-
]
307+
assert caplog.record_tuples == [('pre_commit', logging.WARNING, warning)]
282308

283309

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

0 commit comments

Comments
 (0)