-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_add.py
More file actions
298 lines (263 loc) · 8.67 KB
/
test_add.py
File metadata and controls
298 lines (263 loc) · 8.67 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import re
import pytest
import blurb._add
from blurb._add import (
_blurb_template_text,
_extract_issue_number,
_extract_section_name,
)
from blurb._template import sections as SECTIONS
from blurb._template import template as blurb_template
def test_valid_no_issue_number():
assert _extract_issue_number(None) is None
res = _blurb_template_text(issue=None, section=None)
lines = frozenset(res.splitlines())
assert '.. gh-issue:' not in lines
assert '.. gh-issue: ' in lines
@pytest.mark.parametrize(
'issue',
(
# issue given by their number
'12345',
' 12345 ',
# issue given by their number and a 'GH-' prefix
'GH-12345',
' GH-12345 ',
# issue given by their number and a 'gh-' prefix
'gh-12345',
' gh-12345 ',
# issue given by their number and a '#' prefix
'#12345',
' #12345 ',
# issue given by their URL (no scheme)
'github.com/python/cpython/issues/12345',
' github.com/python/cpython/issues/12345 ',
# issue given by their URL (with scheme)
'https://github.com/python/cpython/issues/12345',
' https://github.com/python/cpython/issues/12345 ',
),
)
def test_valid_issue_number_12345(issue):
actual = _extract_issue_number(issue)
assert actual == 12345
res = _blurb_template_text(issue=issue, section=None)
lines = frozenset(res.splitlines())
assert '.. gh-issue:' not in lines
assert '.. gh-issue: ' not in lines
assert '.. gh-issue: 12345' in lines
@pytest.mark.parametrize(
'issue',
(
'',
'abc',
'Gh-123',
'gh-abc',
'gh- 123',
'gh -123',
'gh-',
'bpo-',
'bpo-12345',
'github.com/python/cpython/issues',
'github.com/python/cpython/issues/',
'github.com/python/cpython/issues/abc',
'github.com/python/cpython/issues/gh-abc',
'github.com/python/cpython/issues/gh-123',
'github.com/python/cpython/issues/1234?param=1',
'https://github.com/python/cpython/issues',
'https://github.com/python/cpython/issues/',
'https://github.com/python/cpython/issues/abc',
'https://github.com/python/cpython/issues/gh-abc',
'https://github.com/python/cpython/issues/gh-123',
'https://github.com/python/cpython/issues/1234?param=1',
),
)
def test_invalid_issue_number(issue):
error_message = re.escape(f'Invalid GitHub issue number: {issue}')
with pytest.raises(SystemExit, match=error_message):
_blurb_template_text(issue=issue, section=None)
@pytest.mark.parametrize(
'invalid',
(
'gh-issue: ',
'gh-issue: 1',
'gh-issue',
),
)
def test_malformed_gh_issue_line(invalid, monkeypatch):
template = blurb_template.replace('.. gh-issue:', invalid)
error_message = re.escape("Can't find gh-issue line in the template!")
with monkeypatch.context() as cm:
cm.setattr(blurb._add, 'template', template)
with pytest.raises(SystemExit, match=error_message):
_blurb_template_text(issue='1234', section=None)
def _check_section_name(section_name, expected):
actual = _extract_section_name(section_name)
assert actual == expected
res = _blurb_template_text(issue=None, section=section_name)
res = res.splitlines()
for section_name in SECTIONS:
if section_name == expected:
assert f'.. section: {section_name}' in res
else:
assert f'#.. section: {section_name}' in res
assert f'.. section: {section_name}' not in res
@pytest.mark.parametrize(
('section_name', 'expected'),
[(name, name) for name in SECTIONS],
)
def test_exact_names(section_name, expected):
_check_section_name(section_name, expected)
@pytest.mark.parametrize(
('section_name', 'expected'),
[(name.lower(), name) for name in SECTIONS],
)
def test_exact_names_lowercase(section_name, expected):
_check_section_name(section_name, expected)
@pytest.mark.parametrize(
('section', 'expect'),
[
('Sec', 'Security'),
('sec', 'Security'),
('security', 'Security'),
('Core And', 'Core and Builtins'),
('Core And Built', 'Core and Builtins'),
('Core And Builtins', 'Core and Builtins'),
('Lib', 'Library'),
('doc', 'Documentation'),
('document', 'Documentation'),
('Tes', 'Tests'),
('tes', 'Tests'),
('Test', 'Tests'),
('Tests', 'Tests'),
('Buil', 'Build'),
('buil', 'Build'),
('build', 'Build'),
('Tool', 'Tools/Demos'),
('Tools', 'Tools/Demos'),
('Tools/', 'Tools/Demos'),
('core', 'Core and Builtins'),
],
)
def test_partial_words(section, expect):
_check_section_name(section, expect)
@pytest.mark.parametrize(
('section', 'expect'),
[
('builtin', 'Core and Builtins'),
('builtins', 'Core and Builtins'),
('api', 'C API'),
('c-api', 'C API'),
('c/api', 'C API'),
('c api', 'C API'),
('dem', 'Tools/Demos'),
('demo', 'Tools/Demos'),
('demos', 'Tools/Demos'),
],
)
def test_partial_special_names(section, expect):
_check_section_name(section, expect)
@pytest.mark.parametrize(
('section', 'expect'),
[
('Core-and-Builtins', 'Core and Builtins'),
('Core_and_Builtins', 'Core and Builtins'),
('Core_and-Builtins', 'Core and Builtins'),
('Core and', 'Core and Builtins'),
('Core_and', 'Core and Builtins'),
('core_and', 'Core and Builtins'),
('core-and', 'Core and Builtins'),
('Core and Builtins', 'Core and Builtins'),
('cOre _ and - bUILtins', 'Core and Builtins'),
('Tools/demo', 'Tools/Demos'),
('Tools-demo', 'Tools/Demos'),
('Tools demo', 'Tools/Demos'),
],
)
def test_partial_separators(section, expect):
# normalize the separtors '_', '-', ' ' and '/'
_check_section_name(section, expect)
@pytest.mark.parametrize(
('prefix', 'expect'),
[
('corean', 'Core and Builtins'),
('coreand', 'Core and Builtins'),
('coreandbuilt', 'Core and Builtins'),
('coreand Builtins', 'Core and Builtins'),
('coreand Builtins', 'Core and Builtins'),
('coreAnd Builtins', 'Core and Builtins'),
('CoreAnd Builtins', 'Core and Builtins'),
('Coreand', 'Core and Builtins'),
('Coreand Builtins', 'Core and Builtins'),
('Coreand builtin', 'Core and Builtins'),
('Coreand buil', 'Core and Builtins'),
],
)
def test_partial_prefix_words(prefix, expect):
# try to find a match using prefixes (without separators and lowercase)
_check_section_name(prefix, expect)
@pytest.mark.parametrize(
'section',
(
'',
' ',
'\t',
'\n',
'\r\n',
' ',
),
)
def test_empty_section_name(section):
error_message = re.escape('Empty section name!')
with pytest.raises(SystemExit, match=error_message):
_extract_section_name(section)
with pytest.raises(SystemExit, match=error_message):
_blurb_template_text(issue=None, section=section)
@pytest.mark.parametrize(
'section',
[
# Wrong capitalisation
'C api',
'c API',
'LibrarY',
# Invalid
'_',
'-',
'/',
'invalid',
'Not a section',
# Non-special names
'c?api',
'cXapi',
'C+API',
# Super-strings
'Library and more',
'library3',
'librari',
],
)
def test_invalid_section_name(section):
error_message = rf"(?m)Invalid section name: '{re.escape(section)}'\n\n.+"
with pytest.raises(SystemExit, match=error_message):
_extract_section_name(section)
with pytest.raises(SystemExit, match=error_message):
_blurb_template_text(issue=None, section=section)
@pytest.mark.parametrize(
('section', 'matches'),
[
# 'matches' must be a sorted sequence of matching section names
('c', ['C API', 'Core and Builtins']),
('C', ['C API', 'Core and Builtins']),
('t', ['Tests', 'Tools/Demos']),
('T', ['Tests', 'Tools/Demos']),
],
)
def test_ambiguous_section_name(section, matches):
matching_list = ', '.join(map(repr, matches))
error_message = re.escape(
f'More than one match for: {section!r}\nMatches: {matching_list}'
)
error_message = re.compile(rf'{error_message}', re.MULTILINE)
with pytest.raises(SystemExit, match=error_message):
_extract_section_name(section)
with pytest.raises(SystemExit, match=error_message):
_blurb_template_text(issue=None, section=section)