Debian Codegraph @

bin/gen_release_notes_test.py

raw
1 # Copyright © 2019,2021 Intel Corporation
2
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to deal
5 # in the Software without restriction, including without limitation the rights
6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 # copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
9
10 # The above copyright notice and this permission notice shall be included in
11 # all copies or substantial portions of the Software.
12
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 # SOFTWARE.
20
21 import sys
22 import textwrap
23 import typing
24
25 import pytest
26
27 if typing.TYPE_CHECKING:
28 import aiohttp
29
30 # AsyncMock is new in 3.8, so if we're using an older version we need the
31 # backported version of mock
32 if sys.version_info >= (3, 8):
33 from unittest import mock
34 else:
35 import mock
36
37 from .gen_release_notes import *
38
39
40 @pytest.mark.parametrize(
41 'current, is_point, expected',
42 [
43 ('19.2.0', True, '19.2.1'),
44 ('19.3.6', True, '19.3.7'),
45 ('20.0.0-rc4', False, '20.0.0'),
46 ])
47 def test_next_version(current: str, is_point: bool, expected: str) -> None:
48 assert calculate_next_version(current, is_point) == expected
49
50
51 @pytest.mark.parametrize(
52 'current, is_point, expected',
53 [
54 ('19.3.6', True, '19.3.6'),
55 ('20.0.0-rc4', False, '19.3.0'),
56 ])
57 def test_previous_version(current: str, is_point: bool, expected: str) -> None:
58 assert calculate_previous_version(current, is_point) == expected
59
60
61 @pytest.mark.asyncio
62 async def test_get_shortlog():
63 # Certainly not perfect, but it's something
64 version = '19.2.0'
65 out = await get_shortlog(version)
66 assert out
67
68
69 @pytest.mark.asyncio
70 async def test_gather_commits():
71 # Certainly not perfect, but it's something
72 version = '19.2.0'
73 out = await gather_commits(version)
74 assert out
75
76
77 @pytest.mark.asyncio
78 @pytest.mark.parametrize(
79 'content, bugs',
80 [
81 # It is important to have the title on a new line, as
82 # textwrap.dedent won't work otherwise.
83
84 # Test the `Closes: #N` syntax
85 (
86 '''\
87 A commit
88
89 It has a message in it
90
91 Closes: #1
92 ''',
93 ['1'],
94 ),
95
96 # Test the Full url
97 (
98 '''\
99 A commit with no body
100
101 Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3456
102 ''',
103 ['3456'],
104 ),
105
106 # Test projects that are not mesa
107 (
108 '''\
109 A commit for libdrm
110
111 Closes: https://gitlab.freedesktop.org/mesa/drm/-/3456
112 ''',
113 [],
114 ),
115 (
116 '''\
117 A commit for for something else completely
118
119 Closes: https://github.com/Organization/project/1234
120 ''',
121 [],
122 ),
123
124 # Test multiple issues on one line
125 (
126 '''\
127 Fix many bugs
128
129 Closes: #1, #2
130 ''',
131 ['1', '2'],
132 ),
133
134 # Test multiple closes
135 (
136 '''\
137 Fix many bugs
138
139 Closes: #1
140 Closes: #2
141 ''',
142 ['1', '2'],
143 ),
144 (
145 '''\
146 With long form
147
148 Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3456
149 Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3457
150 Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3458
151 ''',
152 ['3456', '3457', '3458'],
153 ),
154 (
155 '''\
156 Without /-/
157
158 Closes: https://gitlab.freedesktop.org/mesa/mesa/issues/36
159 ''',
160 ['36'],
161 ),
162 (
163 '''\
164 Ignore merge_requests
165
166 Closes: https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/20241
167 ''',
168 [],
169 ),
170 (
171 '''\
172 Parse "Fixes:" tag too
173
174 Fixes: https://gitlab.freedesktop.org/mesa/mesa/issues/36
175 Fixes: 142565a3bc2
176 Fixes: 142565a3bc2 ("docs: do something very useful")
177 Fixes: 142565a3bc2 ("docs: fix #1234, have a comma")
178 Fixes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/37
179 ''',
180 ['36', '37'],
181 ),
182 (
183 '''\
184 Parse Fixes/Closes in weird cases
185
186 fixes: https://gitlab.freedesktop.org/mesa/mesa/issues/36
187 fiXES: https://gitlab.freedesktop.org/mesa/mesa/issues/37
188 closes: https://gitlab.freedesktop.org/mesa/mesa/issues/38
189 cloSES: https://gitlab.freedesktop.org/mesa/mesa/issues/39
190 ''',
191 ['36', '37', '38', '39'],
192 ),
193 ])
194 async def test_parse_issues(content: str, bugs: typing.List[str]) -> None:
195 mock_com = mock.AsyncMock(return_value=(textwrap.dedent(content).encode(), ''))
196 mock_p = mock.Mock()
197 mock_p.communicate = mock_com
198 mock_exec = mock.AsyncMock(return_value=mock_p)
199
200 with mock.patch('bin.gen_release_notes.asyncio.create_subprocess_exec', mock_exec), \
201 mock.patch('bin.gen_release_notes.gather_commits', mock.AsyncMock(return_value='sha\n')):
202 ids = await parse_issues('1234 not used')
203 assert set(ids) == set(bugs)
204
205
206 @pytest.mark.asyncio
207 async def test_rst_escape():
208 out = inliner.quoteInline('foo@bar')
209 assert out == 'foo\@bar'
210
211
212 @pytest.mark.asyncio
213 async def test_gather_bugs_duplicates():
214 mock_gc = mock.AsyncMock(return_value='something')
215 mock_pi = mock.AsyncMock(return_value=['a', 'b', 'a', 'a', 'c', 'b'])
216
217 async def get_bug(session: 'aiohttp.ClientSession', bug_id: str) -> str:
218 return bug_id
219
220 with mock.patch('bin.gen_release_notes.gather_commits', mock_gc), \
221 mock.patch('bin.gen_release_notes.parse_issues', mock_pi), \
222 mock.patch('bin.gen_release_notes.get_bug', get_bug):
223 bugs = await gather_bugs('')
224
225 assert bugs == ['a', 'b', 'c']
226