Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,21 +561,21 @@ def __call__(


def commitizen_excepthook(
type: type[BaseException],
exctype: type[BaseException],
value: BaseException,
traceback: TracebackType | None,
debug: bool = False,
no_raise: list[int] | None = None,
) -> None:
traceback = traceback if isinstance(traceback, TracebackType) else None
if not isinstance(value, CommitizenException):
sys.__excepthook__(type, value, traceback)
sys.__excepthook__(exctype, value, traceback)
return

if value.message:
value.output_method(value.message)
if debug:
sys.__excepthook__(type, value, traceback)
sys.__excepthook__(exctype, value, traceback)
exit_code = value.exit_code
if no_raise is not None and exit_code in no_raise:
sys.exit(ExitCode.EXPECTED_EXIT)
Expand Down Expand Up @@ -629,6 +629,8 @@ class Args(argparse.Namespace):


def main() -> None:
sys.excepthook = commitizen_excepthook

parser: argparse.ArgumentParser = cli(data)
argcomplete.autocomplete(parser)
# Show help if no arg provided
Expand Down Expand Up @@ -673,7 +675,6 @@ def main() -> None:
elif not conf.path:
conf.update({"name": "cz_conventional_commits"})

sys.excepthook = commitizen_excepthook
if args.debug:
logging.getLogger("commitizen").setLevel(logging.DEBUG)
sys.excepthook = partial(sys.excepthook, debug=True)
Expand Down
10 changes: 1 addition & 9 deletions tests/commands/test_common_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,17 @@
"version",
],
)
@pytest.mark.usefixtures("python_version")
@pytest.mark.usefixtures("python_version", "consistent_terminal_output")
def test_command_shows_description_when_use_help_option(
capsys,
file_regression,
monkeypatch: pytest.MonkeyPatch,
command: str,
util: UtilFixture,
):
"""Test that the command shows the description when the help option is used.

Note: If the command description changes, please run `poe test:regen` to regenerate the test files.
"""
# Force consistent terminal output
monkeypatch.setenv("COLUMNS", "80")
monkeypatch.setenv("TERM", "dumb")
monkeypatch.setenv("LC_ALL", "C")
monkeypatch.setenv("LANG", "C")
monkeypatch.setenv("NO_COLOR", "1")
monkeypatch.setenv("PAGER", "cat")

with pytest.raises(SystemExit):
util.run_cli(command, "--help")
Expand Down
11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,14 @@ def any_changelog_format(config: BaseConfig) -> ChangelogFormat:
def python_version(request: pytest.FixtureRequest) -> str:
"""The current python version in '{major}.{minor}' format"""
return cast("str", request.param)


@pytest.fixture
def consistent_terminal_output(monkeypatch: pytest.MonkeyPatch):
"""Force consistent terminal output."""
monkeypatch.setenv("COLUMNS", "80")
monkeypatch.setenv("TERM", "dumb")
monkeypatch.setenv("LC_ALL", "C")
monkeypatch.setenv("LANG", "C")
monkeypatch.setenv("NO_COLOR", "1")
monkeypatch.setenv("PAGER", "cat")
24 changes: 21 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,29 @@
from tests.utils import UtilFixture


def test_sysexit_no_argv(util: UtilFixture, capsys):
@pytest.mark.usefixtures("python_version", "consistent_terminal_output")
def test_no_argv(util: UtilFixture, capsys, file_regression):
with pytest.raises(ExpectedExit):
util.run_cli()
out, _ = capsys.readouterr()
assert out.startswith("usage")
Comment on lines -24 to -25
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are unreachable...

out, err = capsys.readouterr()
assert out == ""
file_regression.check(err, extension=".txt")


@pytest.mark.parametrize(
"arg",
[
"--invalid-arg",
"invalidCommand",
],
)
@pytest.mark.usefixtures("python_version", "consistent_terminal_output")
def test_invalid_command(util: UtilFixture, capsys, file_regression, arg):
with pytest.raises(NoCommandFoundError):
util.run_cli(arg)
out, err = capsys.readouterr()
assert out == ""
file_regression.check(err, extension=".txt")


def test_cz_config_file_without_correct_file_path(util: UtilFixture, capsys):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...
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')
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...
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')
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...
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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...
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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...
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)
34 changes: 34 additions & 0 deletions tests/test_cli/test_no_argv_py_3_10_.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...

Commitizen is a powerful release management tool that helps teams maintain consistent and meaningful commit messages while automating version management.
For more information, please visit https://commitizen-tools.github.io/commitizen

options:
-h, --help show this help message and exit
--config CONFIG the path of configuration file
--debug use debug mode
-n NAME, --name NAME use the given commitizen (default:
cz_conventional_commits)
-nr NO_RAISE, --no-raise NO_RAISE
comma separated error codes that won't raise error,
e.g: cz -nr 1,2,3 bump. See codes at
https://commitizen-
tools.github.io/commitizen/exit_codes/

commands:
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
init init commitizen configuration
commit (c) create new commit
ls show available commitizens
example show commit example
info show information about the cz
schema show commit schema
bump bump semantic version based on the git log
changelog (ch) generate changelog (note that it will overwrite
existing file)
check validates that a commit message matches the commitizen
schema
version get the version of the installed commitizen or the
current project (default: installed commitizen)
34 changes: 34 additions & 0 deletions tests/test_cli/test_no_argv_py_3_11_.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...

Commitizen is a powerful release management tool that helps teams maintain consistent and meaningful commit messages while automating version management.
For more information, please visit https://commitizen-tools.github.io/commitizen

options:
-h, --help show this help message and exit
--config CONFIG the path of configuration file
--debug use debug mode
-n NAME, --name NAME use the given commitizen (default:
cz_conventional_commits)
-nr NO_RAISE, --no-raise NO_RAISE
comma separated error codes that won't raise error,
e.g: cz -nr 1,2,3 bump. See codes at
https://commitizen-
tools.github.io/commitizen/exit_codes/

commands:
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
init init commitizen configuration
commit (c) create new commit
ls show available commitizens
example show commit example
info show information about the cz
schema show commit schema
bump bump semantic version based on the git log
changelog (ch) generate changelog (note that it will overwrite
existing file)
check validates that a commit message matches the commitizen
schema
version get the version of the installed commitizen or the
current project (default: installed commitizen)
34 changes: 34 additions & 0 deletions tests/test_cli/test_no_argv_py_3_12_.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...

Commitizen is a powerful release management tool that helps teams maintain consistent and meaningful commit messages while automating version management.
For more information, please visit https://commitizen-tools.github.io/commitizen

options:
-h, --help show this help message and exit
--config CONFIG the path of configuration file
--debug use debug mode
-n NAME, --name NAME use the given commitizen (default:
cz_conventional_commits)
-nr NO_RAISE, --no-raise NO_RAISE
comma separated error codes that won't raise error,
e.g: cz -nr 1,2,3 bump. See codes at
https://commitizen-
tools.github.io/commitizen/exit_codes/

commands:
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
init init commitizen configuration
commit (c) create new commit
ls show available commitizens
example show commit example
info show information about the cz
schema show commit schema
bump bump semantic version based on the git log
changelog (ch) generate changelog (note that it will overwrite
existing file)
check validates that a commit message matches the commitizen
schema
version get the version of the installed commitizen or the
current project (default: installed commitizen)
33 changes: 33 additions & 0 deletions tests/test_cli/test_no_argv_py_3_13_.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...

Commitizen is a powerful release management tool that helps teams maintain consistent and meaningful commit messages while automating version management.
For more information, please visit https://commitizen-tools.github.io/commitizen

options:
-h, --help show this help message and exit
--config CONFIG the path of configuration file
--debug use debug mode
-n, --name NAME use the given commitizen (default:
cz_conventional_commits)
-nr, --no-raise NO_RAISE
comma separated error codes that won't raise error,
e.g: cz -nr 1,2,3 bump. See codes at
https://commitizen-
tools.github.io/commitizen/exit_codes/

commands:
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
init init commitizen configuration
commit (c) create new commit
ls show available commitizens
example show commit example
info show information about the cz
schema show commit schema
bump bump semantic version based on the git log
changelog (ch) generate changelog (note that it will overwrite
existing file)
check validates that a commit message matches the commitizen
schema
version get the version of the installed commitizen or the
current project (default: installed commitizen)
33 changes: 33 additions & 0 deletions tests/test_cli/test_no_argv_py_3_14_.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...

Commitizen is a powerful release management tool that helps teams maintain consistent and meaningful commit messages while automating version management.
For more information, please visit https://commitizen-tools.github.io/commitizen

options:
-h, --help show this help message and exit
--config CONFIG the path of configuration file
--debug use debug mode
-n, --name NAME use the given commitizen (default:
cz_conventional_commits)
-nr, --no-raise NO_RAISE
comma separated error codes that won't raise error,
e.g: cz -nr 1,2,3 bump. See codes at
https://commitizen-
tools.github.io/commitizen/exit_codes/

commands:
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
init init commitizen configuration
commit (c) create new commit
ls show available commitizens
example show commit example
info show information about the cz
schema show commit schema
bump bump semantic version based on the git log
changelog (ch) generate changelog (note that it will overwrite
existing file)
check validates that a commit message matches the commitizen
schema
version get the version of the installed commitizen or the
current project (default: installed commitizen)