mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Merge pull request #2053 from radek-sprta/master
Add warning for regular expression with [\/] (#2043)
This commit is contained in:
commit
ef7b126ee3
2 changed files with 73 additions and 23 deletions
|
|
@ -143,6 +143,18 @@ class OptionalSensibleRegexAtHook(cfgv.OptionalNoDefault):
|
||||||
f"regex, not a glob -- matching '/*' probably isn't what you "
|
f"regex, not a glob -- matching '/*' probably isn't what you "
|
||||||
f'want here',
|
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):
|
class OptionalSensibleRegexAtTop(cfgv.OptionalNoDefault):
|
||||||
|
|
@ -154,6 +166,18 @@ class OptionalSensibleRegexAtTop(cfgv.OptionalNoDefault):
|
||||||
f'The top-level {self.key!r} field is a regex, not a glob -- '
|
f'The top-level {self.key!r} field is a regex, not a glob -- '
|
||||||
f"matching '/*' probably isn't what you want here",
|
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:
|
class MigrateShaToRev:
|
||||||
|
|
|
||||||
|
|
@ -247,38 +247,64 @@ def test_warn_mutable_rev_conditional():
|
||||||
cfgv.validate(config_obj, CONFIG_REPO_DICT)
|
cfgv.validate(config_obj, CONFIG_REPO_DICT)
|
||||||
|
|
||||||
|
|
||||||
def test_validate_optional_sensible_regex_at_hook_level(caplog):
|
@pytest.mark.parametrize(
|
||||||
config_obj = {
|
('regex', 'warning'),
|
||||||
'id': 'flake8',
|
(
|
||||||
'files': 'dir/*.py',
|
|
||||||
}
|
|
||||||
cfgv.validate(config_obj, CONFIG_HOOK_DICT)
|
|
||||||
|
|
||||||
assert caplog.record_tuples == [
|
|
||||||
(
|
(
|
||||||
'pre_commit',
|
r'dir/*.py',
|
||||||
logging.WARNING,
|
|
||||||
"The 'files' field in hook 'flake8' is a regex, not a glob -- "
|
"The 'files' field in hook 'flake8' is a regex, not a glob -- "
|
||||||
"matching '/*' probably isn't what you want here",
|
"matching '/*' probably isn't what you want here",
|
||||||
),
|
),
|
||||||
]
|
(
|
||||||
|
r'dir[\/].*\.py',
|
||||||
|
r"pre-commit normalizes slashes in the 'files' field in hook "
|
||||||
def test_validate_optional_sensible_regex_at_top_level(caplog):
|
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 = {
|
config_obj = {
|
||||||
'files': 'dir/*.py',
|
'id': 'flake8',
|
||||||
|
'files': regex,
|
||||||
|
}
|
||||||
|
cfgv.validate(config_obj, CONFIG_HOOK_DICT)
|
||||||
|
|
||||||
|
assert caplog.record_tuples == [('pre_commit', logging.WARNING, warning)]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
('regex', 'warning'),
|
||||||
|
(
|
||||||
|
(
|
||||||
|
r'dir/*.py',
|
||||||
|
"The top-level 'files' field is a regex, not a glob -- "
|
||||||
|
"matching '/*' probably isn't what you want here",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
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': regex,
|
||||||
'repos': [],
|
'repos': [],
|
||||||
}
|
}
|
||||||
cfgv.validate(config_obj, CONFIG_SCHEMA)
|
cfgv.validate(config_obj, CONFIG_SCHEMA)
|
||||||
|
|
||||||
assert caplog.record_tuples == [
|
assert caplog.record_tuples == [('pre_commit', logging.WARNING, warning)]
|
||||||
(
|
|
||||||
'pre_commit',
|
|
||||||
logging.WARNING,
|
|
||||||
"The top-level 'files' field is a regex, not a glob -- matching "
|
|
||||||
"'/*' probably isn't what you want here",
|
|
||||||
),
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('fn', (validate_config_main, validate_manifest_main))
|
@pytest.mark.parametrize('fn', (validate_config_main, validate_manifest_main))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue