Skip to content

Commit 28da20d

Browse files
committed
fix(cli): move sys.excepthook override to correct line, rename 'type' parameter, fix no argv test
1 parent aa82b98 commit 28da20d

23 files changed

+289
-21
lines changed

commitizen/cli.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -561,21 +561,21 @@ def __call__(
561561

562562

563563
def commitizen_excepthook(
564-
type: type[BaseException],
564+
exctype: type[BaseException],
565565
value: BaseException,
566566
traceback: TracebackType | None,
567567
debug: bool = False,
568568
no_raise: list[int] | None = None,
569569
) -> None:
570570
traceback = traceback if isinstance(traceback, TracebackType) else None
571571
if not isinstance(value, CommitizenException):
572-
sys.__excepthook__(type, value, traceback)
572+
sys.__excepthook__(exctype, value, traceback)
573573
return
574574

575575
if value.message:
576576
value.output_method(value.message)
577577
if debug:
578-
sys.__excepthook__(type, value, traceback)
578+
sys.__excepthook__(exctype, value, traceback)
579579
exit_code = value.exit_code
580580
if no_raise is not None and exit_code in no_raise:
581581
sys.exit(ExitCode.EXPECTED_EXIT)
@@ -629,6 +629,8 @@ class Args(argparse.Namespace):
629629

630630

631631
def main() -> None:
632+
sys.excepthook = commitizen_excepthook
633+
632634
parser: argparse.ArgumentParser = cli(data)
633635
argcomplete.autocomplete(parser)
634636
# Show help if no arg provided
@@ -673,7 +675,6 @@ def main() -> None:
673675
elif not conf.path:
674676
conf.update({"name": "cz_conventional_commits"})
675677

676-
sys.excepthook = commitizen_excepthook
677678
if args.debug:
678679
logging.getLogger("commitizen").setLevel(logging.DEBUG)
679680
sys.excepthook = partial(sys.excepthook, debug=True)

tests/commands/test_common_command.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,35 @@
22
from pytest_mock import MockFixture
33

44
from commitizen.commands import Example, Info, ListCz, Schema
5+
from commitizen.exceptions import ExpectedExit, NoCommandFoundError
56
from tests.utils import UtilFixture
67

78

9+
@pytest.mark.usefixtures("python_version", "consistent_terminal_output")
10+
def test_no_argv(util: UtilFixture, capsys, file_regression):
11+
with pytest.raises(ExpectedExit):
12+
util.run_cli()
13+
out, err = capsys.readouterr()
14+
assert out == ""
15+
file_regression.check(err, extension=".txt")
16+
17+
18+
@pytest.mark.parametrize(
19+
"arg",
20+
[
21+
"--invalid-arg",
22+
"invalidCommand",
23+
],
24+
)
25+
@pytest.mark.usefixtures("python_version", "consistent_terminal_output")
26+
def test_invalid_command(util: UtilFixture, capsys, file_regression, arg):
27+
with pytest.raises(NoCommandFoundError):
28+
util.run_cli(arg)
29+
out, err = capsys.readouterr()
30+
assert out == ""
31+
file_regression.check(err, extension=".txt")
32+
33+
834
@pytest.mark.parametrize(
935
"command",
1036
[
@@ -20,25 +46,17 @@
2046
"version",
2147
],
2248
)
23-
@pytest.mark.usefixtures("python_version")
49+
@pytest.mark.usefixtures("python_version", "consistent_terminal_output")
2450
def test_command_shows_description_when_use_help_option(
2551
capsys,
2652
file_regression,
27-
monkeypatch: pytest.MonkeyPatch,
2853
command: str,
2954
util: UtilFixture,
3055
):
3156
"""Test that the command shows the description when the help option is used.
3257
3358
Note: If the command description changes, please run `poe test:regen` to regenerate the test files.
3459
"""
35-
# Force consistent terminal output
36-
monkeypatch.setenv("COLUMNS", "80")
37-
monkeypatch.setenv("TERM", "dumb")
38-
monkeypatch.setenv("LC_ALL", "C")
39-
monkeypatch.setenv("LANG", "C")
40-
monkeypatch.setenv("NO_COLOR", "1")
41-
monkeypatch.setenv("PAGER", "cat")
4260

4361
with pytest.raises(SystemExit):
4462
util.run_cli(command, "--help")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
2+
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...
3+
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
2+
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...
3+
cz: error: argument {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}: invalid choice: 'invalidCommand' (choose from init, commit, c, ls, example, info, schema, bump, changelog, ch, check, version)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
2+
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...
3+
4+
Commitizen is a powerful release management tool that helps teams maintain consistent and meaningful commit messages while automating version management.
5+
For more information, please visit https://commitizen-tools.github.io/commitizen
6+
7+
options:
8+
-h, --help show this help message and exit
9+
--config CONFIG the path of configuration file
10+
--debug use debug mode
11+
-n, --name NAME use the given commitizen (default:
12+
cz_conventional_commits)
13+
-nr, --no-raise NO_RAISE
14+
comma separated error codes that won't raise error,
15+
e.g: cz -nr 1,2,3 bump. See codes at
16+
https://commitizen-
17+
tools.github.io/commitizen/exit_codes/
18+
19+
commands:
20+
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
21+
init init commitizen configuration
22+
commit (c) create new commit
23+
ls show available commitizens
24+
example show commit example
25+
info show information about the cz
26+
schema show commit schema
27+
bump bump semantic version based on the git log
28+
changelog (ch) generate changelog (note that it will overwrite
29+
existing file)
30+
check validates that a commit message matches the commitizen
31+
schema
32+
version get the version of the installed commitizen or the
33+
current project (default: installed commitizen)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
2+
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
3+
...
4+
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
2+
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
3+
...
4+
cz: error: argument {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}: invalid choice: 'invalidCommand' (choose from 'init', 'commit', 'c', 'ls', 'example', 'info', 'schema', 'bump', 'changelog', 'ch', 'check', 'version')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
2+
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
3+
...
4+
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
2+
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
3+
...
4+
cz: error: argument {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}: invalid choice: 'invalidCommand' (choose from 'init', 'commit', 'c', 'ls', 'example', 'info', 'schema', 'bump', 'changelog', 'ch', 'check', 'version')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
2+
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
3+
...
4+
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}

0 commit comments

Comments
 (0)