Skip to content

Commit 5d6f91a

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

File tree

9 files changed

+195
-21
lines changed

9 files changed

+195
-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: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@
22
from pytest_mock import MockFixture
33

44
from commitizen.commands import Example, Info, ListCz, Schema
5+
from commitizen.exceptions import ExpectedExit
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+
818
@pytest.mark.parametrize(
919
"command",
1020
[
@@ -20,25 +30,17 @@
2030
"version",
2131
],
2232
)
23-
@pytest.mark.usefixtures("python_version")
33+
@pytest.mark.usefixtures("python_version", "consistent_terminal_output")
2434
def test_command_shows_description_when_use_help_option(
2535
capsys,
2636
file_regression,
27-
monkeypatch: pytest.MonkeyPatch,
2837
command: str,
2938
util: UtilFixture,
3039
):
3140
"""Test that the command shows the description when the help option is used.
3241
3342
Note: If the command description changes, please run `poe test:regen` to regenerate the test files.
3443
"""
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")
4244

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

tests/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,14 @@ def any_changelog_format(config: BaseConfig) -> ChangelogFormat:
302302
def python_version(request: pytest.FixtureRequest) -> str:
303303
"""The current python version in '{major}.{minor}' format"""
304304
return cast("str", request.param)
305+
306+
307+
@pytest.fixture
308+
def consistent_terminal_output(monkeypatch: pytest.MonkeyPatch):
309+
"""Force consistent terminal output."""
310+
monkeypatch.setenv("COLUMNS", "80")
311+
monkeypatch.setenv("TERM", "dumb")
312+
monkeypatch.setenv("LC_ALL", "C")
313+
monkeypatch.setenv("LANG", "C")
314+
monkeypatch.setenv("NO_COLOR", "1")
315+
monkeypatch.setenv("PAGER", "cat")

tests/test_cli.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,13 @@
1010
from commitizen import cli
1111
from commitizen.exceptions import (
1212
ConfigFileNotFound,
13-
ExpectedExit,
1413
InvalidCommandArgumentError,
1514
NoCommandFoundError,
1615
NotAGitProjectError,
1716
)
1817
from tests.utils import UtilFixture
1918

2019

21-
def test_sysexit_no_argv(util: UtilFixture, capsys):
22-
with pytest.raises(ExpectedExit):
23-
util.run_cli()
24-
out, _ = capsys.readouterr()
25-
assert out.startswith("usage")
26-
27-
2820
def test_cz_config_file_without_correct_file_path(util: UtilFixture, capsys):
2921
with pytest.raises(ConfigFileNotFound) as excinfo:
3022
util.run_cli("--config", "./config/pyproject.toml", "example")

0 commit comments

Comments
 (0)