From 5252b56756cca73f3467dcc6e9aff0a4a7ceab00 Mon Sep 17 00:00:00 2001 From: Jaspar S Date: Tue, 22 Aug 2023 11:28:44 +0200 Subject: [PATCH] Change: Support full year (YYYY) for calendar versioning (#858) --- pontos/version/_calculator.py | 24 ++++++++++++++---------- tests/version/schemes/test_pep440.py | 13 +++++++++++++ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/pontos/version/_calculator.py b/pontos/version/_calculator.py index d90ab406..2354d01b 100644 --- a/pontos/version/_calculator.py +++ b/pontos/version/_calculator.py @@ -59,33 +59,37 @@ def next_calendar_version(cls, current_version: Version) -> Version: today = datetime.today() current_year_short = today.year % 100 - if current_version.major < current_year_short or ( - current_version.major == current_year_short + if current_version.major > 2000: + # version expected to be YYYY.MM.P + current_year = today.year + else: + # version expected to be YY.MM.P + current_year = current_year_short + + if current_version.major < current_year or ( + current_version.major == current_year and current_version.minor < today.month ): - return cls.version_from_string( - f"{current_year_short}.{today.month}.0" - ) + return cls.version_from_string(f"{current_year}.{today.month}.0") if ( - current_version.major == today.year % 100 + current_version.major == current_year and current_version.minor == today.month ): if current_version.dev is None: release_version = cls.version_from_string( - f"{current_year_short}.{today.month}." + f"{current_year}.{today.month}." f"{current_version.patch + 1}" ) else: release_version = cls.version_from_string( - f"{current_year_short}.{today.month}." - f"{current_version.patch}" + f"{current_year}.{today.month}." f"{current_version.patch}" ) return release_version else: raise VersionError( f"'{current_version}' is higher than " - f"'{current_year_short}.{today.month}'." + f"'{current_year}.{today.month}'." ) @classmethod diff --git a/tests/version/schemes/test_pep440.py b/tests/version/schemes/test_pep440.py index bd3761bb..d75c2eb4 100644 --- a/tests/version/schemes/test_pep440.py +++ b/tests/version/schemes/test_pep440.py @@ -538,12 +538,20 @@ def test_next_calendar_versions(self): f"19.{today.month}.1.dev3", f"{year_short}.{today.month}.1.dev3", f"{year_short}.{today.month}.1", + "2022.4.1", + "2023.5.1", + f"{today.year}.{today.month}.1.dev2", + f"{today.year}.{today.month}.1", ] assert_versions = [ f"{year_short}.{today.month}.0", f"{year_short}.{today.month}.0", f"{year_short}.{today.month}.1", f"{year_short}.{today.month}.2", + f"{today.year}.{today.month}.0", + f"{today.year}.{today.month}.0", + f"{today.year}.{today.month}.1", + f"{today.year}.{today.month}.2", ] for current_version, assert_version in zip( @@ -571,6 +579,11 @@ def test_next_calendar_version_error(self): Version.from_string(f"{year_short}.{today.month + 1}.0") ) + with self.assertRaisesRegex(VersionError, "'.+' is higher than '.+'."): + calculator.next_calendar_version( + Version.from_string(f"{today.year + 1}.{today.month + 1}.0") + ) + def test_next_minor_version(self): calculator = VersionCalculator()