-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_add.py
More file actions
206 lines (180 loc) · 5.77 KB
/
test_add.py
File metadata and controls
206 lines (180 loc) · 5.77 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
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',
(
'',
' ',
'\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',
[
# 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, expected',
[
# Case variations now work
('C api', 'C API'),
('c API', 'C API'),
('c api', 'C API'),
('LibrarY', 'Library'),
('LIBRARY', 'Library'),
# Substring matching
('lib', 'Library'),
('api', 'C API'),
('core', 'Core and Builtins'),
('builtin', 'Core and Builtins'),
('doc', 'Documentation'),
('test', 'Tests'),
('tool', 'Tools/Demos'),
('demo', 'Tools/Demos'),
],
)
def test_smart_section_matching(section, expected):
"""Test that smart section matching and aliases work correctly."""
assert _extract_section_name(section) == expected