Skip to content

Commit

Permalink
Added more tests for scm
Browse files Browse the repository at this point in the history
  • Loading branch information
coordt committed Apr 10, 2023
1 parent ce9464c commit fe794dd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Testing fixtures for Pytest."""
import subprocess
from contextlib import contextmanager
from pathlib import Path
from typing import Generator
Expand Down Expand Up @@ -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
57 changes: 42 additions & 15 deletions tests/test_scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

0 comments on commit fe794dd

Please sign in to comment.