Skip to content

Commit

Permalink
Change: Require a version parsing function for get_last_release_version
Browse files Browse the repository at this point in the history
When using a versioning scheme the resulting version of the
git_last_release_version depends on the versioning scheme. Therefore
allow to pass a version parsing function to get_last_release_version.

Also require to pass git_tag_prefix as keyword now because it is an
optional argument.
  • Loading branch information
bjoernricks authored and y0urself committed Mar 14, 2023
1 parent 531b6fe commit 3c09b27
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
7 changes: 4 additions & 3 deletions pontos/version/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@

from pontos.git import Git, TagSort

from .version import Version, parse_version
from .version import ParseVersionFuncType, Version


def get_last_release_version(
git_tag_prefix: Optional[str] = "",
parse_version: ParseVersionFuncType,
*,
git_tag_prefix: Optional[str] = "",
ignore_pre_releases: Optional[bool] = False,
) -> Optional[Version]:
"""Get the last released Version from git.
Expand All @@ -48,7 +49,7 @@ def get_last_release_version(
last_release_version = last_release_version.strip(git_tag_prefix)

version = parse_version(last_release_version)
if not version.is_prerelease or not ignore_pre_releases:
if not version.is_pre_release or not ignore_pre_releases:
return version

tag_list = tag_list[:-1]
Expand Down
49 changes: 27 additions & 22 deletions tests/version/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,39 @@

from pontos.git.git import Git
from pontos.version.helper import get_last_release_version
from pontos.version.version import Version
from pontos.version.schemes import PEP440VersioningScheme

parse_version = PEP440VersioningScheme.parse_version
Version = PEP440VersioningScheme.version_cls


class GetLastReleaseVersionTestCase(unittest.TestCase):
@patch("pontos.version.helper.Git", spec=Git)
def test_get_last_release_version(self, _git_interface_mock):
git_interface = _git_interface_mock.return_value
def test_get_last_release_version(self, git_mock):
git_interface = git_mock.return_value
git_interface.list_tags.return_value = ["1", "2", "3.55"]
self.assertEqual(get_last_release_version(), Version("3.55"))
self.assertEqual(
get_last_release_version(parse_version), Version("3.55")
)

@patch("pontos.version.helper.Git", spec=Git)
def test_get_no_release_version(self, _git_interface_mock):
git_interface = _git_interface_mock.return_value
def test_get_no_release_version(self, git_mock):
git_interface = git_mock.return_value
git_interface.list_tags.return_value = []
self.assertIsNone(get_last_release_version())
self.assertIsNone(get_last_release_version(parse_version))

@patch("pontos.version.helper.Git", spec=Git)
def test_get_last_release_version_with_git_prefix(
self, _git_interface_mock
):
git_interface = _git_interface_mock.return_value
def test_get_last_release_version_with_git_prefix(self, git_mock):
git_interface = git_mock.return_value
git_interface.list_tags.return_value = ["v1", "v2", "v3.55"]
self.assertEqual(get_last_release_version("v"), Version("3.55"))
self.assertEqual(
get_last_release_version(parse_version, git_tag_prefix="v"),
Version("3.55"),
)

@patch("pontos.version.helper.Git", spec=Git)
def test_get_last_release_version_ignore_pre_releases(
self, _git_interface_mock
):
git_interface = _git_interface_mock.return_value
def test_get_last_release_version_ignore_pre_releases(self, git_mock):
git_interface = git_mock.return_value
git_interface.list_tags.return_value = [
"1",
"2",
Expand All @@ -58,18 +62,19 @@ def test_get_last_release_version_ignore_pre_releases(
"4.0.1b1",
]
self.assertEqual(
get_last_release_version(ignore_pre_releases=True), Version("2")
get_last_release_version(parse_version, ignore_pre_releases=True),
Version("2"),
)

@patch("pontos.version.helper.Git", spec=Git)
def test_get_last_release_version_no_non_pre_release(
self, _git_interface_mock
):
git_interface = _git_interface_mock.return_value
def test_get_last_release_version_no_non_pre_release(self, git_mock):
git_interface = git_mock.return_value
git_interface.list_tags.return_value = [
"3.55a1",
"3.56.dev1",
"4.0.0rc1",
"4.0.1b1",
]
self.assertIsNone(get_last_release_version(ignore_pre_releases=True))
self.assertIsNone(
get_last_release_version(parse_version, ignore_pre_releases=True)
)

0 comments on commit 3c09b27

Please sign in to comment.