Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a2bd009

Browse files
authored
Merge pull request #821 from pre-commit/allow_language_fail_check_useless_excludes
Exempt `language: fail` hooks from check-hooks-apply
2 parents cd11697 + ce25b65 commit a2bd009

3 files changed

Lines changed: 140 additions & 117 deletions

File tree

pre_commit/meta_hooks/check_hooks_apply.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def check_all_hooks_match_files(config_file):
1515

1616
for repo in repositories(load_config(config_file), Store()):
1717
for hook_id, hook in repo.hooks:
18-
if hook['always_run']:
18+
if hook['always_run'] or hook['language'] == 'fail':
1919
continue
2020
include, exclude = hook['files'], hook['exclude']
2121
filtered = _filter_by_include_exclude(files, include, exclude)

tests/meta_hooks/check_hooks_apply_test.py

Lines changed: 97 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
from collections import OrderedDict
2-
31
from pre_commit.meta_hooks import check_hooks_apply
42
from testing.fixtures import add_config_to_repo
53
from testing.fixtures import git_dir
64
from testing.util import cwd
75

86

97
def test_hook_excludes_everything(capsys, tempdir_factory, mock_store_dir):
10-
config = OrderedDict((
11-
('repo', 'meta'),
12-
(
13-
'hooks', (
14-
OrderedDict((
15-
('id', 'check-useless-excludes'),
16-
('exclude', '.pre-commit-config.yaml'),
17-
)),
18-
),
19-
),
20-
))
8+
config = {
9+
'repos': [
10+
{
11+
'repo': 'meta',
12+
'hooks': [
13+
{
14+
'id': 'check-useless-excludes',
15+
'exclude': '.pre-commit-config.yaml',
16+
},
17+
],
18+
},
19+
],
20+
}
2121

2222
repo = git_dir(tempdir_factory)
2323
add_config_to_repo(repo, config)
@@ -30,17 +30,19 @@ def test_hook_excludes_everything(capsys, tempdir_factory, mock_store_dir):
3030

3131

3232
def test_hook_includes_nothing(capsys, tempdir_factory, mock_store_dir):
33-
config = OrderedDict((
34-
('repo', 'meta'),
35-
(
36-
'hooks', (
37-
OrderedDict((
38-
('id', 'check-useless-excludes'),
39-
('files', 'foo'),
40-
)),
41-
),
42-
),
43-
))
33+
config = {
34+
'repos': [
35+
{
36+
'repo': 'meta',
37+
'hooks': [
38+
{
39+
'id': 'check-useless-excludes',
40+
'files': 'foo',
41+
},
42+
],
43+
},
44+
],
45+
}
4446

4547
repo = git_dir(tempdir_factory)
4648
add_config_to_repo(repo, config)
@@ -53,17 +55,19 @@ def test_hook_includes_nothing(capsys, tempdir_factory, mock_store_dir):
5355

5456

5557
def test_hook_types_not_matched(capsys, tempdir_factory, mock_store_dir):
56-
config = OrderedDict((
57-
('repo', 'meta'),
58-
(
59-
'hooks', (
60-
OrderedDict((
61-
('id', 'check-useless-excludes'),
62-
('types', ['python']),
63-
)),
64-
),
65-
),
66-
))
58+
config = {
59+
'repos': [
60+
{
61+
'repo': 'meta',
62+
'hooks': [
63+
{
64+
'id': 'check-useless-excludes',
65+
'types': ['python'],
66+
},
67+
],
68+
},
69+
],
70+
}
6771

6872
repo = git_dir(tempdir_factory)
6973
add_config_to_repo(repo, config)
@@ -78,17 +82,19 @@ def test_hook_types_not_matched(capsys, tempdir_factory, mock_store_dir):
7882
def test_hook_types_excludes_everything(
7983
capsys, tempdir_factory, mock_store_dir,
8084
):
81-
config = OrderedDict((
82-
('repo', 'meta'),
83-
(
84-
'hooks', (
85-
OrderedDict((
86-
('id', 'check-useless-excludes'),
87-
('exclude_types', ['yaml']),
88-
)),
89-
),
90-
),
91-
))
85+
config = {
86+
'repos': [
87+
{
88+
'repo': 'meta',
89+
'hooks': [
90+
{
91+
'id': 'check-useless-excludes',
92+
'exclude_types': ['yaml'],
93+
},
94+
],
95+
},
96+
],
97+
}
9298

9399
repo = git_dir(tempdir_factory)
94100
add_config_to_repo(repo, config)
@@ -100,23 +106,51 @@ def test_hook_types_excludes_everything(
100106
assert 'check-useless-excludes does not apply to this repository' in out
101107

102108

103-
def test_valid_includes(capsys, tempdir_factory, mock_store_dir):
104-
config = OrderedDict((
105-
('repo', 'meta'),
106-
(
107-
'hooks', (
108-
OrderedDict((
109-
('id', 'check-useless-excludes'),
110-
)),
111-
# Should not be reported as an error due to always_run
112-
OrderedDict((
113-
('id', 'check-useless-excludes'),
114-
('files', '^$'),
115-
('always_run', True),
116-
)),
117-
),
118-
),
119-
))
109+
def test_valid_always_run(capsys, tempdir_factory, mock_store_dir):
110+
config = {
111+
'repos': [
112+
{
113+
'repo': 'meta',
114+
'hooks': [
115+
# Should not be reported as an error due to always_run
116+
{
117+
'id': 'check-useless-excludes',
118+
'files': '^$',
119+
'always_run': True,
120+
},
121+
],
122+
},
123+
],
124+
}
125+
126+
repo = git_dir(tempdir_factory)
127+
add_config_to_repo(repo, config)
128+
129+
with cwd(repo):
130+
assert check_hooks_apply.main(()) == 0
131+
132+
out, _ = capsys.readouterr()
133+
assert out == ''
134+
135+
136+
def test_valid_language_fail(capsys, tempdir_factory, mock_store_dir):
137+
config = {
138+
'repos': [
139+
{
140+
'repo': 'local',
141+
'hooks': [
142+
# Should not be reported as an error due to language: fail
143+
{
144+
'id': 'changelogs-rst',
145+
'name': 'changelogs must be rst',
146+
'entry': 'changelog filenames must end in .rst',
147+
'language': 'fail',
148+
'files': r'changelog/.*(?<!\.rst)$',
149+
},
150+
],
151+
},
152+
],
153+
}
120154

121155
repo = git_dir(tempdir_factory)
122156
add_config_to_repo(repo, config)

tests/meta_hooks/useless_excludes_test.py renamed to tests/meta_hooks/check_useless_excludes_test.py

Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
1-
from collections import OrderedDict
2-
31
from pre_commit.meta_hooks import check_useless_excludes
42
from testing.fixtures import add_config_to_repo
53
from testing.fixtures import git_dir
64
from testing.util import cwd
75

86

97
def test_useless_exclude_global(capsys, tempdir_factory):
10-
config = OrderedDict((
11-
('exclude', 'foo'),
12-
(
13-
'repos', [
14-
OrderedDict((
15-
('repo', 'meta'),
16-
(
17-
'hooks', (
18-
OrderedDict((
19-
('id', 'check-useless-excludes'),
20-
)),
21-
),
22-
),
23-
)),
24-
],
25-
),
26-
))
8+
config = {
9+
'exclude': 'foo',
10+
'repos': [
11+
{
12+
'repo': 'meta',
13+
'hooks': [{'id': 'check-useless-excludes'}],
14+
},
15+
],
16+
}
2717

2818
repo = git_dir(tempdir_factory)
2919
add_config_to_repo(repo, config)
@@ -32,21 +22,19 @@ def test_useless_exclude_global(capsys, tempdir_factory):
3222
assert check_useless_excludes.main(()) == 1
3323

3424
out, _ = capsys.readouterr()
35-
assert "The global exclude pattern 'foo' does not match any files" in out
25+
out = out.strip()
26+
assert "The global exclude pattern 'foo' does not match any files" == out
3627

3728

3829
def test_useless_exclude_for_hook(capsys, tempdir_factory):
39-
config = OrderedDict((
40-
('repo', 'meta'),
41-
(
42-
'hooks', (
43-
OrderedDict((
44-
('id', 'check-useless-excludes'),
45-
('exclude', 'foo'),
46-
)),
47-
),
48-
),
49-
))
30+
config = {
31+
'repos': [
32+
{
33+
'repo': 'meta',
34+
'hooks': [{'id': 'check-useless-excludes', 'exclude': 'foo'}],
35+
},
36+
],
37+
}
5038

5139
repo = git_dir(tempdir_factory)
5240
add_config_to_repo(repo, config)
@@ -55,24 +43,23 @@ def test_useless_exclude_for_hook(capsys, tempdir_factory):
5543
assert check_useless_excludes.main(()) == 1
5644

5745
out, _ = capsys.readouterr()
46+
out = out.strip()
5847
expected = (
5948
"The exclude pattern 'foo' for check-useless-excludes "
6049
"does not match any files"
6150
)
62-
assert expected in out
51+
assert expected == out
6352

6453

6554
def test_no_excludes(capsys, tempdir_factory):
66-
config = OrderedDict((
67-
('repo', 'meta'),
68-
(
69-
'hooks', (
70-
OrderedDict((
71-
('id', 'check-useless-excludes'),
72-
)),
73-
),
74-
),
75-
))
55+
config = {
56+
'repos': [
57+
{
58+
'repo': 'meta',
59+
'hooks': [{'id': 'check-useless-excludes'}],
60+
},
61+
],
62+
}
7663

7764
repo = git_dir(tempdir_factory)
7865
add_config_to_repo(repo, config)
@@ -85,17 +72,19 @@ def test_no_excludes(capsys, tempdir_factory):
8572

8673

8774
def test_valid_exclude(capsys, tempdir_factory):
88-
config = OrderedDict((
89-
('repo', 'meta'),
90-
(
91-
'hooks', (
92-
OrderedDict((
93-
('id', 'check-useless-excludes'),
94-
('exclude', '.pre-commit-config.yaml'),
95-
)),
96-
),
97-
),
98-
))
75+
config = {
76+
'repos': [
77+
{
78+
'repo': 'meta',
79+
'hooks': [
80+
{
81+
'id': 'check-useless-excludes',
82+
'exclude': '.pre-commit-config.yaml',
83+
},
84+
],
85+
},
86+
],
87+
}
9988

10089
repo = git_dir(tempdir_factory)
10190
add_config_to_repo(repo, config)

0 commit comments

Comments
 (0)