mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Implement check-files-matches-any meta hook
This commit is contained in:
parent
8df11ee7aa
commit
8a0dd01c7e
3 changed files with 76 additions and 0 deletions
36
pre_commit/meta_hooks/check_files_matches_any.py
Normal file
36
pre_commit/meta_hooks/check_files_matches_any.py
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import pre_commit.constants as C
|
||||||
|
from pre_commit.clientlib import load_config
|
||||||
|
from pre_commit.git import get_all_files
|
||||||
|
|
||||||
|
|
||||||
|
def files_matches_any(filenames, include):
|
||||||
|
include_re = re.compile(include)
|
||||||
|
for filename in filenames:
|
||||||
|
if include_re.search(filename):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def check_files_matches_any(config_file=None):
|
||||||
|
config = load_config(config_file or C.CONFIG_FILE)
|
||||||
|
files = get_all_files()
|
||||||
|
files_not_matched = False
|
||||||
|
|
||||||
|
for repo in config['repos']:
|
||||||
|
for hook in repo['hooks']:
|
||||||
|
include = hook.get('files', '')
|
||||||
|
if include and not files_matches_any(files, include):
|
||||||
|
print(
|
||||||
|
'The files pattern for {} does not match any files'
|
||||||
|
.format(hook['id'])
|
||||||
|
)
|
||||||
|
files_not_matched = True
|
||||||
|
|
||||||
|
return files_not_matched
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(check_files_matches_any())
|
||||||
|
|
@ -259,6 +259,13 @@ class MetaRepository(LocalRepository):
|
||||||
'entry': pipes.quote(sys.executable),
|
'entry': pipes.quote(sys.executable),
|
||||||
'args': ['-m', 'pre_commit.meta_hooks.check_useless_excludes'],
|
'args': ['-m', 'pre_commit.meta_hooks.check_useless_excludes'],
|
||||||
},
|
},
|
||||||
|
'check-files-matches-any': {
|
||||||
|
'name': 'Check hooks match any files',
|
||||||
|
'files': '.pre-commit-config.yaml',
|
||||||
|
'language': 'system',
|
||||||
|
'entry': pipes.quote(sys.executable),
|
||||||
|
'args': ['-m', 'pre_commit.meta_hooks.check_files_matches_any'],
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
|
|
|
||||||
|
|
@ -735,6 +735,39 @@ def test_useless_exclude_for_hook(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_files_match_any(
|
||||||
|
cap_out, repo_with_passing_hook, mock_out_store_directory,
|
||||||
|
):
|
||||||
|
config = OrderedDict((
|
||||||
|
('repo', 'meta'),
|
||||||
|
(
|
||||||
|
'hooks', (
|
||||||
|
OrderedDict((
|
||||||
|
('id', 'check-files-matches-any'),
|
||||||
|
)),
|
||||||
|
OrderedDict((
|
||||||
|
('id', 'check-useless-excludes'),
|
||||||
|
('files', 'foo'),
|
||||||
|
)),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
))
|
||||||
|
add_config_to_repo(repo_with_passing_hook, config)
|
||||||
|
|
||||||
|
_test_run(
|
||||||
|
cap_out,
|
||||||
|
repo_with_passing_hook,
|
||||||
|
opts={'all_files': True},
|
||||||
|
expected_outputs=[
|
||||||
|
b'Check hooks match any files',
|
||||||
|
b'The files pattern for check-useless-excludes '
|
||||||
|
b'does not match any files',
|
||||||
|
],
|
||||||
|
expected_ret=1,
|
||||||
|
stage=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
def modified_config_repo(repo_with_passing_hook):
|
def modified_config_repo(repo_with_passing_hook):
|
||||||
with modify_config(repo_with_passing_hook, commit=False) as config:
|
with modify_config(repo_with_passing_hook, commit=False) as config:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue