Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9cc174e

Browse files
committed
feat: add --no-raise to avoid raising error codes
Closes #485
1 parent 6efad39 commit 9cc174e

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

commitizen/cli.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import sys
44
from functools import partial
5+
from typing import List
56

67
import argcomplete
78
from decli import cli
@@ -24,6 +25,13 @@
2425
"name": ["-n", "--name"],
2526
"help": "use the given commitizen (default: cz_conventional_commits)",
2627
},
28+
{
29+
"name": ["-nr", "--no-raise"],
30+
"action": "append",
31+
"type": int,
32+
"required": False,
33+
"help": "error codes that won't rise error, provide -nr multiple times for many",
34+
},
2735
],
2836
"subcommands": {
2937
"title": "commands",
@@ -268,13 +276,20 @@
268276
original_excepthook = sys.excepthook
269277

270278

271-
def commitizen_excepthook(type, value, tracekback, debug=False):
279+
def commitizen_excepthook(
280+
type, value, tracekback, debug=False, no_raise: List[int] = None
281+
):
282+
if no_raise is None:
283+
no_raise = []
272284
if isinstance(value, CommitizenException):
273285
if value.message:
274286
value.output_method(value.message)
275287
if debug:
276288
original_excepthook(type, value, tracekback)
277-
sys.exit(value.exit_code)
289+
exit_code = value.exit_code
290+
if exit_code in no_raise:
291+
exit_code = 0
292+
sys.exit(exit_code)
278293
else:
279294
original_excepthook(type, value, tracekback)
280295

@@ -313,6 +328,11 @@ def main():
313328
if args.debug:
314329
logging.getLogger("commitizen").setLevel(logging.DEBUG)
315330
sys.excepthook = commitizen_debug_excepthook
331+
elif args.no_raise:
332+
no_raise_debug_excepthook = partial(
333+
commitizen_excepthook, no_raise=args.no_raise
334+
)
335+
sys.excepthook = no_raise_debug_excepthook
316336

317337
args.func(conf, vars(args))()
318338

tests/test_cli.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
import pytest
55

66
from commitizen import cli
7-
from commitizen.exceptions import ExpectedExit, NoCommandFoundError, NotAGitProjectError
7+
from commitizen.exceptions import (
8+
ExpectedExit,
9+
NoCommandFoundError,
10+
NotAGitProjectError,
11+
)
812

913

1014
def test_sysexit_no_argv(mocker, capsys):
@@ -95,3 +99,16 @@ def test_argcomplete_activation():
9599
output = subprocess.run(["register-python-argcomplete", "cz"])
96100

97101
assert output.returncode == 0
102+
103+
104+
def test_commitizen_excepthook_no_raises(capsys):
105+
with pytest.raises(SystemExit) as excinfo:
106+
cli.commitizen_excepthook(
107+
NotAGitProjectError,
108+
NotAGitProjectError(),
109+
"",
110+
no_raise=[NotAGitProjectError.exit_code],
111+
)
112+
113+
assert excinfo.type == SystemExit
114+
assert excinfo.value.code == 0

0 commit comments

Comments
 (0)