Skip to content

Commit

Permalink
fix: Use provided version when creating first changelog entry
Browse files Browse the repository at this point in the history
PR-69: #69
  • Loading branch information
chme authored Mar 6, 2024
1 parent e6f887e commit dd264cc
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/git_changelog/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ def __init__(
if bump:
self._bump(bump)

# fix a single, initial version to 0.1.0
self._fix_single_version()
# 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 @@ -441,10 +441,10 @@ def _bump(self, version: str) -> None:
target=last_version.planned_tag,
)

def _fix_single_version(self) -> None:
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:
planned_tag = "0.1.0"
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)
87 changes: 87 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""Tests for the `build` module."""

from __future__ import annotations

import random
import shutil
import subprocess
import uuid
from typing import TYPE_CHECKING

import pytest

from git_changelog import Changelog
from git_changelog.commit import AngularConvention

if TYPE_CHECKING:
from pathlib import Path


class GitRepo:
"""Test utility class to initalize and work with a git repository."""

def __init__(self, repo: Path) -> None:
"""Initialization method.
Initializes a new git repository under the given path.
Arguments:
repo: Path to the git repository.
"""
self.path = repo
self._git("init")
self._git("config", "user.name", "dummy")
self._git("config", "user.email", "[email protected]")
self._git("remote", "add", "origin", "[email protected]:example/example")
self.commit("chore: Initial repository creation")

def commit(self, message: str) -> str:
"""Create, add and commit a new file into the git repository.
Parameters:
message: The commit message.
Returns:
The git commit hash.
"""
with self.path.joinpath(str(uuid.uuid4())).open("a") as fh:
fh.write(str(random.randint(0, 1))) # noqa: S311
self._git("add", "-A")
self._git("commit", "-m", message)
return self._git("rev-parse", "HEAD")

def _git(self, *args: str) -> str:
return subprocess.check_output(
[shutil.which("git") or "git", "-C", str(self.path), *args], # noqa: S603
text=True,
)


@pytest.fixture(name="repo")
def git_repo(tmp_path: Path) -> GitRepo:
"""Pytest fixture setting up a temporary Git repository.
Parameters:
tmp_path: Path to a temporary directory (pytest fixture).
Yields:
A Git repository wrapper instance.
"""
return GitRepo(tmp_path)


@pytest.mark.parametrize(
("bump", "expected"),
[("auto", "0.1.0"), ("major", "0.1.0"), ("minor", "0.1.0"), ("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.
Parameters:
repo: GitRepo to a temporary repository.
"""
changelog = Changelog(repo.path, convention=AngularConvention, bump=bump)

assert len(changelog.versions_list) == 1
tag = changelog.versions_list[0].tag
assert tag == expected

0 comments on commit dd264cc

Please sign in to comment.