Skip to content

Commit

Permalink
Change: Use VersionCalculator directly in release CLI
Browse files Browse the repository at this point in the history
The initial idea from getting a VersionCalculator was to allow returning
a different calculator for different projects. Now it seems that this
wont be the case anymore and it might just end as a different CLI
argument.
  • Loading branch information
bjoernricks committed Mar 2, 2023
1 parent 7e41232 commit 829073f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
7 changes: 3 additions & 4 deletions pontos/release/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from pontos.version.errors import VersionError
from pontos.version.helper import get_last_release_version
from pontos.version.project import Project
from pontos.version.version import Version
from pontos.version.version import Version, VersionCalculator

from .helper import ReleaseType, find_signing_key, get_git_repository_name

Expand Down Expand Up @@ -60,7 +60,7 @@ def _get_release_version(
release_version: Optional[Version],
) -> Version:
current_version = project.get_current_version()
calculator = project.get_version_calculator()
calculator = VersionCalculator()
if release_type == ReleaseType.CALENDAR:
return calculator.next_calendar_version(current_version)

Expand Down Expand Up @@ -254,8 +254,7 @@ async def run(
return ReleaseReturnValue.CREATE_RELEASE_ERROR

if not next_version:
calculator = project.get_version_calculator()
next_version = calculator.next_dev_version(release_version)
next_version = VersionCalculator().next_dev_version(release_version)

try:
updated = project.update_version(next_version)
Expand Down
14 changes: 2 additions & 12 deletions pontos/version/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,14 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.


from typing import List, Literal, Type, Union
from typing import List, Literal, Union

from pontos.version.commands import get_commands
from pontos.version.errors import ProjectError
from pontos.version.version import (
Version,
VersionCalculator,
VersionCommand,
VersionUpdate,
)
from pontos.version.version import Version, VersionCommand, VersionUpdate


class Project:
version_calculator_class: Type[VersionCalculator] = VersionCalculator

def __init__(self, commands: List[VersionCommand]) -> None:
self._commands = commands

Expand All @@ -54,9 +47,6 @@ def gather_project(cls) -> "Project":

return cls(commands)

def get_version_calculator(self) -> VersionCalculator:
return self.version_calculator_class()

def update_version(
self, new_version: Version, *, force: bool = False
) -> VersionUpdate:
Expand Down
33 changes: 16 additions & 17 deletions tests/release/test_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# pylint: disable=too-many-lines
# pylint: disable=too-many-lines, line-too-long

import unittest
from datetime import datetime
Expand All @@ -31,7 +31,7 @@
from pontos.version.errors import VersionError
from pontos.version.go import GoVersionCommand
from pontos.version.project import Project
from pontos.version.version import Version, VersionCalculator, VersionUpdate
from pontos.version.version import Version, VersionUpdate


def mock_terminal() -> MagicMock:
Expand Down Expand Up @@ -171,7 +171,6 @@ def test_release_patch(
),
]
project_mock.get_current_version.return_value = current_version
project_mock.get_version_calculator.return_value = VersionCalculator()

_, token, args = parse_args(
[
Expand Down Expand Up @@ -234,6 +233,7 @@ def test_release_patch(
self.assertEqual(released, ReleaseReturnValue.SUCCESS)

@patch("pontos.release.release.Git", autospec=True)
@patch("pontos.release.release.VersionCalculator", autospec=True)
@patch(
"pontos.release.release.ReleaseCommand._create_release", autospec=True
)
Expand All @@ -247,6 +247,7 @@ def test_release_calendar(
gather_project_mock: MagicMock,
create_changelog_mock: MagicMock,
create_release_mock: AsyncMock,
version_calculator_mock: MagicMock,
git_mock: MagicMock,
):
current_version = Version("0.0.1")
Expand All @@ -268,9 +269,9 @@ def test_release_calendar(
),
]
project_mock.get_current_version.return_value = current_version
version_calculator = MagicMock(spec=VersionCalculator)
version_calculator.next_calendar_version.return_value = release_version
project_mock.get_version_calculator.return_value = version_calculator
version_calculator_mock.return_value.next_calendar_version.return_value = (
release_version
)

_, token, args = parse_args(
[
Expand Down Expand Up @@ -299,7 +300,7 @@ def test_release_calendar(
call(follow_tags=True, remote=None),
],
)
version_calculator.next_calendar_version.assert_called_once_with(
version_calculator_mock.return_value.next_calendar_version.assert_called_once_with(
current_version
)
project_mock.update_version.assert_has_calls(
Expand Down Expand Up @@ -370,7 +371,6 @@ def test_release_minor(
),
]
project_mock.get_current_version.return_value = current_version
project_mock.get_version_calculator.return_value = VersionCalculator()

_, token, args = parse_args(
[
Expand Down Expand Up @@ -467,7 +467,6 @@ def test_release_major(
),
]
project_mock.get_current_version.return_value = current_version
project_mock.get_version_calculator.return_value = VersionCalculator()

_, token, args = parse_args(
[
Expand Down Expand Up @@ -564,7 +563,6 @@ def test_release_alpha(
),
]
project_mock.get_current_version.return_value = current_version
project_mock.get_version_calculator.return_value = VersionCalculator()

_, token, args = parse_args(
[
Expand Down Expand Up @@ -659,7 +657,6 @@ def test_release_beta(
),
]
project_mock.get_current_version.return_value = current_version
project_mock.get_version_calculator.return_value = VersionCalculator()

_, token, args = parse_args(
[
Expand Down Expand Up @@ -754,7 +751,6 @@ def test_release_release_candidate(
),
]
project_mock.get_current_version.return_value = current_version
project_mock.get_version_calculator.return_value = VersionCalculator()

_, token, args = parse_args(
[
Expand Down Expand Up @@ -885,16 +881,19 @@ def test_no_release_error(self, gather_project_mock: MagicMock):

self.assertEqual(released, ReleaseReturnValue.NO_RELEASE_VERSION)

@patch("pontos.release.release.VersionCalculator", autospec=True)
@patch("pontos.release.release.Project.gather_project", autospec=True)
def test_no_release(self, gather_project_mock: MagicMock):
def test_no_release(
self,
gather_project_mock: MagicMock,
version_calculator_mock: MagicMock,
):
project_mock = MagicMock(spec=Project)
version_calculator_mock = MagicMock(spec=VersionCalculator)
gather_project_mock.return_value = project_mock
project_mock.get_current_version.return_value = None
project_mock.get_version_calculator.return_value = (
version_calculator_mock
version_calculator_mock.return_value.next_patch_version.return_value = (
None
)
version_calculator_mock.next_patch_version.return_value = None

_, token, args = parse_args(
[
Expand Down

0 comments on commit 829073f

Please sign in to comment.