diff --git a/tests/conftest.py b/tests/conftest.py index 9d62d86b..377eaab6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,5 @@ """Testing fixtures for Pytest.""" +import subprocess from contextlib import contextmanager from pathlib import Path from typing import Generator @@ -46,3 +47,17 @@ def get_config_data(overrides: dict) -> tuple: version = version_config.parse(conf.current_version) return conf, version_config, version + + +@pytest.fixture +def git_repo(tmp_path: Path) -> Path: + """Generate a simple temporary git repo and return the path.""" + subprocess.run(["git", "init"], cwd=tmp_path, check=True, capture_output=True) + return tmp_path + + +@pytest.fixture +def hg_repo(tmp_path: Path) -> Path: + """Generate a simple temporary mercurial repo and return the path.""" + subprocess.run(["hg", "init"], cwd=tmp_path, check=True, capture_output=True) + return tmp_path diff --git a/tests/test_scm.py b/tests/test_scm.py index 168c2c44..aa6cb495 100644 --- a/tests/test_scm.py +++ b/tests/test_scm.py @@ -3,24 +3,11 @@ from pathlib import Path import pytest +from pytest import param from bumpversion import scm from bumpversion.exceptions import DirtyWorkingDirectoryError -from tests.conftest import inside_dir - - -@pytest.fixture -def git_repo(tmp_path: Path) -> Path: - """Generate a simple temporary git repo and return the path.""" - subprocess.run(["git", "init"], cwd=tmp_path) - return tmp_path - - -@pytest.fixture -def hg_repo(tmp_path: Path) -> Path: - """Generate a simple temporary mercurial repo and return the path.""" - subprocess.run(["hg", "init"], cwd=tmp_path) - return tmp_path +from tests.conftest import get_config_data, inside_dir def test_git_is_usable(git_repo: Path) -> None: @@ -88,3 +75,43 @@ def test_hg_is_usable(hg_repo: Path) -> None: """Should return false if it is not a mercurial repo.""" with inside_dir(hg_repo): assert scm.Mercurial.is_usable() + + +@pytest.mark.parametrize( + ["repo", "scm_command", "scm_class"], + [ + param("git_repo", "git", scm.Git, id="git"), + param("hg_repo", "hg", scm.Mercurial, id="hg"), + ], +) +def test_commit_and_tag_from_below_scm_root(repo: str, scm_command: str, scm_class: scm.SourceCodeManager, request): + # Arrange + repo_path: Path = request.getfixturevalue(repo) + version_path = repo_path / "VERSION" + version_path.write_text("30.0.3") + sub_dir_path = repo_path / "subdir" + sub_dir_path.mkdir(exist_ok=True) + + overrides = {"current_version": "30.0.3", "commit": True, "tag": True, "files": [{"filename": str(version_path)}]} + context = { + "current_version": "30.0.3", + "new_version": "30.1.0", + } + with inside_dir(repo_path): + conf, version_config, current_version = get_config_data(overrides) + subprocess.run([scm_command, "add", "VERSION"], check=True, capture_output=True) + subprocess.run([scm_command, "commit", "-m", "initial commit"], check=True, capture_output=True) + with inside_dir(sub_dir_path): + version_path.write_text("30.1.0") + + # Act + scm_class.commit_to_scm(files=[version_path], config=conf, context=context) + scm_class.tag_in_scm(config=conf, context=context) + + # Assert + tag_info = scm_class.latest_tag_info("v*") + if scm_command == "git": + assert tag_info.commit_sha is not None + assert tag_info.distance_to_latest_tag == 0 + assert tag_info.current_version == "30.1.0" + assert tag_info.dirty is False