diff --git a/commitizen/cli.py b/commitizen/cli.py index 85674e8c4..99bf1d735 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -2,6 +2,7 @@ import argparse import logging +import platform import sys from copy import deepcopy from functools import partial @@ -13,6 +14,7 @@ from decli import cli from commitizen import commands, config, out, version_schemes +from commitizen.__version__ import __version__ from commitizen.defaults import DEFAULT_SETTINGS from commitizen.exceptions import ( CommitizenException, @@ -104,10 +106,20 @@ def __call__( "required": False, "help": "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/", }, + { + "name": ["-v", "--version"], + "action": "store_true", + "help": "Show the version of the installed commitizen", + }, + { + "name": ["--report"], + "action": "store_true", + "help": "Show system information for reporting bugs", + }, ], "subcommands": { "title": "commands", - "required": True, + "required": False, "commands": [ { "name": ["init"], @@ -685,6 +697,21 @@ def main() -> None: raise NoCommandFoundError() raise e + if getattr(args, "version", False): + out.write(__version__) + raise ExpectedExit() + + if getattr(args, "report", False) and ( + not hasattr(args, "func") or args.func is not commands.Version + ): + out.write(f"Commitizen Version: {__version__}") + out.write(f"Python Version: {sys.version}") + out.write(f"Operating System: {platform.system()}") + raise ExpectedExit() + + if not hasattr(args, "func"): + raise NoCommandFoundError() + arguments = vars(args) if unknown_args: # Raise error for extra-args without -- separation diff --git a/commitizen/commands/version.py b/commitizen/commands/version.py index 2a4bcea88..2376589d4 100644 --- a/commitizen/commands/version.py +++ b/commitizen/commands/version.py @@ -1,5 +1,6 @@ import platform import sys +import warnings from typing import TypedDict from packaging.version import InvalidVersion @@ -45,6 +46,12 @@ def __init__(self, config: BaseConfig, arguments: VersionArgs) -> None: def __call__(self) -> None: if self.arguments.get("report"): + warnings.warn( + "`cz version --report` is deprecated and will be removed in v5. " + "Use `cz --report` instead.", + DeprecationWarning, + stacklevel=2, + ) out.write(f"Commitizen Version: {__version__}") out.write(f"Python Version: {sys.version}") out.write(f"Operating System: {platform.system()}") @@ -54,6 +61,12 @@ def __call__(self) -> None: out.write(f"Installed Commitizen Version: {__version__}") if self.arguments.get("commitizen"): + warnings.warn( + "`cz version --commitizen` is deprecated and will be removed in v5. " + "Use `cz --version` instead.", + DeprecationWarning, + stacklevel=2, + ) out.write(__version__) return diff --git a/tests/commands/test_version_command.py b/tests/commands/test_version_command.py index 099e7110e..bbca193c7 100644 --- a/tests/commands/test_version_command.py +++ b/tests/commands/test_version_command.py @@ -296,3 +296,23 @@ def test_version_no_arguments_shows_commitizen_version(config, capsys): commands.Version(config, {})() captured = capsys.readouterr() assert captured.out.strip() == __version__ + + +def test_version_report_emits_deprecation_warning(config, capsys): + with pytest.warns( + DeprecationWarning, + match=r"`cz version --report` is deprecated.*Use `cz --report` instead", + ): + commands.Version(config, {"report": True})() + captured = capsys.readouterr() + assert f"Commitizen Version: {__version__}" in captured.out + + +def test_version_commitizen_emits_deprecation_warning(config, capsys): + with pytest.warns( + DeprecationWarning, + match=r"`cz version --commitizen` is deprecated.*Use `cz --version` instead", + ): + commands.Version(config, {"commitizen": True})() + captured = capsys.readouterr() + assert __version__ in captured.out diff --git a/tests/test_cli.py b/tests/test_cli.py index c1f6d5bed..1d08df2a2 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -8,6 +8,7 @@ from pytest_mock import MockFixture from commitizen import cli +from commitizen.__version__ import __version__ from commitizen.exceptions import ( ConfigFileNotFound, ExpectedExit, @@ -55,6 +56,48 @@ def test_cz_with_arg_but_without_command(util: UtilFixture): assert "Command is required" in str(excinfo.value) +def test_cz_with_version_arg(util: UtilFixture, capsys): + """Test that cz shows the version when --version is used.""" + with pytest.raises(ExpectedExit): + util.run_cli("--version") + out, _ = capsys.readouterr() + assert __version__ in out + + +def test_cz_with_version_short_arg(util: UtilFixture, capsys): + """Test that cz shows the version when -v is used.""" + with pytest.raises(ExpectedExit): + util.run_cli("-v") + out, _ = capsys.readouterr() + assert __version__ in out + + +def test_cz_version_flag_takes_precedence_over_subcommand(util: UtilFixture, capsys): + """Test that --version takes precedence even when a subcommand is given.""" + with pytest.raises(ExpectedExit): + util.run_cli("--version", "bump") + out, _ = capsys.readouterr() + assert __version__ in out + + +def test_cz_with_report_arg(util: UtilFixture, capsys): + """Test that cz shows the report when --report is used.""" + with pytest.raises(ExpectedExit): + util.run_cli("--report") + out, _ = capsys.readouterr() + assert "Commitizen Version:" in out + assert "Python Version:" in out + assert "Operating System:" in out + + +def test_cz_report_flag_takes_precedence_over_subcommand(util: UtilFixture, capsys): + """Test that --report takes precedence over non-version subcommands.""" + with pytest.raises(ExpectedExit): + util.run_cli("--report", "bump") + out, _ = capsys.readouterr() + assert "Commitizen Version:" in out + + def test_name(util: UtilFixture, capsys): util.run_cli("-n", "cz_jira", "example") out, _ = capsys.readouterr() 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 index 148b4eacd..e69de29bb 100644 --- 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 @@ -1,4 +0,0 @@ -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 index e2d4416b8..ac64f94f9 100644 --- a/tests/test_cli/test_invalid_command_py_3_10_invalidCommand_.txt +++ b/tests/test_cli/test_invalid_command_py_3_10_invalidCommand_.txt @@ -1,4 +1,5 @@ -usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] +usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v] + [--report] {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 index 148b4eacd..e69de29bb 100644 --- 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 @@ -1,4 +0,0 @@ -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 index e2d4416b8..ac64f94f9 100644 --- a/tests/test_cli/test_invalid_command_py_3_11_invalidCommand_.txt +++ b/tests/test_cli/test_invalid_command_py_3_11_invalidCommand_.txt @@ -1,4 +1,5 @@ -usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] +usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v] + [--report] {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 index 148b4eacd..e69de29bb 100644 --- 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 @@ -1,4 +0,0 @@ -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 index c92220c4d..e3b00a23b 100644 --- a/tests/test_cli/test_invalid_command_py_3_12_invalidCommand_.txt +++ b/tests/test_cli/test_invalid_command_py_3_12_invalidCommand_.txt @@ -1,4 +1,5 @@ -usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] +usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v] + [--report] {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 index 4f0ba2b14..e69de29bb 100644 --- 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 @@ -1,3 +0,0 @@ -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 index 749066c55..fa8e3d893 100644 --- a/tests/test_cli/test_invalid_command_py_3_13_invalidCommand_.txt +++ b/tests/test_cli/test_invalid_command_py_3_13_invalidCommand_.txt @@ -1,3 +1,4 @@ -usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] +usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v] + [--report] {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 index 4f0ba2b14..e69de29bb 100644 --- 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 @@ -1,3 +0,0 @@ -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 index 749066c55..fa8e3d893 100644 --- a/tests/test_cli/test_invalid_command_py_3_14_invalidCommand_.txt +++ b/tests/test_cli/test_invalid_command_py_3_14_invalidCommand_.txt @@ -1,3 +1,4 @@ -usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] +usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v] + [--report] {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 index 69f410e96..aba56be44 100644 --- a/tests/test_cli/test_no_argv_py_3_10_.txt +++ b/tests/test_cli/test_no_argv_py_3_10_.txt @@ -1,4 +1,5 @@ -usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] +usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v] + [--report] {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ... @@ -16,6 +17,8 @@ options: e.g., cz -nr 1,2,3 bump. See codes at https://commitizen- tools.github.io/commitizen/exit_codes/ + -v, --version Show the version of the installed commitizen + --report Show system information for reporting bugs commands: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} diff --git a/tests/test_cli/test_no_argv_py_3_11_.txt b/tests/test_cli/test_no_argv_py_3_11_.txt index 69f410e96..aba56be44 100644 --- a/tests/test_cli/test_no_argv_py_3_11_.txt +++ b/tests/test_cli/test_no_argv_py_3_11_.txt @@ -1,4 +1,5 @@ -usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] +usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v] + [--report] {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ... @@ -16,6 +17,8 @@ options: e.g., cz -nr 1,2,3 bump. See codes at https://commitizen- tools.github.io/commitizen/exit_codes/ + -v, --version Show the version of the installed commitizen + --report Show system information for reporting bugs commands: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} diff --git a/tests/test_cli/test_no_argv_py_3_12_.txt b/tests/test_cli/test_no_argv_py_3_12_.txt index 69f410e96..aba56be44 100644 --- a/tests/test_cli/test_no_argv_py_3_12_.txt +++ b/tests/test_cli/test_no_argv_py_3_12_.txt @@ -1,4 +1,5 @@ -usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] +usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v] + [--report] {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ... @@ -16,6 +17,8 @@ options: e.g., cz -nr 1,2,3 bump. See codes at https://commitizen- tools.github.io/commitizen/exit_codes/ + -v, --version Show the version of the installed commitizen + --report Show system information for reporting bugs commands: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} diff --git a/tests/test_cli/test_no_argv_py_3_13_.txt b/tests/test_cli/test_no_argv_py_3_13_.txt index b47528ec3..b90fc6834 100644 --- a/tests/test_cli/test_no_argv_py_3_13_.txt +++ b/tests/test_cli/test_no_argv_py_3_13_.txt @@ -1,4 +1,5 @@ -usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] +usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v] + [--report] {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. @@ -15,6 +16,8 @@ options: e.g., cz -nr 1,2,3 bump. See codes at https://commitizen- tools.github.io/commitizen/exit_codes/ + -v, --version Show the version of the installed commitizen + --report Show system information for reporting bugs commands: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} diff --git a/tests/test_cli/test_no_argv_py_3_14_.txt b/tests/test_cli/test_no_argv_py_3_14_.txt index b47528ec3..b90fc6834 100644 --- a/tests/test_cli/test_no_argv_py_3_14_.txt +++ b/tests/test_cli/test_no_argv_py_3_14_.txt @@ -1,4 +1,5 @@ -usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] +usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v] + [--report] {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. @@ -15,6 +16,8 @@ options: e.g., cz -nr 1,2,3 bump. See codes at https://commitizen- tools.github.io/commitizen/exit_codes/ + -v, --version Show the version of the installed commitizen + --report Show system information for reporting bugs commands: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}