Skip to content

Commit fe794dd

Browse files
committed
Added more tests for scm
1 parent ce9464c commit fe794dd

File tree

2 files changed

+57
-15
lines changed

2 files changed

+57
-15
lines changed

tests/conftest.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Testing fixtures for Pytest."""
2+
import subprocess
23
from contextlib import contextmanager
34
from pathlib import Path
45
from typing import Generator
@@ -46,3 +47,17 @@ def get_config_data(overrides: dict) -> tuple:
4647
version = version_config.parse(conf.current_version)
4748

4849
return conf, version_config, version
50+
51+
52+
@pytest.fixture
53+
def git_repo(tmp_path: Path) -> Path:
54+
"""Generate a simple temporary git repo and return the path."""
55+
subprocess.run(["git", "init"], cwd=tmp_path, check=True, capture_output=True)
56+
return tmp_path
57+
58+
59+
@pytest.fixture
60+
def hg_repo(tmp_path: Path) -> Path:
61+
"""Generate a simple temporary mercurial repo and return the path."""
62+
subprocess.run(["hg", "init"], cwd=tmp_path, check=True, capture_output=True)
63+
return tmp_path

tests/test_scm.py

+42-15
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,11 @@
33
from pathlib import Path
44

55
import pytest
6+
from pytest import param
67

78
from bumpversion import scm
89
from bumpversion.exceptions import DirtyWorkingDirectoryError
9-
from tests.conftest import inside_dir
10-
11-
12-
@pytest.fixture
13-
def git_repo(tmp_path: Path) -> Path:
14-
"""Generate a simple temporary git repo and return the path."""
15-
subprocess.run(["git", "init"], cwd=tmp_path)
16-
return tmp_path
17-
18-
19-
@pytest.fixture
20-
def hg_repo(tmp_path: Path) -> Path:
21-
"""Generate a simple temporary mercurial repo and return the path."""
22-
subprocess.run(["hg", "init"], cwd=tmp_path)
23-
return tmp_path
10+
from tests.conftest import get_config_data, inside_dir
2411

2512

2613
def test_git_is_usable(git_repo: Path) -> None:
@@ -88,3 +75,43 @@ def test_hg_is_usable(hg_repo: Path) -> None:
8875
"""Should return false if it is not a mercurial repo."""
8976
with inside_dir(hg_repo):
9077
assert scm.Mercurial.is_usable()
78+
79+
80+
@pytest.mark.parametrize(
81+
["repo", "scm_command", "scm_class"],
82+
[
83+
param("git_repo", "git", scm.Git, id="git"),
84+
param("hg_repo", "hg", scm.Mercurial, id="hg"),
85+
],
86+
)
87+
def test_commit_and_tag_from_below_scm_root(repo: str, scm_command: str, scm_class: scm.SourceCodeManager, request):
88+
# Arrange
89+
repo_path: Path = request.getfixturevalue(repo)
90+
version_path = repo_path / "VERSION"
91+
version_path.write_text("30.0.3")
92+
sub_dir_path = repo_path / "subdir"
93+
sub_dir_path.mkdir(exist_ok=True)
94+
95+
overrides = {"current_version": "30.0.3", "commit": True, "tag": True, "files": [{"filename": str(version_path)}]}
96+
context = {
97+
"current_version": "30.0.3",
98+
"new_version": "30.1.0",
99+
}
100+
with inside_dir(repo_path):
101+
conf, version_config, current_version = get_config_data(overrides)
102+
subprocess.run([scm_command, "add", "VERSION"], check=True, capture_output=True)
103+
subprocess.run([scm_command, "commit", "-m", "initial commit"], check=True, capture_output=True)
104+
with inside_dir(sub_dir_path):
105+
version_path.write_text("30.1.0")
106+
107+
# Act
108+
scm_class.commit_to_scm(files=[version_path], config=conf, context=context)
109+
scm_class.tag_in_scm(config=conf, context=context)
110+
111+
# Assert
112+
tag_info = scm_class.latest_tag_info("v*")
113+
if scm_command == "git":
114+
assert tag_info.commit_sha is not None
115+
assert tag_info.distance_to_latest_tag == 0
116+
assert tag_info.current_version == "30.1.0"
117+
assert tag_info.dirty is False

0 commit comments

Comments
 (0)