#!/usr/bin/env -S uv run --script # /// script # dependencies = ["pytest"] # /// from pathlib import Path import subprocess import sys import pytest import bump_version as script @pytest.mark.parametrize("tags, part, expected", [ ([], "patch", "v0.0.1"), ([], "minor", "v0.1.0"), ([], "major", "v1.0.0"), (["v1.9.9", "v1.10.2", "v1.2.3"], "patch", "v1.10.3"), (["v1.9.9", "v1.10.2"], "minor", "v1.11.0"), (["v1.9.9", "v1.10.2"], "major", "v2.0.0"), (["1.2.3", "v1.0.0"], "patch", "1.2.4"), (["release", "v9.0.0-rc.1", "v01.0.0", "v1.2.3+build", "v1.2.3"], "patch", "v1.2.4"), ]) def test_next_tag(tags: list[str], part: str, expected: str) -> None: assert script.next_tag(tags=tags, part=part) == expected @pytest.mark.parametrize("push", [False, True]) def test_create_and_push_tag(tmp_path: Path, push: bool) -> None: repository = tmp_path / "repository" remote = tmp_path / "remote.git" subprocess.run(args=["git", "init", "--initial-branch=master", str(repository)], check=True, capture_output=True) subprocess.run(args=["git", "init", "--bare", str(remote)], check=True, capture_output=True) script.git(cwd=repository, arguments=["-c", "user.name=Test", "-c", "user.email=test@example.com", "-c", "commit.gpgsign=false", "commit", "--allow-empty", "--message=Initial"]) script.git(cwd=repository, arguments=["remote", "add", "origin", str(remote)]) script.git(cwd=repository, arguments=["tag", "v1.9.9"]) script.git(cwd=repository, arguments=["tag", "v1.10.2"]) command = [sys.executable, str(Path(script.__file__).resolve()), "--minor", "--cwd", str(repository)] if push: command.append("--push") subprocess.run(args=command, check=True, capture_output=True, text=True) assert script.git(cwd=repository, arguments=["rev-parse", "v1.11.0"]) == script.git( cwd=repository, arguments=["rev-parse", "HEAD"], ) assert script.git(cwd=remote, arguments=["tag", "--list"]) == ("v1.11.0" if push else "") assert script.git(cwd=remote, arguments=["for-each-ref", "refs/heads"]) == "" @pytest.mark.parametrize("arguments", [[], ["--major", "--patch"]]) def test_requires_exactly_one_bump(arguments: list[str]) -> None: result = subprocess.run( args=[sys.executable, str(Path(script.__file__).resolve()), *arguments], capture_output=True, ) assert result.returncode == 2 def test_missing_repository(tmp_path: Path) -> None: result = subprocess.run( args=[sys.executable, str(Path(script.__file__).resolve()), "--patch", "--cwd", str(tmp_path)], capture_output=True, text=True, ) assert result.returncode == 1 assert "Git failed:" in result.stderr if __name__ == "__main__": sys.exit(pytest.main(args=[str(Path(__file__).resolve()), *sys.argv[1:]]))