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
21import sys
22import textwrap
23import typing
24
25import pytest
26
27# AsyncMock is new in 3.8, so if we're using an older version we need the
28# backported version of mock
29if sys.version_info >= (3, 8):
30    from unittest import mock
31else:
32    import mock
33
34from .gen_release_notes import *
35
36
37@pytest.mark.parametrize(
38    'current, is_point, expected',
39    [
40        ('19.2.0', True, '19.2.1'),
41        ('19.3.6', True, '19.3.7'),
42        ('20.0.0-rc4', False, '20.0.0'),
43    ])
44def test_next_version(current: str, is_point: bool, expected: str) -> None:
45    assert calculate_next_version(current, is_point) == expected
46
47
48@pytest.mark.parametrize(
49    'current, is_point, expected',
50    [
51        ('19.3.6', True, '19.3.6'),
52        ('20.0.0-rc4', False, '19.3.0'),
53    ])
54def test_previous_version(current: str, is_point: bool, expected: str) -> None:
55    assert calculate_previous_version(current, is_point) == expected
56
57
58@pytest.mark.asyncio
59async def test_get_shortlog():
60    # Certainly not perfect, but it's something
61    version = '19.2.0'
62    out = await get_shortlog(version)
63    assert out
64
65
66@pytest.mark.asyncio
67async def test_gather_commits():
68    # Certainly not perfect, but it's something
69    version = '19.2.0'
70    out = await gather_commits(version)
71    assert out
72
73
74@pytest.mark.asyncio
75@pytest.mark.parametrize(
76    'content, bugs',
77    [
78        # It is important to have the title on a new line, as
79        # textwrap.dedent won't work otherwise.
80
81        # Test the `Closes: #N` syntax
82        (
83            '''\
84            A commit
85
86            It has a message in it
87
88            Closes: #1
89            ''',
90            ['1'],
91        ),
92
93        # Test the Full url
94        (
95            '''\
96            A commit with no body
97
98            Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3456
99            ''',
100            ['3456'],
101        ),
102
103        # Test projects that are not mesa
104        (
105            '''\
106            A commit for libdrm
107
108            Closes: https://gitlab.freedesktop.org/mesa/drm/-/3456
109            ''',
110            [],
111        ),
112        (
113            '''\
114            A commit for for something else completely
115
116            Closes: https://github.com/Organization/project/1234
117            ''',
118            [],
119        ),
120
121        # Test  multiple issues on one line
122        (
123            '''\
124            Fix many bugs
125
126            Closes: #1, #2
127            ''',
128            ['1', '2'],
129        ),
130
131        # Test multiple closes
132        (
133            '''\
134            Fix many bugs
135
136            Closes: #1
137            Closes: #2
138            ''',
139            ['1', '2'],
140        ),
141        (
142            '''\
143            With long form
144
145            Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3456
146            Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3457
147            Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3458
148            ''',
149            ['3456', '3457', '3458'],
150        ),
151        (
152            '''\
153            Without /-/
154
155            Closes: https://gitlab.freedesktop.org/mesa/mesa/issues/36
156            ''',
157            ['36'],
158        ),
159        (
160            '''\
161            Ignore merge_requests
162
163            Closes: https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/20241
164            ''',
165            [],
166        ),
167        (
168            '''\
169            Parse "Fixes:" tag too
170
171            Fixes: https://gitlab.freedesktop.org/mesa/mesa/issues/36
172            Fixes: 142565a3bc2
173            Fixes: 142565a3bc2 ("docs: do something very useful")
174            Fixes: 142565a3bc2 ("docs: fix #1234, have a comma")
175            Fixes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/37
176            ''',
177            ['36', '37'],
178        ),
179        (
180            '''\
181            Parse Fixes/Closes in weird cases
182
183            fixes: https://gitlab.freedesktop.org/mesa/mesa/issues/36
184            fiXES: https://gitlab.freedesktop.org/mesa/mesa/issues/37
185            closes: https://gitlab.freedesktop.org/mesa/mesa/issues/38
186            cloSES: https://gitlab.freedesktop.org/mesa/mesa/issues/39
187            ''',
188            ['36', '37', '38', '39'],
189        ),
190    ])
191async def test_parse_issues(content: str, bugs: typing.List[str]) -> None:
192    mock_com = mock.AsyncMock(return_value=(textwrap.dedent(content).encode(), ''))
193    mock_p = mock.Mock()
194    mock_p.communicate = mock_com
195    mock_exec = mock.AsyncMock(return_value=mock_p)
196
197    with mock.patch('bin.gen_release_notes.asyncio.create_subprocess_exec', mock_exec), \
198            mock.patch('bin.gen_release_notes.gather_commits', mock.AsyncMock(return_value='sha\n')):
199        ids = await parse_issues('1234 not used')
200        assert set(ids) == set(bugs)
201