Path: blob/21.2-virgl/bin/gen_calendar_entries_test.py
4546 views
#!/usr/bin/env python31# SPDX-License-Identifier: MIT23# Copyright © 2021 Intel Corporation45# Permission is hereby granted, free of charge, to any person obtaining a copy6# of this software and associated documentation files (the "Software"), to deal7# in the Software without restriction, including without limitation the rights8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell9# copies of the Software, and to permit persons to whom the Software is10# furnished to do so, subject to the following conditions:1112# The above copyright notice and this permission notice shall be included in13# all copies or substantial portions of the Software.1415# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21# SOFTWARE.2223from __future__ import annotations24from unittest import mock25import argparse26import csv27import contextlib28import datetime29import tempfile30import os31import pathlib32import typing3334import pytest3536from . import gen_calendar_entries373839@contextlib.contextmanager40def mock_csv(data: typing.List[gen_calendar_entries.CalendarRowType]) -> typing.Iterator[None]:41"""Replace the actual CSV data with our test data."""42with tempfile.TemporaryDirectory() as d:43c = os.path.join(d, 'calendar.csv')44with open(c, 'w') as f:45writer = csv.writer(f)46writer.writerows(data)4748with mock.patch('bin.gen_calendar_entries.CALENDAR_CSV', pathlib.Path(c)):49yield505152@pytest.fixture(autouse=True, scope='module')53def disable_git_commits() -> None:54"""Mock out the commit function so no git commits are made durring testing."""55with mock.patch('bin.gen_calendar_entries.commit', mock.Mock()):56yield575859class TestReleaseStart:6061def test_first_is_wednesday(self) -> None:62d = gen_calendar_entries._calculate_release_start('20', '0')63assert d.day == 1564assert d.month == 165assert d.year == 20206667def test_first_is_before_wednesday(self) -> None:68d = gen_calendar_entries._calculate_release_start('19', '0')69assert d.day == 1670assert d.month == 171assert d.year == 20197273def test_first_is_after_wednesday(self) -> None:74d = gen_calendar_entries._calculate_release_start('21', '0')75assert d.day == 1376assert d.month == 177assert d.year == 2021787980class TestNextReleaseDate:8182@contextlib.contextmanager83def _patch_date(date: datetime.date) -> typing.Iterator[None]:84mdate = mock.Mock()85mdate.today = mock.Mock(return_value=date)86with mock.patch('bin.gen_calendar_entries.datetime.date', mdate):87yield8889class TestIsWeds:9091@pytest.fixture(scope='class', autouse=True)92def data(self) -> None:93date = datetime.date(2021, 1, 6)94with TestNextReleaseDate._patch_date(date):95yield9697@pytest.mark.parametrize(98'is_zero, expected',99[100(True, 13),101(False, 20),102],103)104def test(self, is_zero: bool, expected: int) -> None:105date = gen_calendar_entries._calculate_next_release_date(is_zero)106assert date.day == expected107108class TestBeforeWeds:109110@pytest.fixture(scope='class', autouse=True)111def data(self) -> None:112date = datetime.date(2021, 1, 5)113with TestNextReleaseDate._patch_date(date):114yield115116@pytest.mark.parametrize(117'is_zero, expected',118[119(True, 13),120(False, 20),121],122)123def test(self, is_zero: bool, expected: int) -> None:124date = gen_calendar_entries._calculate_next_release_date(is_zero)125assert date.day == expected126127class TestAfterWeds:128129@pytest.fixture(scope='class', autouse=True)130def data(self) -> None:131date = datetime.date(2021, 1, 8)132with TestNextReleaseDate._patch_date(date):133yield134135@pytest.mark.parametrize(136'is_zero, expected',137[138(True, 13),139(False, 20),140],141)142def test(self, is_zero: bool, expected: int) -> None:143date = gen_calendar_entries._calculate_next_release_date(is_zero)144assert date.day == expected145146147class TestRC:148149ORIGINAL_DATA = [150('20.3', '2021-01-13', '20.3.3', 'Dylan Baker', ''),151('', '2021-01-27', '20.3.4', 'Dylan Baker', 'Last planned release of the 20.3.x series'),152]153154@pytest.fixture(autouse=True, scope='class')155def mock_version(self) -> None:156"""Keep the version set at a specific value."""157with tempfile.TemporaryDirectory() as d:158v = os.path.join(d, 'version')159with open(v, 'w') as f:160f.write('21.0.0-devel\n')161162with mock.patch('bin.gen_calendar_entries.VERSION', pathlib.Path(v)):163yield164165@pytest.fixture(autouse=True)166def csv(self) -> None:167"""inject our test data.."""168with mock_csv(self.ORIGINAL_DATA):169yield170171def test_basic(self) -> None:172args: gen_calendar_entries.RCArguments = argparse.Namespace()173args.manager = "Dylan Baker"174gen_calendar_entries.release_candidate(args)175176expected = self.ORIGINAL_DATA.copy()177expected.append(('21.0', '2021-01-13', f'21.0.0-rc1', 'Dylan Baker'))178expected.append(( '', '2021-01-20', f'21.0.0-rc2', 'Dylan Baker'))179expected.append(( '', '2021-01-27', f'21.0.0-rc3', 'Dylan Baker'))180expected.append(( '', '2021-02-03', f'21.0.0-rc4', 'Dylan Baker', 'Or 21.0.0 final.'))181182actual = gen_calendar_entries.read_calendar()183184assert actual == expected185186187class TestExtend:188189def test_one_release(self) -> None:190data = [191('20.3', '2021-01-13', '20.3.3', 'Dylan Baker', ''),192('', '2021-01-27', '20.3.4', 'Dylan Baker', 'This is the last planned release of the 20.3.x series.'),193]194195args: gen_calendar_entries.ExtendArguments = argparse.Namespace()196args.series = '20.3'197args.count = 2198199with mock_csv(data):200gen_calendar_entries.extend(args)201actual = gen_calendar_entries.read_calendar()202203expected = [204data[0],205('', '2021-01-27', '20.3.4', 'Dylan Baker', ''),206('', '2021-02-10', '20.3.5', 'Dylan Baker', ''),207('', '2021-02-24', '20.3.6', 'Dylan Baker', 'This is the last planned release of the 20.3.x series.'),208]209210assert actual == expected211def test_one_release(self) -> None:212data = [213('20.3', '2021-01-13', '20.3.3', 'Dylan Baker', ''),214('', '2021-01-27', '20.3.4', 'Dylan Baker', 'This is the last planned release of the 20.3.x series.'),215('21.0', '2021-01-13', '21.0.1', 'Dylan Baker', ''),216('', '2021-01-27', '21.0.2', 'Dylan Baker', ''),217('', '2021-02-10', '21.0.3', 'Dylan Baker', ''),218('', '2021-02-24', '21.0.4', 'Dylan Baker', 'This is the last planned release of the 21.0.x series.'),219]220221args: gen_calendar_entries.ExtendArguments = argparse.Namespace()222args.series = '21.0'223args.count = 1224225with mock_csv(data):226gen_calendar_entries.extend(args)227actual = gen_calendar_entries.read_calendar()228229expected = data.copy()230d = list(data[-1])231d[-1] = ''232expected[-1] = tuple(d)233expected.extend([234('', '2021-03-10', '21.0.5', 'Dylan Baker', 'This is the last planned release of the 21.0.x series.'),235])236237assert actual == expected238239def test_rc(self) -> None:240data = [241('20.3', '2021-01-13', '20.3.3', 'Dylan Baker', ''),242('', '2021-01-27', '20.3.4', 'Dylan Baker', 'This is the last planned release of the 20.3.x series.'),243('21.0', '2021-01-13', '21.0.0-rc1', 'Dylan Baker', ''),244('', '2021-01-20', '21.0.0-rc2', 'Dylan Baker', gen_calendar_entries.OR_FINAL.format('21.0')),245]246247args: gen_calendar_entries.ExtendArguments = argparse.Namespace()248args.series = '21.0'249args.count = 2250251with mock_csv(data):252gen_calendar_entries.extend(args)253actual = gen_calendar_entries.read_calendar()254255expected = data.copy()256d = list(expected[-1])257d[-1] = ''258expected[-1] = tuple(d)259expected.extend([260('', '2021-01-27', '21.0.0-rc3', 'Dylan Baker', ''),261('', '2021-02-03', '21.0.0-rc4', 'Dylan Baker', gen_calendar_entries.OR_FINAL.format('21.0')),262])263264assert actual == expected265266267class TestFinal:268269@pytest.fixture(autouse=True, scope='class')270def _patch_date(self) -> typing.Iterator[None]:271mdate = mock.Mock()272mdate.today = mock.Mock(return_value=datetime.date(2021, 1, 6))273with mock.patch('bin.gen_calendar_entries.datetime.date', mdate):274yield275276ORIGINAL_DATA = [277('20.3', '2021-01-13', '20.3.3', 'Dylan Baker', ''),278('', '2021-01-27', '20.3.4', 'Dylan Baker', 'Last planned release of the 20.3.x series'),279]280281@pytest.fixture(autouse=True)282def csv(self) -> None:283"""inject our test data.."""284with mock_csv(self.ORIGINAL_DATA):285yield286287def test_zero_released(self) -> None:288args: gen_calendar_entries.FinalArguments = argparse.Namespace()289args.manager = "Dylan Baker"290args.zero_released = True291args.series = '21.0'292gen_calendar_entries.final_release(args)293294expected = self.ORIGINAL_DATA.copy()295expected.append(('21.0', '2021-01-20', f'21.0.1', 'Dylan Baker'))296expected.append(( '', '2021-02-03', f'21.0.2', 'Dylan Baker'))297expected.append(( '', '2021-02-17', f'21.0.3', 'Dylan Baker', gen_calendar_entries.LAST_RELEASE.format(args.series)))298299actual = gen_calendar_entries.read_calendar()300301assert actual == expected302303def test_zero_not_released(self) -> None:304args: gen_calendar_entries.FinalArguments = argparse.Namespace()305args.manager = "Dylan Baker"306args.zero_released = False307args.series = '21.0'308gen_calendar_entries.final_release(args)309310expected = self.ORIGINAL_DATA.copy()311expected.append(('21.0', '2021-01-13', f'21.0.0', 'Dylan Baker'))312expected.append(( '', '2021-01-27', f'21.0.1', 'Dylan Baker'))313expected.append(( '', '2021-02-10', f'21.0.2', 'Dylan Baker'))314expected.append(( '', '2021-02-24', f'21.0.3', 'Dylan Baker', gen_calendar_entries.LAST_RELEASE.format(args.series)))315316actual = gen_calendar_entries.read_calendar()317318assert actual == expected319320321