Skip to content

Commit 1b496c5

Browse files
committed
Fix check-useless-exclude to consider types filter
1 parent 0831910 commit 1b496c5

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

pre_commit/meta_hooks/check_useless_excludes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pre_commit import git
1010
from pre_commit.clientlib import load_config
1111
from pre_commit.clientlib import MANIFEST_HOOK_DICT
12+
from pre_commit.commands.run import _filter_by_types
1213

1314

1415
def exclude_matches_any(filenames, include, exclude):
@@ -39,8 +40,10 @@ def check_useless_excludes(config_file):
3940
# Not actually a manifest dict, but this more accurately reflects
4041
# the defaults applied during runtime
4142
hook = apply_defaults(hook, MANIFEST_HOOK_DICT)
43+
types, exclude_types = hook['types'], hook['exclude_types']
44+
filtered_by_types = _filter_by_types(files, types, exclude_types)
4245
include, exclude = hook['files'], hook['exclude']
43-
if not exclude_matches_any(files, include, exclude):
46+
if not exclude_matches_any(filtered_by_types, include, exclude):
4447
print(
4548
'The exclude pattern {!r} for {} does not match any files'
4649
.format(exclude, hook['id']),

tests/meta_hooks/check_useless_excludes_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,37 @@ def test_useless_exclude_for_hook(capsys, tempdir_factory):
5151
assert expected == out
5252

5353

54+
def test_useless_exclude_with_types_filter(capsys, tempdir_factory):
55+
config = {
56+
'repos': [
57+
{
58+
'repo': 'meta',
59+
'hooks': [
60+
{
61+
'id': 'check-useless-excludes',
62+
'exclude': '.pre-commit-config.yaml',
63+
'types': ['python'],
64+
},
65+
],
66+
},
67+
],
68+
}
69+
70+
repo = git_dir(tempdir_factory)
71+
add_config_to_repo(repo, config)
72+
73+
with cwd(repo):
74+
assert check_useless_excludes.main(()) == 1
75+
76+
out, _ = capsys.readouterr()
77+
out = out.strip()
78+
expected = (
79+
"The exclude pattern '.pre-commit-config.yaml' for "
80+
"check-useless-excludes does not match any files"
81+
)
82+
assert expected == out
83+
84+
5485
def test_no_excludes(capsys, tempdir_factory):
5586
config = {
5687
'repos': [

0 commit comments

Comments
 (0)