Skip to content

Commit f7244d8

Browse files
committed
DistroVersion class
1 parent fc2d93a commit f7244d8

File tree

4 files changed

+110
-31
lines changed

4 files changed

+110
-31
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from typing import Optional
2+
3+
4+
class UnsupportedDistroVersion(Exception):
5+
pass
6+
7+
8+
class NotAPatchRelease(Exception):
9+
pass
10+
11+
12+
class DistroVersion:
13+
major: Optional[int] = None
14+
minor: Optional[int] = None
15+
patch: Optional[int] = None
16+
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+
)
23+
24+
if not all(map(lambda x: x.isdigit(), version_components)):
25+
raise UnsupportedDistroVersion(
26+
f"Unexpected version number format {maybe_version_str}"
27+
)
28+
29+
self.major = int(version_components[0])
30+
31+
if len(version_components) == 2:
32+
self.patch = int(version_components[1])
33+
34+
if len(version_components) == 3:
35+
self.minor = int(version_components[1])
36+
self.patch = int(version_components[2])
37+
38+
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+
)
44+
45+
def is_patch_release(self):
46+
return self.patch > 0
47+
48+
def previous_patch_release(self):
49+
if not self.is_patch_release():
50+
raise NotAPatchRelease(f"{self} is not a patch release")
51+
return self.__str__()

src/gardenlinux/github/release_notes/sections.py

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from yaml import SafeLoader
77

88
from gardenlinux.constants import GLVD_BASE_URL, REQUESTS_TIMEOUTS
9+
from gardenlinux.distro_version import DistroVersion
910
from gardenlinux.logger import LoggerSetup
1011

1112
from .deployment_platform.ali_cloud import AliCloud
@@ -102,34 +103,15 @@ def release_notes_software_components_section(package_list):
102103

103104

104105
def release_notes_compare_package_versions_section(gardenlinux_version, package_list):
105-
version_components = gardenlinux_version.split(".")
106-
if len(version_components) > 3 or len(version_components) < 2:
107-
LOGGER.error(f"Unexpected version number format {gardenlinux_version}")
108-
return ""
109-
if not all(map(lambda x: x.isdigit(), version_components)):
110-
LOGGER.error(f"Unexpected version number format {gardenlinux_version}")
111-
return ""
112-
113-
major = int(version_components[0])
114-
minor = None
115-
patch = None
116-
117-
if len(version_components) == 2:
118-
patch = int(version_components[1])
119-
if len(version_components) == 3:
120-
minor = int(version_components[1])
121-
patch = int(version_components[2])
122-
106+
version = DistroVersion(gardenlinux_version)
123107
output = ""
124108

125-
if patch > 0:
126-
previous_version = (
127-
f"{major}.{minor}.{patch - 1}" if minor else f"{major}.{patch - 1}"
128-
)
109+
if version.is_patch_release():
110+
previous_version = version.previous_patch_release()
129111

130112
output += f"## Changes in Package Versions Compared to {previous_version}\n"
131113
output += compare_apt_repo_versions(previous_version, gardenlinux_version)
132-
elif patch == 0:
114+
else:
133115
output += (
134116
f"## Full List of Packages in Garden Linux version {gardenlinux_version}\n"
135117
)
@@ -138,8 +120,8 @@ def release_notes_compare_package_versions_section(gardenlinux_version, package_
138120
output += "\n"
139121
for entry in package_list.values():
140122
output += f"{entry!r}\n"
141-
output += "</pre>"
142-
output += "\n</details>\n\n"
123+
output += "</pre>"
124+
output += "\n</details>\n\n"
143125

144126
return output
145127

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import pytest
2+
3+
from gardenlinux.distro_version import (
4+
DistroVersion,
5+
NotAPatchRelease,
6+
UnsupportedDistroVersion,
7+
)
8+
9+
10+
def test_distro_version_unrecognizable_non_numeric_version():
11+
with pytest.raises(UnsupportedDistroVersion):
12+
DistroVersion("garden.linux")
13+
14+
15+
def test_distro_version_unrecognizable_numeric_version():
16+
with pytest.raises(UnsupportedDistroVersion):
17+
DistroVersion("1.2.3.4")
18+
with pytest.raises(UnsupportedDistroVersion):
19+
DistroVersion("1.100.-10")
20+
21+
22+
def test_distro_version_unrecognizable_too_short_version():
23+
with pytest.raises(UnsupportedDistroVersion):
24+
DistroVersion("1")
25+
26+
27+
def test_distro_version_legacy_version_is_parsable():
28+
assert isinstance(DistroVersion("1.2"), DistroVersion)
29+
30+
31+
def test_distro_version_semver_version_is_parsable():
32+
assert isinstance(DistroVersion("1.2.3"), DistroVersion)
33+
34+
35+
def test_distro_version_patch_release_is_recognized():
36+
assert DistroVersion("1.1").is_patch_release()
37+
assert DistroVersion("1.1.100").is_patch_release()
38+
assert not DistroVersion("1.0").is_patch_release()
39+
assert not DistroVersion("1.0.0").is_patch_release()
40+
41+
42+
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+
with pytest.raises(NotAPatchRelease):
46+
DistroVersion("1.0").previous_patch_release()
47+
with pytest.raises(NotAPatchRelease):
48+
DistroVersion("1.100.0").previous_patch_release()

tests/github/test_create_github_release_notes.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from moto import mock_aws
55

66
from gardenlinux.constants import GLVD_BASE_URL
7+
from gardenlinux.distro_version import UnsupportedDistroVersion
78
from gardenlinux.github.release_notes import (
89
release_notes_changes_section,
910
release_notes_compare_package_versions_section,
@@ -104,12 +105,9 @@ def mock_compare_apt_repo_versions(previous_version, current_version):
104105
), "Semver patch releases are supported"
105106

106107

107-
def test_release_notes_compare_package_versions_section_unrecognizable_version(caplog):
108-
assert release_notes_compare_package_versions_section("garden.linux", {}) == ""
109-
assert any(
110-
"Unexpected version number format garden.linux" in record.message
111-
for record in caplog.records
112-
), "Expected an error log message"
108+
def test_release_notes_compare_package_versions_section_unrecognizable_version():
109+
with pytest.raises(UnsupportedDistroVersion):
110+
release_notes_compare_package_versions_section("garden.linux", {})
113111

114112

115113
@pytest.mark.parametrize("flavor", TEST_FLAVORS)

0 commit comments

Comments
 (0)