diff --git a/commitizen/cli.py b/commitizen/cli.py index e5538aeb4..364bc93f8 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -561,7 +561,7 @@ def __call__( def commitizen_excepthook( - type: type[BaseException], + exctype: type[BaseException], value: BaseException, traceback: TracebackType | None, debug: bool = False, @@ -569,13 +569,13 @@ def commitizen_excepthook( ) -> 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) @@ -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 @@ -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) diff --git a/tests/commands/test_common_command.py b/tests/commands/test_common_command.py index 7a5a26b6b..c70bc96e9 100644 --- a/tests/commands/test_common_command.py +++ b/tests/commands/test_common_command.py @@ -20,11 +20,10 @@ "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, ): @@ -32,13 +31,6 @@ def test_command_shows_description_when_use_help_option( 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") diff --git a/tests/conftest.py b/tests/conftest.py index d7fb6a23d..9b4c033e5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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") diff --git a/tests/test_cli.py b/tests/test_cli.py index 9a362e81e..348fc74c6 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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") + 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): diff --git a/tests/test_cli/test_invalid_command_py_3_10___invalid_arg_.txt b/tests/test_cli/test_invalid_command_py_3_10___invalid_arg_.txt new file mode 100644 index 000000000..148b4eacd --- /dev/null +++ b/tests/test_cli/test_invalid_command_py_3_10___invalid_arg_.txt @@ -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} diff --git a/tests/test_cli/test_invalid_command_py_3_10_invalidCommand_.txt b/tests/test_cli/test_invalid_command_py_3_10_invalidCommand_.txt new file mode 100644 index 000000000..e2d4416b8 --- /dev/null +++ b/tests/test_cli/test_invalid_command_py_3_10_invalidCommand_.txt @@ -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') diff --git a/tests/test_cli/test_invalid_command_py_3_11___invalid_arg_.txt b/tests/test_cli/test_invalid_command_py_3_11___invalid_arg_.txt new file mode 100644 index 000000000..148b4eacd --- /dev/null +++ b/tests/test_cli/test_invalid_command_py_3_11___invalid_arg_.txt @@ -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} diff --git a/tests/test_cli/test_invalid_command_py_3_11_invalidCommand_.txt b/tests/test_cli/test_invalid_command_py_3_11_invalidCommand_.txt new file mode 100644 index 000000000..e2d4416b8 --- /dev/null +++ b/tests/test_cli/test_invalid_command_py_3_11_invalidCommand_.txt @@ -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') diff --git a/tests/test_cli/test_invalid_command_py_3_12___invalid_arg_.txt b/tests/test_cli/test_invalid_command_py_3_12___invalid_arg_.txt new file mode 100644 index 000000000..148b4eacd --- /dev/null +++ b/tests/test_cli/test_invalid_command_py_3_12___invalid_arg_.txt @@ -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} diff --git a/tests/test_cli/test_invalid_command_py_3_12_invalidCommand_.txt b/tests/test_cli/test_invalid_command_py_3_12_invalidCommand_.txt new file mode 100644 index 000000000..c92220c4d --- /dev/null +++ b/tests/test_cli/test_invalid_command_py_3_12_invalidCommand_.txt @@ -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) diff --git a/tests/test_cli/test_invalid_command_py_3_13___invalid_arg_.txt b/tests/test_cli/test_invalid_command_py_3_13___invalid_arg_.txt new file mode 100644 index 000000000..4f0ba2b14 --- /dev/null +++ b/tests/test_cli/test_invalid_command_py_3_13___invalid_arg_.txt @@ -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} diff --git a/tests/test_cli/test_invalid_command_py_3_13_invalidCommand_.txt b/tests/test_cli/test_invalid_command_py_3_13_invalidCommand_.txt new file mode 100644 index 000000000..749066c55 --- /dev/null +++ b/tests/test_cli/test_invalid_command_py_3_13_invalidCommand_.txt @@ -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) diff --git a/tests/test_cli/test_invalid_command_py_3_14___invalid_arg_.txt b/tests/test_cli/test_invalid_command_py_3_14___invalid_arg_.txt new file mode 100644 index 000000000..4f0ba2b14 --- /dev/null +++ b/tests/test_cli/test_invalid_command_py_3_14___invalid_arg_.txt @@ -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} diff --git a/tests/test_cli/test_invalid_command_py_3_14_invalidCommand_.txt b/tests/test_cli/test_invalid_command_py_3_14_invalidCommand_.txt new file mode 100644 index 000000000..749066c55 --- /dev/null +++ b/tests/test_cli/test_invalid_command_py_3_14_invalidCommand_.txt @@ -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) diff --git a/tests/test_cli/test_no_argv_py_3_10_.txt b/tests/test_cli/test_no_argv_py_3_10_.txt new file mode 100644 index 000000000..691c75a86 --- /dev/null +++ b/tests/test_cli/test_no_argv_py_3_10_.txt @@ -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) diff --git a/tests/test_cli/test_no_argv_py_3_11_.txt b/tests/test_cli/test_no_argv_py_3_11_.txt new file mode 100644 index 000000000..691c75a86 --- /dev/null +++ b/tests/test_cli/test_no_argv_py_3_11_.txt @@ -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) diff --git a/tests/test_cli/test_no_argv_py_3_12_.txt b/tests/test_cli/test_no_argv_py_3_12_.txt new file mode 100644 index 000000000..691c75a86 --- /dev/null +++ b/tests/test_cli/test_no_argv_py_3_12_.txt @@ -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) diff --git a/tests/test_cli/test_no_argv_py_3_13_.txt b/tests/test_cli/test_no_argv_py_3_13_.txt new file mode 100644 index 000000000..f880f9977 --- /dev/null +++ b/tests/test_cli/test_no_argv_py_3_13_.txt @@ -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) diff --git a/tests/test_cli/test_no_argv_py_3_14_.txt b/tests/test_cli/test_no_argv_py_3_14_.txt new file mode 100644 index 000000000..f880f9977 --- /dev/null +++ b/tests/test_cli/test_no_argv_py_3_14_.txt @@ -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)