Skip to content

Commit 70edfe0

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

File tree

4 files changed

+61
-20
lines changed

4 files changed

+61
-20
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: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,31 @@
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.fixture
10+
def patch_consistent_terminal_output(monkeypatch: pytest.MonkeyPatch):
11+
"""Force consistent terminal output."""
12+
monkeypatch.setenv("COLUMNS", "80")
13+
monkeypatch.setenv("TERM", "dumb")
14+
monkeypatch.setenv("LC_ALL", "C")
15+
monkeypatch.setenv("LANG", "C")
16+
monkeypatch.setenv("NO_COLOR", "1")
17+
monkeypatch.setenv("PAGER", "cat")
18+
19+
20+
def test_no_argv(
21+
util: UtilFixture, capsys, patch_consistent_terminal_output, file_regression
22+
):
23+
with pytest.raises(ExpectedExit):
24+
util.run_cli()
25+
out, err = capsys.readouterr()
26+
assert out == ""
27+
file_regression.check(err, extension=".txt")
28+
29+
830
@pytest.mark.parametrize(
931
"command",
1032
[
@@ -24,21 +46,14 @@
2446
def test_command_shows_description_when_use_help_option(
2547
capsys,
2648
file_regression,
27-
monkeypatch: pytest.MonkeyPatch,
49+
patch_consistent_terminal_output,
2850
command: str,
2951
util: UtilFixture,
3052
):
3153
"""Test that the command shows the description when the help option is used.
3254
3355
Note: If the command description changes, please run `poe test:regen` to regenerate the test files.
3456
"""
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")
4257

4358
with pytest.raises(SystemExit):
4459
util.run_cli(command, "--help")
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/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)