Skip to content

Commit 951bf29

Browse files
committed
class-based DistroVersion
1 parent f7244d8 commit 951bf29

File tree

3 files changed

+47
-33
lines changed

3 files changed

+47
-33
lines changed

src/gardenlinux/distro_version/__init__.py

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,55 @@ class NotAPatchRelease(Exception):
99
pass
1010

1111

12-
class DistroVersion:
13-
major: Optional[int] = None
14-
minor: Optional[int] = None
15-
patch: Optional[int] = None
12+
def DistroVersion(maybe_distro_version):
13+
version_components = maybe_distro_version.split(".")
14+
if len(version_components) > 3 or len(version_components) < 2:
15+
raise UnsupportedDistroVersion(
16+
f"Unexpected version number format {maybe_distro_version}"
17+
)
18+
19+
if not all(map(lambda x: x.isdigit(), version_components)):
20+
raise UnsupportedDistroVersion(
21+
f"Unexpected version number format {maybe_distro_version}"
22+
)
1623

17-
def __init__(self, maybe_version_str):
18-
version_components = maybe_version_str.split(".")
19-
if len(version_components) > 3 or len(version_components) < 2:
20-
raise UnsupportedDistroVersion(
21-
f"Unexpected version number format {maybe_version_str}"
22-
)
24+
if len(version_components) == 2:
25+
return LegacyDistroVersion(*(int(c) for c in version_components))
26+
if len(version_components) == 3:
27+
return SemverDistroVersion(*(int(c) for c in version_components))
2328

24-
if not all(map(lambda x: x.isdigit(), version_components)):
25-
raise UnsupportedDistroVersion(
26-
f"Unexpected version number format {maybe_version_str}"
27-
)
2829

29-
self.major = int(version_components[0])
30+
class BaseDistroVersion:
31+
def is_patch_release(self):
32+
return self.patch and self.patch > 0
3033

31-
if len(version_components) == 2:
32-
self.patch = int(version_components[1])
3334

34-
if len(version_components) == 3:
35-
self.minor = int(version_components[1])
36-
self.patch = int(version_components[2])
35+
class LegacyDistroVersion(BaseDistroVersion):
36+
def __init__(self, major, patch):
37+
self.major = major
38+
self.patch = patch
3739

3840
def __str__(self):
39-
return (
40-
f"{self.major}.{self.minor}.{self.patch - 1}"
41-
if self.minor
42-
else f"{self.major}.{self.patch - 1}"
43-
)
41+
return f"{self.major}.{self.patch}"
4442

45-
def is_patch_release(self):
46-
return self.patch > 0
43+
def previous_patch_release(self):
44+
if not self.is_patch_release():
45+
raise NotAPatchRelease(f"{self} is not a patch release")
46+
47+
return LegacyDistroVersion(self.major, self.patch - 1)
48+
49+
50+
class SemverDistroVersion(BaseDistroVersion):
51+
def __init__(self, major, minor, patch):
52+
self.major = major
53+
self.minor = minor
54+
self.patch = patch
55+
56+
def __str__(self):
57+
return f"{self.major}.{self.minor}.{self.patch}"
4758

4859
def previous_patch_release(self):
4960
if not self.is_patch_release():
5061
raise NotAPatchRelease(f"{self} is not a patch release")
51-
return self.__str__()
62+
63+
return SemverDistroVersion(self.major, self.minor, self.patch - 1)

src/gardenlinux/github/release_notes/sections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def release_notes_compare_package_versions_section(gardenlinux_version, package_
107107
output = ""
108108

109109
if version.is_patch_release():
110-
previous_version = version.previous_patch_release()
110+
previous_version = f"{version.previous_patch_release()}"
111111

112112
output += f"## Changes in Package Versions Compared to {previous_version}\n"
113113
output += compare_apt_repo_versions(previous_version, gardenlinux_version)

tests/distro_version/test_distro_version.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
from gardenlinux.distro_version import (
44
DistroVersion,
5+
LegacyDistroVersion,
56
NotAPatchRelease,
7+
SemverDistroVersion,
68
UnsupportedDistroVersion,
79
)
810

@@ -25,11 +27,11 @@ def test_distro_version_unrecognizable_too_short_version():
2527

2628

2729
def test_distro_version_legacy_version_is_parsable():
28-
assert isinstance(DistroVersion("1.2"), DistroVersion)
30+
assert isinstance(DistroVersion("1.2"), LegacyDistroVersion)
2931

3032

3133
def test_distro_version_semver_version_is_parsable():
32-
assert isinstance(DistroVersion("1.2.3"), DistroVersion)
34+
assert isinstance(DistroVersion("1.2.3"), SemverDistroVersion)
3335

3436

3537
def test_distro_version_patch_release_is_recognized():
@@ -40,8 +42,8 @@ def test_distro_version_patch_release_is_recognized():
4042

4143

4244
def test_distro_version_previous_patch_release_is_recognized():
43-
assert DistroVersion("1.1").previous_patch_release() == "1.0"
44-
assert DistroVersion("1.1.100").previous_patch_release() == "1.1.99"
45+
assert DistroVersion("1.1").previous_patch_release().__str__() == "1.0"
46+
assert DistroVersion("1.1.100").previous_patch_release().__str__() == "1.1.99"
4547
with pytest.raises(NotAPatchRelease):
4648
DistroVersion("1.0").previous_patch_release()
4749
with pytest.raises(NotAPatchRelease):

0 commit comments

Comments
 (0)