-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathtest_hook_file_matches.py
More file actions
215 lines (193 loc) · 5.49 KB
/
test_hook_file_matches.py
File metadata and controls
215 lines (193 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from __future__ import annotations
import pathlib
import re
import pytest
import ruamel.yaml
from identify.identify import tags_from_filename
yaml = ruamel.yaml.YAML(typ="safe")
HERE = pathlib.Path(__file__).parent
def check_pattern_match(
pattern: str | re.Pattern, value: str, *, should_match: bool = True
) -> None:
__tracebackhide__ = True
if not isinstance(pattern, re.Pattern):
pattern = re.compile(pattern)
matches = bool(pattern.fullmatch(value))
if matches == should_match:
return
if should_match:
pytest.fail(f"'{pattern}' did not match '{value}' (expected match)")
else:
pytest.fail(f"'{pattern}' matched '{value}' (expected no match)")
def check_types_match(
types: list[str],
types_or: list[str],
path: str,
) -> None:
__tracebackhide__ = True
tags = tags_from_filename(path)
if types and not tags.issuperset(types):
pytest.fail(f"types={types} did not match '{path}' (expected match)")
if types_or and not tags.intersection(types_or):
pytest.fail(f"types_or={types_or} did not match '{path}' (expected match)")
def get_hook_config(hookid):
config_file = HERE.parent.parent / ".pre-commit-hooks.yaml"
with open(config_file) as fp:
for hook in yaml.load(fp):
if hook["id"] == hookid:
return hook
else:
raise LookupError(f"could not find hook with id={hookid}")
_HOOKID_PATH_MAP = {
"check-azure-pipelines": {
"good": (
"azure-pipelines.yml",
"azure-pipelines.yaml",
".azure-pipelines.yml",
".azure-pipelines.yaml",
),
"bad": (
"foo.yml",
"foo/azure-pipelines.yaml",
),
},
"check-bamboo-spec": {
"good": (
"bamboo-specs/foo.yml",
"bamboo-specs/foo.yaml",
),
"bad": (
"bamboo-specs.yaml",
"bamboo-spec/foo.yml",
"bamboo-specs/README.md",
),
},
"check-compose-spec": {
"good": (
"compose.yml",
"compose.yaml",
"docker-compose.yml",
"docker-compose.yaml",
"compose.override.yml",
"docker-compose.override.yml",
"path/to/compose.yml",
),
"bad": (
"docker.compose.yml",
"docker.md",
"Dockerfile",
),
},
"check-meltano": {
"good": (
"meltano.yml",
"data/meltano.yml",
"extractors.meltano.yml",
"meltano-manifest.json",
"meltano-manifest.prod.json",
),
"bad": (
"meltano.yaml",
"meltano.yml.md",
"meltano-manifest.yml",
),
},
"check-dependabot": {
"good": (".github/dependabot.yml", ".github/dependabot.yaml"),
"bad": (".dependabot.yaml", ".dependabot.yml"),
},
"check-github-actions": {
"good": (
"action.yaml",
".github/actions/action.yml",
".github/actions/foo/bar/action.yaml",
".github/actions/path with spaces/action.yml",
),
"bad": (".github/actions/foo/other.yaml",),
},
"check-github-workflows": {
"good": (
".github/workflows/build.yml",
".github/workflows/build.yaml",
),
"bad": (
".github/workflows.yaml",
".github/workflows/foo/bar.yaml",
),
},
"check-gitlab-ci": {
"good": (
".gitlab-ci.yml",
".gitlab/.gitlab-ci.yml",
"gitlab/.gitlab-ci.yml",
),
"bad": (
".gitlab-ci.yaml",
"gitlab-ci.yml",
".gitlab/gitlab-ci.yml",
"gitlab/gitlab-ci.yml",
),
},
"check-readthedocs": {
"good": (
".readthedocs.yml",
".readthedocs.yaml",
),
"bad": (
"readthedocs.yml",
"readthedocs.yaml",
),
},
"check-renovate": {
"good": (
"renovate.json",
"renovate.json5",
".github/renovate.json",
".gitlab/renovate.json5",
".renovaterc",
".renovaterc.json",
),
"bad": (
".github/renovaterc",
".renovate",
".renovate.json",
),
},
"check-snapcraft": {
"good": ("snapcraft.yaml", "snap/snapcraft.yaml", "foo/bar/snapcraft.yaml"),
"bad": ("snapcraft.yml", "snap.yaml", "snapcraft"),
},
"check-travis": {
"good": (
".travis.yml",
".travis.yaml",
),
"bad": (
"travis.yml",
".travis",
),
},
}
@pytest.mark.parametrize(
"hookid, filepath",
[
(hookid, path)
for (hookid, pathlist) in _HOOKID_PATH_MAP.items()
for path in pathlist.get("good", ())
],
)
def test_hook_matches_known_good_paths(hookid, filepath):
config = get_hook_config(hookid)
check_pattern_match(config["files"], filepath)
check_types_match(config.get("types", []), config.get("types_or", []), filepath)
@pytest.mark.parametrize(
"hookid, filepath",
[
(hookid, path)
for (hookid, pathlist) in _HOOKID_PATH_MAP.items()
for path in pathlist.get("bad", ())
],
)
def test_hook_does_not_matches_known_bad_paths(hookid, filepath):
config = get_hook_config(hookid)
check_pattern_match(config["files"], filepath, should_match=False)