Skip to content

Commit

Permalink
fix: Fix bumping initial version
Browse files Browse the repository at this point in the history
This change also changes the behavior back to having the latest version be "Unreleased" if the bump option wasn't used.

Issue-82: #82
  • Loading branch information
pawamoy committed Apr 4, 2024
1 parent 38ef35b commit 37a3b62
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
22 changes: 8 additions & 14 deletions src/git_changelog/build.py
Expand Up @@ -295,9 +295,6 @@ def __init__(
if bump:
self._bump(bump)

# fix a single, initial version to the user specified version or 0.1.0 if none is specified
self._fix_single_version(bump)

def run_git(self, *args: str) -> str:
"""Run a git command in the chosen repository.
Expand Down Expand Up @@ -480,8 +477,11 @@ def _assign_previous_versions(self, versions_dict: dict[str, Version], previous_

def _bump(self, version: str) -> None:
last_version = self.versions_list[0]
if not last_version.tag and last_version.previous_version:
last_tag = last_version.previous_version.tag
if not last_version.tag:
if last_version.previous_version:
last_tag = last_version.previous_version.tag
else:
last_tag = self.version_bumper.initial
version, *plus = version.split("+")
if version == "auto":
# guess the next version number based on last version and recent commits
Expand All @@ -507,14 +507,8 @@ def _bump(self, version: str) -> None:
if self.provider:
last_version.url = self.provider.get_tag_url(tag=last_version.planned_tag)
last_version.compare_url = self.provider.get_compare_url(
base=last_version.previous_version.tag,
base=last_version.previous_version.tag
if last_version.previous_version
else last_version.commits[-1].hash,
target=last_version.planned_tag,
)

def _fix_single_version(self, version: str | None) -> None:
last_version = self.versions_list[0]
if len(self.versions_list) == 1 and last_version.planned_tag is None and not last_version.tag:
planned_tag = version if version and version not in {"auto", "major", "minor", "patch"} else "0.1.0"
last_version.tag = planned_tag
last_version.url += planned_tag
last_version.compare_url = last_version.compare_url.replace("HEAD", planned_tag)
6 changes: 6 additions & 0 deletions src/git_changelog/versioning.py
Expand Up @@ -645,6 +645,8 @@ def parse_pep440(version: str) -> tuple[PEP440Version, str]:
class VersionBumper:
"""Base class for version bumpers."""

initial: str

def __init__(self, strategies: tuple[str, ...]) -> None:
"""Initialize the bumper.
Expand All @@ -670,6 +672,8 @@ def __call__(self, version: str, strategy: str = ..., **kwargs: Any) -> str:
class PEP440Bumper(VersionBumper):
"""PEP 440 version bumper."""

initial: str = "0.0.0"

def __call__( # type: ignore[override]
self,
version: str,
Expand Down Expand Up @@ -744,6 +748,8 @@ def __call__( # type: ignore[override]
class SemVerBumper(VersionBumper):
"""SemVer version bumper."""

initial: str = "0.0.0"

def __call__( # type: ignore[override]
self,
version: str,
Expand Down
34 changes: 25 additions & 9 deletions tests/test_build.py
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

from time import sleep
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Literal

import pytest

Expand All @@ -16,20 +16,36 @@


@pytest.mark.parametrize(
("bump", "expected"),
[("auto", "0.1.0"), ("major", "0.1.0"), ("minor", "0.1.0"), ("1.1.1", "1.1.1")],
("versioning", "bump", "expected"),
[
("semver", "auto", "0.0.1"), # chore commit
("semver", "major", "1.0.0"),
("semver", "minor", "0.1.0"),
("semver", "patch", "0.0.1"),
("semver", "1.1.1", "1.1.1"),
("pep440", "auto", "0.0.1"), # chore commit
("pep440", "major", "1.0.0"),
("pep440", "minor+dev", "0.1.0.dev0"),
("pep440", "micro+alpha+dev", "0.0.1a0.dev0"),
("pep440", "1.1.1", "1.1.1"),
],
)
def test_bump_with_semver_on_new_repo(repo: GitRepo, bump: str, expected: str) -> None:
"""Bump to user specified version (SemVer) on new git repo.
def test_bump_with_semver_on_new_repo(
repo: GitRepo,
versioning: Literal["pep440", "semver"],
bump: str,
expected: str,
) -> None:
"""Bump to user specified version on new Git repo.
Parameters:
repo: GitRepo to a temporary repository.
bump: The bump parameter value.
expected: Expected version for the new changelog entry.
"""
changelog = Changelog(repo.path, convention=AngularConvention, bump=bump)
changelog = Changelog(repo.path, convention=AngularConvention, bump=bump, versioning=versioning, zerover=False)
assert len(changelog.versions_list) == 1
assert changelog.versions_list[0].tag == expected
assert changelog.versions_list[0].planned_tag == expected


@pytest.mark.parametrize("bump", ["auto", "major", "minor", "2.0.0"])
Expand Down Expand Up @@ -189,7 +205,7 @@ def _assert_version(
expected_prev_tag: str | None,
expected_commits: list[str],
) -> None:
assert version.tag == expected_tag
assert expected_tag in (version.tag, version.planned_tag)
if expected_prev_tag:
assert version.previous_version is not None, f"Expected previous version '{expected_prev_tag}', but was None"
assert version.previous_version.tag == expected_prev_tag
Expand Down Expand Up @@ -252,7 +268,7 @@ def test_merge_into_unreleased(repo: GitRepo) -> None:
version = changelog.versions_list[0]
_assert_version(
version,
expected_tag="0.1.0",
expected_tag="",
expected_prev_tag=None,
expected_commits=[commit_e, commit_c, commit_d, commit_a, commit_b],
)
Expand Down

0 comments on commit 37a3b62

Please sign in to comment.