Skip to content

Commit 4a1e975

Browse files
committed
refactor: introduce DryRunExit, ExpectedExit, NoCommandFoundError, InvalidCommandArgumentError
Instead of using SystemExit, we raise these error as CommitizenException
1 parent 8a51843 commit 4a1e975

File tree

9 files changed

+50
-23
lines changed

9 files changed

+50
-23
lines changed

commitizen/commands/bump.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from commitizen.config import BaseConfig
99
from commitizen.exceptions import (
1010
CommitFailedError,
11+
DryRunExit,
12+
ExpectedExit,
1113
NoCommitsFoundError,
1214
NoPatternMapError,
1315
NoVersionSpecifiedError,
@@ -129,7 +131,7 @@ def __call__(self): # noqa: C901
129131

130132
# Do not perform operations over files or git.
131133
if dry_run:
132-
raise SystemExit()
134+
raise DryRunExit()
133135

134136
bump.update_version_in_files(
135137
current_version,
@@ -138,7 +140,7 @@ def __call__(self): # noqa: C901
138140
check_consistency=self.check_consistency,
139141
)
140142
if is_files_only:
141-
raise SystemExit()
143+
raise ExpectedExit()
142144

143145
if self.changelog:
144146
changelog = Changelog(

commitizen/commands/changelog.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from commitizen import changelog, factory, git, out
77
from commitizen.config import BaseConfig
88
from commitizen.exceptions import (
9+
DryRunExit,
910
NoCommitsFoundError,
1011
NoPatternMapError,
1112
NoRevisionError,
@@ -108,7 +109,7 @@ def __call__(self):
108109

109110
if self.dry_run:
110111
out.write(changelog_out)
111-
raise SystemExit(0)
112+
raise DryRunExit()
112113

113114
lines = []
114115
if self.incremental and os.path.isfile(self.file_name):

commitizen/commands/check.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
from commitizen import factory, git, out
77
from commitizen.config import BaseConfig
8-
from commitizen.exceptions import InvalidCommitMessageError, NoCommitsFoundError
8+
from commitizen.exceptions import (
9+
InvalidCommandArgumentError,
10+
InvalidCommitMessageError,
11+
NoCommitsFoundError,
12+
)
913

1014

1115
class Check:
@@ -35,13 +39,13 @@ def _valid_command_argument(self):
3539
"See 'cz check -h' for more information"
3640
)
3741
)
38-
raise SystemExit()
42+
raise InvalidCommandArgumentError()
3943

4044
def __call__(self):
4145
"""Validate if commit messages follows the conventional pattern.
4246
4347
Raises:
44-
SystemExit: if the commit provided not follows the conventional pattern
48+
InvalidCommitMessageError: if the commit provided not follows the conventional pattern
4549
"""
4650
commit_msgs = self._get_commit_messages()
4751
if not commit_msgs:

commitizen/commands/commit.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from commitizen.exceptions import (
1111
CommitError,
1212
CustomError,
13+
DryRunExit,
1314
NoAnswersError,
1415
NoCommitBackupError,
1516
NothingToCommitError,
@@ -69,7 +70,7 @@ def __call__(self):
6970
out.info(f"\n{m}\n")
7071

7172
if dry_run:
72-
raise NothingToCommitError()
73+
raise DryRunExit()
7374

7475
c = git.commit(m)
7576

commitizen/exceptions.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,39 @@
44

55

66
class ExitCode(enum.IntEnum):
7+
EXPECTED_EXIT = 0
78
NO_COMMITIZEN_FOUND = 1
8-
99
NOT_A_GIT_PROJECT = 2
10-
MISSING_CONFIG = 15
11-
1210
NO_COMMITS_FOUND = 3
1311
NO_VERSION_SPECIFIED = 4
1412
NO_PATTERN_MAP = 5
1513
COMMIT_FAILED = 6
1614
TAG_FAILED = 7
17-
CURRENT_VERSION_NOT_FOUND = 17
18-
1915
NO_ANSWERS = 8
2016
COMMIT_ERROR = 9
2117
NO_COMMIT_BACKUP = 10
2218
NOTHING_TO_COMMIT = 11
2319
CUSTOM_ERROR = 12
24-
20+
NO_COMMAND_FOUND = 13
2521
INVALID_COMMIT_MSG = 14
26-
27-
# Changelog
22+
MISSING_CONFIG = 15
2823
NO_REVISION = 16
24+
CURRENT_VERSION_NOT_FOUND = 17
25+
INVALID_COMMAND_ARGUMENT = 18
2926

3027

3128
class CommitizenException(Exception):
3229
pass
3330

3431

32+
class ExpectedExit(CommitizenException):
33+
exit_code = ExitCode.EXPECTED_EXIT
34+
35+
36+
class DryRunExit(ExpectedExit):
37+
pass
38+
39+
3540
class NoCommitizenFoundException(CommitizenException):
3641
exit_code = ExitCode.NO_COMMITIZEN_FOUND
3742

@@ -102,3 +107,11 @@ class InvalidCommitMessageError(CommitizenException):
102107

103108
class NoRevisionError(CommitizenException):
104109
exit_code = ExitCode.NO_REVISION
110+
111+
112+
class NoCommandFoundError(CommitizenException):
113+
exit_code = ExitCode.NO_COMMAND_FOUND
114+
115+
116+
class InvalidCommandArgumentError(CommitizenException):
117+
exit_code = ExitCode.INVALID_COMMAND_ARGUMENT

tests/commands/test_changelog_command.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from commitizen import cli, git
88
from commitizen.commands.changelog import Changelog
9-
from commitizen.exceptions import NoCommitsFoundError
9+
from commitizen.exceptions import DryRunExit, NoCommitsFoundError
1010
from tests.utils import create_file_and_commit
1111

1212

@@ -40,7 +40,7 @@ def test_changlog_from_version_zero_point_two(mocker, capsys):
4040

4141
testargs = ["cz", "changelog", "--start-rev", "0.2.0", "--dry-run"]
4242
mocker.patch.object(sys, "argv", testargs)
43-
with pytest.raises(SystemExit):
43+
with pytest.raises(DryRunExit):
4444
cli.main()
4545

4646
out, _ = capsys.readouterr()
@@ -55,7 +55,7 @@ def test_changlog_with_different_cz(mocker, capsys):
5555
testargs = ["cz", "-n", "cz_jira", "changelog", "--dry-run"]
5656
mocker.patch.object(sys, "argv", testargs)
5757

58-
with pytest.raises(SystemExit):
58+
with pytest.raises(DryRunExit):
5959
cli.main()
6060
out, _ = capsys.readouterr()
6161
assert (

tests/commands/test_check_command.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
import pytest
55

66
from commitizen import cli, commands, git
7-
from commitizen.exceptions import InvalidCommitMessageError, NoCommitsFoundError
7+
from commitizen.exceptions import (
8+
InvalidCommandArgumentError,
9+
InvalidCommitMessageError,
10+
NoCommitsFoundError,
11+
)
812

913
COMMIT_LOG = [
1014
"refactor: A code change that neither fixes a bug nor adds a feature",
@@ -191,7 +195,7 @@ def test_check_a_range_of_git_commits_and_failed(config, mocker):
191195
"args", [{"rev_range": "HEAD~10..master", "commit_msg_file": "some_file"}, {}]
192196
)
193197
def test_check_command_with_invalid_argment(args, config, capsys):
194-
with pytest.raises(SystemExit):
198+
with pytest.raises(InvalidCommandArgumentError):
195199
commands.Check(config=config, arguments=args)
196200
_, err = capsys.readouterr()
197201
assert "One and only one argument is required for check command!" in err

tests/commands/test_commit_command.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from commitizen.exceptions import (
88
CommitError,
99
CustomError,
10+
DryRunExit,
1011
NoCommitBackupError,
1112
NothingToCommitError,
1213
)
@@ -97,7 +98,7 @@ def test_commit_command_with_dry_run_option(config, mocker):
9798
"footer": "",
9899
}
99100

100-
with pytest.raises(NothingToCommitError):
101+
with pytest.raises(DryRunExit):
101102
commit_cmd = commands.Commit(config, {"dry_run": True})
102103
commit_cmd()
103104

tests/test_cli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
from commitizen import cli
66
from commitizen.__version__ import __version__
7+
from commitizen.exceptions import ExpectedExit, NoCommandFoundError
78

89

910
def test_sysexit_no_argv(mocker, capsys):
1011
testargs = ["cz"]
1112
mocker.patch.object(sys, "argv", testargs)
1213

13-
with pytest.raises(SystemExit):
14+
with pytest.raises(ExpectedExit):
1415
cli.main()
1516
out, _ = capsys.readouterr()
1617
assert out.startswith("usage")
@@ -20,7 +21,7 @@ def test_cz_with_arg_but_without_command(mocker, capsys):
2021
testargs = ["cz", "--name", "cz_jira"]
2122
mocker.patch.object(sys, "argv", testargs)
2223

23-
with pytest.raises(SystemExit):
24+
with pytest.raises(NoCommandFoundError):
2425
cli.main()
2526
_, err = capsys.readouterr()
2627
assert "Command is required" in err

0 commit comments

Comments
 (0)