forked from python/blurb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_blurb_add.py
More file actions
163 lines (139 loc) · 4.7 KB
/
test_blurb_add.py
File metadata and controls
163 lines (139 loc) · 4.7 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
import re
import pytest
from blurb import blurb
def test_valid_no_issue_number():
assert blurb._extract_issue_number(None) is None
res = blurb._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 = blurb._extract_issue_number(issue)
assert actual == 12345
res = blurb._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._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, 'template', template)
with pytest.raises(SystemExit, match=error_message):
blurb._blurb_template_text(issue='1234', section=None)
def _check_section_name(section_name, expected):
actual = blurb._extract_section_name(section_name)
assert actual == expected
res = blurb._blurb_template_text(issue=None, section=section_name)
res = res.splitlines()
for section_name in blurb.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 blurb.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 blurb.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):
blurb._extract_section_name(section)
with pytest.raises(SystemExit, match=error_message):
blurb._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):
blurb._extract_section_name(section)
with pytest.raises(SystemExit, match=error_message):
blurb._blurb_template_text(issue=None, section=section)