Skip to content

Commit e35eee1

Browse files
committed
Improved mercurial test coverage
1 parent acd94d1 commit e35eee1

File tree

2 files changed

+91
-60
lines changed

2 files changed

+91
-60
lines changed

bumpversion/scm/hg.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def add_path(self, path: Pathlike) -> None:
8080
temp_path = os.path.relpath(path, cwd)
8181
try:
8282
run_command(["hg", "add", str(temp_path)])
83-
except subprocess.CalledProcessError as e:
83+
except subprocess.CalledProcessError as e: # pragma: no-cover
8484
format_and_raise_error(e)
8585

8686
def commit_and_tag(self, files: List[Pathlike], context: MutableMapping, dry_run: bool = False) -> None:

tests/test_scm/test_hg.py

+90-59
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import shutil
55
import subprocess
66
from pathlib import Path
7+
from unittest.mock import patch
78

89
import pytest
910
from pytest import param
@@ -99,6 +100,95 @@ def test_returns_correct_commit_and_tag_info(self, hg_repo: Path, hg_instance: M
99100
assert tag_info.repository_root == hg_repo
100101
assert tag_info.dirty is False
101102

103+
class TestAddPath:
104+
"""Tests for the add_path method in the Mercurial class."""
105+
106+
def test_adds_to_repo_if_file_is_within_repo(self, hg_repo: Path, hg_instance: Mercurial):
107+
"""The file is added to the repo when it is within the repo root."""
108+
# Arrange
109+
new_file = hg_repo / "newfile.txt"
110+
new_file.write_text("new file")
111+
112+
# Act
113+
with inside_dir(hg_repo):
114+
hg_instance.add_path(new_file)
115+
result = run_command(["hg", "status"])
116+
117+
# Assert
118+
assert result.stdout == "A newfile.txt\n"
119+
120+
@patch("bumpversion.scm.hg.run_command")
121+
def test_does_not_add_path_when_not_in_repository(
122+
self, mock_run_command, hg_repo: Path, hg_instance: Mercurial, fixtures_path: Path
123+
):
124+
"""The file is not added to the repo when it is outside the repo root."""
125+
# Arrange
126+
new_file = fixtures_path / "basic_cfg.toml"
127+
128+
# Act
129+
with inside_dir(hg_repo):
130+
hg_instance.add_path(new_file)
131+
132+
mock_run_command.called_once_with(["hg", "root"])
133+
134+
class TestGetAllTags:
135+
"""Tests for the get_all_tags method."""
136+
137+
def test_returns_correct_tags(self, hg_repo: Path, hg_instance: Mercurial):
138+
"""Returns all the tags in the repo."""
139+
# Arrange
140+
readme = hg_repo.joinpath("readme.md")
141+
readme.touch()
142+
file1 = hg_repo.joinpath("file1.txt")
143+
file1.touch()
144+
file2 = hg_repo.joinpath("file2.txt")
145+
file2.touch()
146+
with inside_dir(hg_repo):
147+
commit_readme("first")
148+
tag("tag1")
149+
hg_instance.add_path(file1)
150+
run_command(["hg", "commit", "-m", "add file1"])
151+
tag("tag2")
152+
hg_instance.add_path(file2)
153+
run_command(["hg", "commit", "-m", "add file2"])
154+
tag("tag3")
155+
156+
# Act
157+
tags = hg_instance.get_all_tags()
158+
159+
# Assert
160+
assert set(tags) == {"tip", "tag1", "tag2", "tag3"}
161+
162+
def test_handles_empty_tags(self, hg_repo: Path, hg_instance: Mercurial):
163+
"""When there are no tags, an empty list is returned."""
164+
# Arrange
165+
with inside_dir(hg_repo):
166+
hg_repo.joinpath("readme.md").touch()
167+
commit_readme("first")
168+
169+
# Act
170+
tags = hg_instance.get_all_tags()
171+
172+
# Assert
173+
assert set(tags) == {"tip"}
174+
175+
@pytest.mark.parametrize(
176+
["side_effect"],
177+
[
178+
param(subprocess.CalledProcessError(1, "cmd"), id="command failure"),
179+
param(FileNotFoundError, id="file not found"),
180+
param(PermissionError, id="permission error"),
181+
],
182+
)
183+
def test_failure_raises_error(self, hg_instance, side_effect, mocker):
184+
"""On failure, it raises a BumpVersionError exception."""
185+
# Arrange
186+
mocker.patch("bumpversion.utils.run_command", side_effect=side_effect)
187+
188+
# Act, Assert
189+
with pytest.raises(BumpVersionError):
190+
hg_instance.get_all_tags()
191+
102192

103193
@pytest.mark.skipif(not shutil.which("hg"), reason="Mercurial is not available.")
104194
def test_hg_bump_version_commits_changes(hg_repo: Path, fixtures_path: Path, runner) -> None:
@@ -120,65 +210,6 @@ def test_hg_bump_version_commits_changes(hg_repo: Path, fixtures_path: Path, run
120210
assert "v1.1.0" in tags
121211

122212

123-
class TestGetAllTags:
124-
"""Tests for the get_all_tags method."""
125-
126-
def test_returns_correct_tags(self, hg_repo: Path, hg_instance: Mercurial):
127-
"""Returns all the tags in the repo."""
128-
# Arrange
129-
readme = hg_repo.joinpath("readme.md")
130-
readme.touch()
131-
file1 = hg_repo.joinpath("file1.txt")
132-
file1.touch()
133-
file2 = hg_repo.joinpath("file2.txt")
134-
file2.touch()
135-
with inside_dir(hg_repo):
136-
commit_readme("first")
137-
tag("tag1")
138-
hg_instance.add_path(file1)
139-
run_command(["hg", "commit", "-m", "add file1"])
140-
tag("tag2")
141-
hg_instance.add_path(file2)
142-
run_command(["hg", "commit", "-m", "add file2"])
143-
tag("tag3")
144-
145-
# Act
146-
tags = hg_instance.get_all_tags()
147-
148-
# Assert
149-
assert set(tags) == {"tip", "tag1", "tag2", "tag3"}
150-
151-
def test_handles_empty_tags(self, hg_repo: Path, hg_instance: Mercurial):
152-
"""When there are no tags, an empty list is returned."""
153-
# Arrange
154-
with inside_dir(hg_repo):
155-
hg_repo.joinpath("readme.md").touch()
156-
commit_readme("first")
157-
158-
# Act
159-
tags = hg_instance.get_all_tags()
160-
161-
# Assert
162-
assert set(tags) == {"tip"}
163-
164-
@pytest.mark.parametrize(
165-
["side_effect"],
166-
[
167-
param(subprocess.CalledProcessError(1, "cmd"), id="command failure"),
168-
param(FileNotFoundError, id="file not found"),
169-
param(PermissionError, id="permission error"),
170-
],
171-
)
172-
def test_failure_raises_error(self, hg_instance, side_effect, mocker):
173-
"""On failure, it raises a BumpVersionError exception."""
174-
# Arrange
175-
mocker.patch("bumpversion.utils.run_command", side_effect=side_effect)
176-
177-
# Act, Assert
178-
with pytest.raises(BumpVersionError):
179-
hg_instance.get_all_tags()
180-
181-
182213
class TestAssertNonDirty:
183214
"""Tests for the assert_nondirty function."""
184215

0 commit comments

Comments
 (0)