Skip to content

Commit 810d10d

Browse files
committed
test: replace try with pytest.raises
1 parent 1d0f6cb commit 810d10d

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

tests/commands/test_bump_command.py

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

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

658656
# restore pop stashed
659657
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: 6 additions & 8 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,
@@ -111,11 +112,8 @@ def test_git_message_with_empty_body():
111112
@pytest.mark.usefixtures("tmp_commitizen_project")
112113
def test_get_log_as_str_list_empty():
113114
"""ensure an exception or empty list in an empty project"""
114-
try:
115-
gitlog = git._get_log_as_str_list(start=None, end="HEAD", args="")
116-
except exceptions.GitCommandError:
117-
return
118-
assert len(gitlog) == 0, "list should be empty if no assert"
115+
with pytest.raises(GitCommandError):
116+
git._get_log_as_str_list(start=None, end="HEAD", args="")
119117

120118

121119
@pytest.mark.usefixtures("tmp_commitizen_project")
@@ -409,7 +407,7 @@ def test_get_filenames_in_commit_error(mocker: MockFixture):
409407
"commitizen.cmd.run",
410408
return_value=FakeCommand(out="", err="fatal: bad object HEAD", return_code=1),
411409
)
412-
with pytest.raises(exceptions.GitCommandError) as excinfo:
410+
with pytest.raises(GitCommandError) as excinfo:
413411
git.get_filenames_in_commit()
414412
assert str(excinfo.value) == "fatal: bad object HEAD"
415413

@@ -497,5 +495,5 @@ def test_get_default_branch_error(mocker: MockFixture):
497495
return_code=1,
498496
),
499497
)
500-
with pytest.raises(exceptions.GitCommandError):
498+
with pytest.raises(GitCommandError):
501499
git.get_default_branch()

0 commit comments

Comments
 (0)