1bf215546Sopenharmony_ci# Copyright © 2019,2021 Intel Corporation
2bf215546Sopenharmony_ci
3bf215546Sopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a copy
4bf215546Sopenharmony_ci# of this software and associated documentation files (the "Software"), to deal
5bf215546Sopenharmony_ci# in the Software without restriction, including without limitation the rights
6bf215546Sopenharmony_ci# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7bf215546Sopenharmony_ci# copies of the Software, and to permit persons to whom the Software is
8bf215546Sopenharmony_ci# furnished to do so, subject to the following conditions:
9bf215546Sopenharmony_ci
10bf215546Sopenharmony_ci# The above copyright notice and this permission notice shall be included in
11bf215546Sopenharmony_ci# all copies or substantial portions of the Software.
12bf215546Sopenharmony_ci
13bf215546Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14bf215546Sopenharmony_ci# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15bf215546Sopenharmony_ci# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16bf215546Sopenharmony_ci# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17bf215546Sopenharmony_ci# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18bf215546Sopenharmony_ci# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19bf215546Sopenharmony_ci# SOFTWARE.
20bf215546Sopenharmony_ci
21bf215546Sopenharmony_ciimport sys
22bf215546Sopenharmony_ciimport textwrap
23bf215546Sopenharmony_ciimport typing
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ciimport pytest
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci# AsyncMock is new in 3.8, so if we're using an older version we need the
28bf215546Sopenharmony_ci# backported version of mock
29bf215546Sopenharmony_ciif sys.version_info >= (3, 8):
30bf215546Sopenharmony_ci    from unittest import mock
31bf215546Sopenharmony_cielse:
32bf215546Sopenharmony_ci    import mock
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_cifrom .gen_release_notes import *
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci@pytest.mark.parametrize(
38bf215546Sopenharmony_ci    'current, is_point, expected',
39bf215546Sopenharmony_ci    [
40bf215546Sopenharmony_ci        ('19.2.0', True, '19.2.1'),
41bf215546Sopenharmony_ci        ('19.3.6', True, '19.3.7'),
42bf215546Sopenharmony_ci        ('20.0.0-rc4', False, '20.0.0'),
43bf215546Sopenharmony_ci    ])
44bf215546Sopenharmony_cidef test_next_version(current: str, is_point: bool, expected: str) -> None:
45bf215546Sopenharmony_ci    assert calculate_next_version(current, is_point) == expected
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci@pytest.mark.parametrize(
49bf215546Sopenharmony_ci    'current, is_point, expected',
50bf215546Sopenharmony_ci    [
51bf215546Sopenharmony_ci        ('19.3.6', True, '19.3.6'),
52bf215546Sopenharmony_ci        ('20.0.0-rc4', False, '19.3.0'),
53bf215546Sopenharmony_ci    ])
54bf215546Sopenharmony_cidef test_previous_version(current: str, is_point: bool, expected: str) -> None:
55bf215546Sopenharmony_ci    assert calculate_previous_version(current, is_point) == expected
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci@pytest.mark.asyncio
59bf215546Sopenharmony_ciasync def test_get_shortlog():
60bf215546Sopenharmony_ci    # Certainly not perfect, but it's something
61bf215546Sopenharmony_ci    version = '19.2.0'
62bf215546Sopenharmony_ci    out = await get_shortlog(version)
63bf215546Sopenharmony_ci    assert out
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci@pytest.mark.asyncio
67bf215546Sopenharmony_ciasync def test_gather_commits():
68bf215546Sopenharmony_ci    # Certainly not perfect, but it's something
69bf215546Sopenharmony_ci    version = '19.2.0'
70bf215546Sopenharmony_ci    out = await gather_commits(version)
71bf215546Sopenharmony_ci    assert out
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci@pytest.mark.asyncio
75bf215546Sopenharmony_ci@pytest.mark.parametrize(
76bf215546Sopenharmony_ci    'content, bugs',
77bf215546Sopenharmony_ci    [
78bf215546Sopenharmony_ci        # It is important to have the title on a new line, as
79bf215546Sopenharmony_ci        # textwrap.dedent wont work otherwise.
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci        # Test the `Closes: #N` syntax
82bf215546Sopenharmony_ci        (
83bf215546Sopenharmony_ci            '''\
84bf215546Sopenharmony_ci            A commit
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci            It has a message in it
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci            Closes: #1
89bf215546Sopenharmony_ci            ''',
90bf215546Sopenharmony_ci            ['1'],
91bf215546Sopenharmony_ci        ),
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci        # Test the Full url
94bf215546Sopenharmony_ci        (
95bf215546Sopenharmony_ci            '''\
96bf215546Sopenharmony_ci            A commit with no body
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci            Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3456
99bf215546Sopenharmony_ci            ''',
100bf215546Sopenharmony_ci            ['3456'],
101bf215546Sopenharmony_ci        ),
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci        # Test projects that are not mesa
104bf215546Sopenharmony_ci        (
105bf215546Sopenharmony_ci            '''\
106bf215546Sopenharmony_ci            A commit for libdrm
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci            Closes: https://gitlab.freedesktop.org/mesa/drm/-/3456
109bf215546Sopenharmony_ci            ''',
110bf215546Sopenharmony_ci            [],
111bf215546Sopenharmony_ci        ),
112bf215546Sopenharmony_ci        (
113bf215546Sopenharmony_ci            '''\
114bf215546Sopenharmony_ci            A commit for for something else completely
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci            Closes: https://github.com/Organiztion/project/1234
117bf215546Sopenharmony_ci            ''',
118bf215546Sopenharmony_ci            [],
119bf215546Sopenharmony_ci        ),
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci        # Test  multiple issues on one line
122bf215546Sopenharmony_ci        (
123bf215546Sopenharmony_ci            '''\
124bf215546Sopenharmony_ci            Fix many bugs
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci            Closes: #1, #2
127bf215546Sopenharmony_ci            ''',
128bf215546Sopenharmony_ci            ['1', '2'],
129bf215546Sopenharmony_ci        ),
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci        # Test multiple closes
132bf215546Sopenharmony_ci        (
133bf215546Sopenharmony_ci            '''\
134bf215546Sopenharmony_ci            Fix many bugs
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci            Closes: #1
137bf215546Sopenharmony_ci            Closes: #2
138bf215546Sopenharmony_ci            ''',
139bf215546Sopenharmony_ci            ['1', '2'],
140bf215546Sopenharmony_ci        ),
141bf215546Sopenharmony_ci        (
142bf215546Sopenharmony_ci            '''\
143bf215546Sopenharmony_ci            With long form
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci            Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3456
146bf215546Sopenharmony_ci            Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3457
147bf215546Sopenharmony_ci            Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3458
148bf215546Sopenharmony_ci            ''',
149bf215546Sopenharmony_ci            ['3456', '3457', '3458'],
150bf215546Sopenharmony_ci        ),
151bf215546Sopenharmony_ci    ])
152bf215546Sopenharmony_ciasync def test_parse_issues(content: str, bugs: typing.List[str]) -> None:
153bf215546Sopenharmony_ci    mock_com = mock.AsyncMock(return_value=(textwrap.dedent(content).encode(), ''))
154bf215546Sopenharmony_ci    mock_p = mock.Mock()
155bf215546Sopenharmony_ci    mock_p.communicate = mock_com
156bf215546Sopenharmony_ci    mock_exec = mock.AsyncMock(return_value=mock_p)
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci    with mock.patch('bin.gen_release_notes.asyncio.create_subprocess_exec', mock_exec), \
159bf215546Sopenharmony_ci            mock.patch('bin.gen_release_notes.gather_commits', mock.AsyncMock(return_value='sha\n')):
160bf215546Sopenharmony_ci        ids = await parse_issues('1234 not used')
161bf215546Sopenharmony_ci        assert set(ids) == set(bugs)
162