1bf215546Sopenharmony_ci#!/usr/bin/env python3
2bf215546Sopenharmony_ci# SPDX-License-Identifier: MIT
3bf215546Sopenharmony_ci
4bf215546Sopenharmony_ci# Copyright © 2021 Intel Corporation
5bf215546Sopenharmony_ci
6bf215546Sopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a copy
7bf215546Sopenharmony_ci# of this software and associated documentation files (the "Software"), to deal
8bf215546Sopenharmony_ci# in the Software without restriction, including without limitation the rights
9bf215546Sopenharmony_ci# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10bf215546Sopenharmony_ci# copies of the Software, and to permit persons to whom the Software is
11bf215546Sopenharmony_ci# furnished to do so, subject to the following conditions:
12bf215546Sopenharmony_ci
13bf215546Sopenharmony_ci# The above copyright notice and this permission notice shall be included in
14bf215546Sopenharmony_ci# all copies or substantial portions of the Software.
15bf215546Sopenharmony_ci
16bf215546Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19bf215546Sopenharmony_ci# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21bf215546Sopenharmony_ci# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bf215546Sopenharmony_ci# SOFTWARE.
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_cifrom __future__ import annotations
25bf215546Sopenharmony_cifrom unittest import mock
26bf215546Sopenharmony_ciimport argparse
27bf215546Sopenharmony_ciimport csv
28bf215546Sopenharmony_ciimport contextlib
29bf215546Sopenharmony_ciimport datetime
30bf215546Sopenharmony_ciimport tempfile
31bf215546Sopenharmony_ciimport os
32bf215546Sopenharmony_ciimport pathlib
33bf215546Sopenharmony_ciimport typing
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ciimport pytest
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cifrom . import gen_calendar_entries
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci@contextlib.contextmanager
41bf215546Sopenharmony_cidef mock_csv(data: typing.List[gen_calendar_entries.CalendarRowType]) -> typing.Iterator[None]:
42bf215546Sopenharmony_ci    """Replace the actual CSV data with our test data."""
43bf215546Sopenharmony_ci    with tempfile.TemporaryDirectory() as d:
44bf215546Sopenharmony_ci        c = os.path.join(d, 'calendar.csv')
45bf215546Sopenharmony_ci        with open(c, 'w') as f:
46bf215546Sopenharmony_ci            writer = csv.writer(f)
47bf215546Sopenharmony_ci            writer.writerows(data)
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci        with mock.patch('bin.gen_calendar_entries.CALENDAR_CSV', pathlib.Path(c)):
50bf215546Sopenharmony_ci            yield
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci@pytest.fixture(autouse=True, scope='module')
54bf215546Sopenharmony_cidef disable_git_commits() -> None:
55bf215546Sopenharmony_ci    """Mock out the commit function so no git commits are made durring testing."""
56bf215546Sopenharmony_ci    with mock.patch('bin.gen_calendar_entries.commit', mock.Mock()):
57bf215546Sopenharmony_ci        yield
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ciclass TestReleaseStart:
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci    def test_first_is_wednesday(self) -> None:
63bf215546Sopenharmony_ci        d = gen_calendar_entries._calculate_release_start('20', '0')
64bf215546Sopenharmony_ci        assert d.day == 15
65bf215546Sopenharmony_ci        assert d.month == 1
66bf215546Sopenharmony_ci        assert d.year == 2020
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci    def test_first_is_before_wednesday(self) -> None:
69bf215546Sopenharmony_ci        d = gen_calendar_entries._calculate_release_start('19', '0')
70bf215546Sopenharmony_ci        assert d.day == 16
71bf215546Sopenharmony_ci        assert d.month == 1
72bf215546Sopenharmony_ci        assert d.year == 2019
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci    def test_first_is_after_wednesday(self) -> None:
75bf215546Sopenharmony_ci        d = gen_calendar_entries._calculate_release_start('21', '0')
76bf215546Sopenharmony_ci        assert d.day == 13
77bf215546Sopenharmony_ci        assert d.month == 1
78bf215546Sopenharmony_ci        assert d.year == 2021
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ciclass TestNextReleaseDate:
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci    @contextlib.contextmanager
84bf215546Sopenharmony_ci    def _patch_date(date: datetime.date) -> typing.Iterator[None]:
85bf215546Sopenharmony_ci        mdate = mock.Mock()
86bf215546Sopenharmony_ci        mdate.today = mock.Mock(return_value=date)
87bf215546Sopenharmony_ci        with mock.patch('bin.gen_calendar_entries.datetime.date', mdate):
88bf215546Sopenharmony_ci            yield
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci    class TestIsWeds:
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci        @pytest.fixture(scope='class', autouse=True)
93bf215546Sopenharmony_ci        def data(self) -> None:
94bf215546Sopenharmony_ci            date = datetime.date(2021, 1, 6)
95bf215546Sopenharmony_ci            with TestNextReleaseDate._patch_date(date):
96bf215546Sopenharmony_ci                yield
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci        @pytest.mark.parametrize(
99bf215546Sopenharmony_ci            'is_zero, expected',
100bf215546Sopenharmony_ci            [
101bf215546Sopenharmony_ci                (True, 13),
102bf215546Sopenharmony_ci                (False, 20),
103bf215546Sopenharmony_ci            ],
104bf215546Sopenharmony_ci        )
105bf215546Sopenharmony_ci        def test(self, is_zero: bool, expected: int) -> None:
106bf215546Sopenharmony_ci            date = gen_calendar_entries._calculate_next_release_date(is_zero)
107bf215546Sopenharmony_ci            assert date.day == expected
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci    class TestBeforeWeds:
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci        @pytest.fixture(scope='class', autouse=True)
112bf215546Sopenharmony_ci        def data(self) -> None:
113bf215546Sopenharmony_ci            date = datetime.date(2021, 1, 5)
114bf215546Sopenharmony_ci            with TestNextReleaseDate._patch_date(date):
115bf215546Sopenharmony_ci                yield
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci        @pytest.mark.parametrize(
118bf215546Sopenharmony_ci            'is_zero, expected',
119bf215546Sopenharmony_ci            [
120bf215546Sopenharmony_ci                (True, 13),
121bf215546Sopenharmony_ci                (False, 20),
122bf215546Sopenharmony_ci            ],
123bf215546Sopenharmony_ci        )
124bf215546Sopenharmony_ci        def test(self, is_zero: bool, expected: int) -> None:
125bf215546Sopenharmony_ci            date = gen_calendar_entries._calculate_next_release_date(is_zero)
126bf215546Sopenharmony_ci            assert date.day == expected
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci    class TestAfterWeds:
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci        @pytest.fixture(scope='class', autouse=True)
131bf215546Sopenharmony_ci        def data(self) -> None:
132bf215546Sopenharmony_ci            date = datetime.date(2021, 1, 8)
133bf215546Sopenharmony_ci            with TestNextReleaseDate._patch_date(date):
134bf215546Sopenharmony_ci                yield
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci        @pytest.mark.parametrize(
137bf215546Sopenharmony_ci            'is_zero, expected',
138bf215546Sopenharmony_ci            [
139bf215546Sopenharmony_ci                (True, 13),
140bf215546Sopenharmony_ci                (False, 20),
141bf215546Sopenharmony_ci            ],
142bf215546Sopenharmony_ci        )
143bf215546Sopenharmony_ci        def test(self, is_zero: bool, expected: int) -> None:
144bf215546Sopenharmony_ci            date = gen_calendar_entries._calculate_next_release_date(is_zero)
145bf215546Sopenharmony_ci            assert date.day == expected
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ciclass TestRC:
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci    ORIGINAL_DATA = [
151bf215546Sopenharmony_ci        ('20.3', '2021-01-13', '20.3.3', 'Dylan Baker', ''),
152bf215546Sopenharmony_ci        ('',     '2021-01-27', '20.3.4', 'Dylan Baker', 'Last planned release of the 20.3.x series'),
153bf215546Sopenharmony_ci    ]
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_ci    @pytest.fixture(autouse=True, scope='class')
156bf215546Sopenharmony_ci    def mock_version(self) -> None:
157bf215546Sopenharmony_ci        """Keep the version set at a specific value."""
158bf215546Sopenharmony_ci        with tempfile.TemporaryDirectory() as d:
159bf215546Sopenharmony_ci            v = os.path.join(d, 'version')
160bf215546Sopenharmony_ci            with open(v, 'w') as f:
161bf215546Sopenharmony_ci                f.write('21.0.0-devel\n')
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci            with mock.patch('bin.gen_calendar_entries.VERSION', pathlib.Path(v)):
164bf215546Sopenharmony_ci                yield
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_ci    @pytest.fixture(autouse=True)
167bf215546Sopenharmony_ci    def csv(self) -> None:
168bf215546Sopenharmony_ci        """inject our test data.."""
169bf215546Sopenharmony_ci        with mock_csv(self.ORIGINAL_DATA):
170bf215546Sopenharmony_ci            yield
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci    def test_basic(self) -> None:
173bf215546Sopenharmony_ci        args: gen_calendar_entries.RCArguments = argparse.Namespace()
174bf215546Sopenharmony_ci        args.manager = "Dylan Baker"
175bf215546Sopenharmony_ci        gen_calendar_entries.release_candidate(args)
176bf215546Sopenharmony_ci
177bf215546Sopenharmony_ci        expected = self.ORIGINAL_DATA.copy()
178bf215546Sopenharmony_ci        expected.append(('21.0', '2021-01-13', f'21.0.0-rc1', 'Dylan Baker'))
179bf215546Sopenharmony_ci        expected.append((    '', '2021-01-20', f'21.0.0-rc2', 'Dylan Baker'))
180bf215546Sopenharmony_ci        expected.append((    '', '2021-01-27', f'21.0.0-rc3', 'Dylan Baker'))
181bf215546Sopenharmony_ci        expected.append((    '', '2021-02-03', f'21.0.0-rc4', 'Dylan Baker', 'Or 21.0.0 final.'))
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ci        actual = gen_calendar_entries.read_calendar()
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci        assert actual == expected
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ciclass TestExtend:
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_ci    def test_one_release(self) -> None:
191bf215546Sopenharmony_ci        data = [
192bf215546Sopenharmony_ci            ('20.3', '2021-01-13', '20.3.3', 'Dylan Baker', ''),
193bf215546Sopenharmony_ci            ('',     '2021-01-27', '20.3.4', 'Dylan Baker', 'This is the last planned release of the 20.3.x series.'),
194bf215546Sopenharmony_ci        ]
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci        args: gen_calendar_entries.ExtendArguments = argparse.Namespace()
197bf215546Sopenharmony_ci        args.series = '20.3'
198bf215546Sopenharmony_ci        args.count = 2
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ci        with mock_csv(data):
201bf215546Sopenharmony_ci            gen_calendar_entries.extend(args)
202bf215546Sopenharmony_ci            actual = gen_calendar_entries.read_calendar()
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci        expected = [
205bf215546Sopenharmony_ci            data[0],
206bf215546Sopenharmony_ci            ('', '2021-01-27', '20.3.4', 'Dylan Baker', ''),
207bf215546Sopenharmony_ci            ('', '2021-02-10', '20.3.5', 'Dylan Baker', ''),
208bf215546Sopenharmony_ci            ('', '2021-02-24', '20.3.6', 'Dylan Baker', 'This is the last planned release of the 20.3.x series.'),
209bf215546Sopenharmony_ci        ]
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ci        assert actual == expected
212bf215546Sopenharmony_ci    def test_one_release(self) -> None:
213bf215546Sopenharmony_ci        data = [
214bf215546Sopenharmony_ci            ('20.3', '2021-01-13', '20.3.3', 'Dylan Baker', ''),
215bf215546Sopenharmony_ci            ('',     '2021-01-27', '20.3.4', 'Dylan Baker', 'This is the last planned release of the 20.3.x series.'),
216bf215546Sopenharmony_ci            ('21.0', '2021-01-13', '21.0.1', 'Dylan Baker', ''),
217bf215546Sopenharmony_ci            ('',     '2021-01-27', '21.0.2', 'Dylan Baker', ''),
218bf215546Sopenharmony_ci            ('',     '2021-02-10', '21.0.3', 'Dylan Baker', ''),
219bf215546Sopenharmony_ci            ('',     '2021-02-24', '21.0.4', 'Dylan Baker', 'This is the last planned release of the 21.0.x series.'),
220bf215546Sopenharmony_ci        ]
221bf215546Sopenharmony_ci
222bf215546Sopenharmony_ci        args: gen_calendar_entries.ExtendArguments = argparse.Namespace()
223bf215546Sopenharmony_ci        args.series = '21.0'
224bf215546Sopenharmony_ci        args.count = 1
225bf215546Sopenharmony_ci
226bf215546Sopenharmony_ci        with mock_csv(data):
227bf215546Sopenharmony_ci            gen_calendar_entries.extend(args)
228bf215546Sopenharmony_ci            actual = gen_calendar_entries.read_calendar()
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci        expected = data.copy()
231bf215546Sopenharmony_ci        d = list(data[-1])
232bf215546Sopenharmony_ci        d[-1] = ''
233bf215546Sopenharmony_ci        expected[-1] = tuple(d)
234bf215546Sopenharmony_ci        expected.extend([
235bf215546Sopenharmony_ci            ('',     '2021-03-10', '21.0.5', 'Dylan Baker', 'This is the last planned release of the 21.0.x series.'),
236bf215546Sopenharmony_ci        ])
237bf215546Sopenharmony_ci
238bf215546Sopenharmony_ci        assert actual == expected
239bf215546Sopenharmony_ci
240bf215546Sopenharmony_ci    def test_rc(self) -> None:
241bf215546Sopenharmony_ci        data = [
242bf215546Sopenharmony_ci            ('20.3', '2021-01-13', '20.3.3', 'Dylan Baker', ''),
243bf215546Sopenharmony_ci            ('',     '2021-01-27', '20.3.4', 'Dylan Baker', 'This is the last planned release of the 20.3.x series.'),
244bf215546Sopenharmony_ci            ('21.0', '2021-01-13', '21.0.0-rc1', 'Dylan Baker', ''),
245bf215546Sopenharmony_ci            ('',     '2021-01-20', '21.0.0-rc2', 'Dylan Baker', gen_calendar_entries.OR_FINAL.format('21.0')),
246bf215546Sopenharmony_ci        ]
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_ci        args: gen_calendar_entries.ExtendArguments = argparse.Namespace()
249bf215546Sopenharmony_ci        args.series = '21.0'
250bf215546Sopenharmony_ci        args.count = 2
251bf215546Sopenharmony_ci
252bf215546Sopenharmony_ci        with mock_csv(data):
253bf215546Sopenharmony_ci            gen_calendar_entries.extend(args)
254bf215546Sopenharmony_ci            actual = gen_calendar_entries.read_calendar()
255bf215546Sopenharmony_ci
256bf215546Sopenharmony_ci        expected = data.copy()
257bf215546Sopenharmony_ci        d = list(expected[-1])
258bf215546Sopenharmony_ci        d[-1] = ''
259bf215546Sopenharmony_ci        expected[-1] = tuple(d)
260bf215546Sopenharmony_ci        expected.extend([
261bf215546Sopenharmony_ci            ('', '2021-01-27', '21.0.0-rc3', 'Dylan Baker', ''),
262bf215546Sopenharmony_ci            ('', '2021-02-03', '21.0.0-rc4', 'Dylan Baker', gen_calendar_entries.OR_FINAL.format('21.0')),
263bf215546Sopenharmony_ci        ])
264bf215546Sopenharmony_ci
265bf215546Sopenharmony_ci        assert actual == expected
266bf215546Sopenharmony_ci
267bf215546Sopenharmony_ci
268bf215546Sopenharmony_ciclass TestFinal:
269bf215546Sopenharmony_ci
270bf215546Sopenharmony_ci    @pytest.fixture(autouse=True, scope='class')
271bf215546Sopenharmony_ci    def _patch_date(self) -> typing.Iterator[None]:
272bf215546Sopenharmony_ci        mdate = mock.Mock()
273bf215546Sopenharmony_ci        mdate.today = mock.Mock(return_value=datetime.date(2021, 1, 6))
274bf215546Sopenharmony_ci        with mock.patch('bin.gen_calendar_entries.datetime.date', mdate):
275bf215546Sopenharmony_ci            yield
276bf215546Sopenharmony_ci
277bf215546Sopenharmony_ci    ORIGINAL_DATA = [
278bf215546Sopenharmony_ci        ('20.3', '2021-01-13', '20.3.3', 'Dylan Baker', ''),
279bf215546Sopenharmony_ci        ('',     '2021-01-27', '20.3.4', 'Dylan Baker', 'Last planned release of the 20.3.x series'),
280bf215546Sopenharmony_ci    ]
281bf215546Sopenharmony_ci
282bf215546Sopenharmony_ci    @pytest.fixture(autouse=True)
283bf215546Sopenharmony_ci    def csv(self) -> None:
284bf215546Sopenharmony_ci        """inject our test data.."""
285bf215546Sopenharmony_ci        with mock_csv(self.ORIGINAL_DATA):
286bf215546Sopenharmony_ci            yield
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_ci    def test_zero_released(self) -> None:
289bf215546Sopenharmony_ci        args: gen_calendar_entries.FinalArguments = argparse.Namespace()
290bf215546Sopenharmony_ci        args.manager = "Dylan Baker"
291bf215546Sopenharmony_ci        args.zero_released = True
292bf215546Sopenharmony_ci        args.series = '21.0'
293bf215546Sopenharmony_ci        gen_calendar_entries.final_release(args)
294bf215546Sopenharmony_ci
295bf215546Sopenharmony_ci        expected = self.ORIGINAL_DATA.copy()
296bf215546Sopenharmony_ci        expected.append(('21.0', '2021-01-20', f'21.0.1', 'Dylan Baker'))
297bf215546Sopenharmony_ci        expected.append((    '', '2021-02-03', f'21.0.2', 'Dylan Baker'))
298bf215546Sopenharmony_ci        expected.append((    '', '2021-02-17', f'21.0.3', 'Dylan Baker', gen_calendar_entries.LAST_RELEASE.format(args.series)))
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_ci        actual = gen_calendar_entries.read_calendar()
301bf215546Sopenharmony_ci
302bf215546Sopenharmony_ci        assert actual == expected
303bf215546Sopenharmony_ci
304bf215546Sopenharmony_ci    def test_zero_not_released(self) -> None:
305bf215546Sopenharmony_ci        args: gen_calendar_entries.FinalArguments = argparse.Namespace()
306bf215546Sopenharmony_ci        args.manager = "Dylan Baker"
307bf215546Sopenharmony_ci        args.zero_released = False
308bf215546Sopenharmony_ci        args.series = '21.0'
309bf215546Sopenharmony_ci        gen_calendar_entries.final_release(args)
310bf215546Sopenharmony_ci
311bf215546Sopenharmony_ci        expected = self.ORIGINAL_DATA.copy()
312bf215546Sopenharmony_ci        expected.append(('21.0', '2021-01-13', f'21.0.0', 'Dylan Baker'))
313bf215546Sopenharmony_ci        expected.append((    '', '2021-01-27', f'21.0.1', 'Dylan Baker'))
314bf215546Sopenharmony_ci        expected.append((    '', '2021-02-10', f'21.0.2', 'Dylan Baker'))
315bf215546Sopenharmony_ci        expected.append((    '', '2021-02-24', f'21.0.3', 'Dylan Baker', gen_calendar_entries.LAST_RELEASE.format(args.series)))
316bf215546Sopenharmony_ci
317bf215546Sopenharmony_ci        actual = gen_calendar_entries.read_calendar()
318bf215546Sopenharmony_ci
319bf215546Sopenharmony_ci        assert actual == expected
320