Skip to content

Commit

Permalink
Change: Move get_last_release_version to the pontos.version module
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernricks committed Mar 1, 2023
1 parent 70530ee commit a011aa6
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 59 deletions.
2 changes: 1 addition & 1 deletion pontos/changelog/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

from pontos.changelog.conventional_commits import ChangelogBuilder
from pontos.errors import PontosError
from pontos.release.helper import get_last_release_version
from pontos.terminal.null import NullTerminal
from pontos.terminal.rich import RichTerminal
from pontos.version.helper import get_last_release_version


def parse_args(args: Iterable[str] = None) -> ArgumentParser:
Expand Down
22 changes: 0 additions & 22 deletions pontos/release/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from enum import Enum
from typing import Optional

from pontos.git import Git, GitError
from pontos.git.git import TagSort
from pontos.terminal import Terminal

DEFAULT_TIMEOUT = 1000
Expand All @@ -37,26 +35,6 @@ class ReleaseType(Enum):
RELEASE_CANDIDATE = "release-candidate"


def get_last_release_version(
git_tag_prefix: Optional[str] = "",
) -> Optional[str]:
"""Get the last released Version from git.
Returns:
Last released git-tag if tags were found
or None
"""

git_interface = Git()
tag_list = git_interface.list_tags(sort=TagSort.VERSION)

if not tag_list:
return None

last_release_version = tag_list[-1]
return last_release_version.strip(git_tag_prefix)


def get_git_repository_name(
remote: str = "origin",
) -> str:
Expand Down
8 changes: 2 additions & 6 deletions pontos/release/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,10 @@
from pontos.terminal import Terminal
from pontos.version.commands import gather_project
from pontos.version.errors import VersionError
from pontos.version.helper import get_last_release_version
from pontos.version.version import VersionCommand

from .helper import (
ReleaseType,
find_signing_key,
get_git_repository_name,
get_last_release_version,
)
from .helper import ReleaseType, find_signing_key, get_git_repository_name


class ReleaseReturnValue(IntEnum):
Expand Down
3 changes: 2 additions & 1 deletion pontos/release/sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
from pontos.helper import AsyncDownloadProgressIterable
from pontos.terminal import Terminal
from pontos.terminal.rich import RichTerminal
from pontos.version.helper import get_last_release_version

from .helper import get_git_repository_name, get_last_release_version
from .helper import get_git_repository_name


class SignReturnValue(IntEnum):
Expand Down
27 changes: 26 additions & 1 deletion pontos/version/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
#

import re
from typing import Optional

from packaging.version import InvalidVersion, Version
from packaging.version import InvalidVersion

from pontos.git import Git, TagSort
from pontos.version.version import Version, parse_version


def strip_version(version: str) -> str:
Expand Down Expand Up @@ -70,3 +74,24 @@ def versions_equal(new_version: str, old_version: str) -> bool:
Checks if new_version and old_version are equal
"""
return safe_version(old_version) == safe_version(new_version)


def get_last_release_version(
git_tag_prefix: Optional[str] = "",
) -> Optional[Version]:
"""Get the last released Version from git.
Returns:
Last released git-tag if tags were found
or None
"""

tag_list = Git().list_tags(sort=TagSort.VERSION)

if not tag_list:
return None

last_release_version = tag_list[-1]
last_release_version = last_release_version.strip(git_tag_prefix)

return parse_version(last_release_version)
30 changes: 2 additions & 28 deletions tests/release/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@
import unittest
from pathlib import Path
from typing import Generator
from unittest.mock import MagicMock, patch
from unittest.mock import MagicMock

from pontos.git.git import ConfigScope, Git, GitError, exec_git
from pontos.release.helper import (
find_signing_key,
get_git_repository_name,
get_last_release_version,
)
from pontos.release.helper import find_signing_key, get_git_repository_name
from pontos.testing import temp_git_repository


Expand Down Expand Up @@ -94,25 +90,3 @@ def test_find_no_signing_key(self):
git.config(
"user.signingkey", saved_key, scope=ConfigScope.GLOBAL
)


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

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

@patch("pontos.release.helper.Git", spec=Git)
def test_get_last_release_version_with_git_prefix(
self, _git_interface_mock
):
git_interface = _git_interface_mock.return_value
git_interface.list_tags.return_value = ["v1", "v2", "v3.55"]
self.assertEqual(get_last_release_version("v"), "3.55")
26 changes: 26 additions & 0 deletions tests/version/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import unittest
from unittest.mock import patch

from pontos.git.git import Git
from pontos.version.helper import (
get_last_release_version,
is_version_pep440_compliant,
safe_version,
strip_version,
)
from pontos.version.version import Version


class IsVersionPep440CompliantTestCase(unittest.TestCase):
Expand Down Expand Up @@ -90,3 +94,25 @@ def test_version_string_without_v(self):
def test_version_string_with_v(self):
self.assertEqual(strip_version("v1.2.3"), "1.2.3")
self.assertEqual(strip_version("v1.2.3dev"), "1.2.3dev")


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
git_interface.list_tags.return_value = ["1", "2", "3.55"]
self.assertEqual(get_last_release_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
git_interface.list_tags.return_value = []
self.assertIsNone(get_last_release_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
git_interface.list_tags.return_value = ["v1", "v2", "v3.55"]
self.assertEqual(get_last_release_version("v"), Version("3.55"))

0 comments on commit a011aa6

Please sign in to comment.