Skip to content

Commit 90b8bc6

Browse files
committed
test: replace try with pytest.raises
1 parent 701de0a commit 90b8bc6

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

tests/commands/test_bump_command.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -646,13 +646,11 @@ def test_none_increment_should_not_call_git_tag_and_error_code_is_not_zero(
646646
dummy_value = git.tag("0.0.2")
647647
git.tag = MagicMock(return_value=dummy_value)
648648

649-
with pytest.raises(NoneIncrementExit):
650-
try:
651-
cli.main()
652-
except NoneIncrementExit as e:
653-
git.tag.assert_not_called()
654-
assert e.exit_code == ExitCode.NO_INCREMENT
655-
raise e
649+
with pytest.raises(NoneIncrementExit) as e:
650+
cli.main()
651+
652+
git.tag.assert_not_called()
653+
assert e.value.exit_code == ExitCode.NO_INCREMENT
656654

657655
# restore pop stashed
658656
git.tag = stashed_git_tag

tests/commands/test_changelog_command.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,11 @@ def test_changelog_hook(mocker: MockFixture, config: BaseConfig, dry_run: bool):
292292
config, {"unreleased_version": None, "incremental": True, "dry_run": dry_run}
293293
)
294294
mocker.patch.object(changelog.cz, "changelog_hook", changelog_hook_mock)
295-
try:
295+
if dry_run:
296+
with pytest.raises(DryRunExit):
297+
changelog()
298+
else:
296299
changelog()
297-
except DryRunExit:
298-
pass
299300

300301
full_changelog = (
301302
"## Unreleased\n\n### Refactor\n\n- is in changelog\n\n### Feat\n\n- new file\n"

tests/test_git.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import pytest
99
from pytest_mock import MockFixture
1010

11-
from commitizen import cmd, exceptions, git
11+
from commitizen import cmd, git
12+
from commitizen.exceptions import GitCommandError
1213
from tests.utils import (
1314
FakeCommand,
1415
create_branch,
@@ -110,10 +111,13 @@ def test_git_message_with_empty_body():
110111

111112
@pytest.mark.usefixtures("tmp_commitizen_project")
112113
def test_get_log_as_str_list_empty():
113-
"""ensure an exception or empty list in an empty project"""
114+
"""
115+
Ensure an exception is raised or empty list in an empty project.
116+
The behavior is different depending on the version of git.
117+
"""
114118
try:
115119
gitlog = git._get_log_as_str_list(start=None, end="HEAD", args="")
116-
except exceptions.GitCommandError:
120+
except GitCommandError:
117121
return
118122
assert len(gitlog) == 0, "list should be empty if no assert"
119123

@@ -409,7 +413,7 @@ def test_get_filenames_in_commit_error(mocker: MockFixture):
409413
"commitizen.cmd.run",
410414
return_value=FakeCommand(out="", err="fatal: bad object HEAD", return_code=1),
411415
)
412-
with pytest.raises(exceptions.GitCommandError) as excinfo:
416+
with pytest.raises(GitCommandError) as excinfo:
413417
git.get_filenames_in_commit()
414418
assert str(excinfo.value) == "fatal: bad object HEAD"
415419

@@ -497,5 +501,5 @@ def test_get_default_branch_error(mocker: MockFixture):
497501
return_code=1,
498502
),
499503
)
500-
with pytest.raises(exceptions.GitCommandError):
504+
with pytest.raises(GitCommandError):
501505
git.get_default_branch()

0 commit comments

Comments
 (0)