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
27 changes: 26 additions & 1 deletion commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import argparse
import logging
import platform
import sys
from copy import deepcopy
from functools import partial
Expand All @@ -13,6 +14,7 @@
from decli import cli

from commitizen import commands, config, out, version_schemes
from commitizen.__version__ import __version__
from commitizen.exceptions import (
CommitizenException,
ExitCode,
Expand Down Expand Up @@ -102,6 +104,16 @@ 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",
Expand Down Expand Up @@ -629,13 +641,27 @@ 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
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
raise ExpectedExit()

# TODO(bearomorphism): mark `cz version --commitizen` as deprecated after `cz version` feature is stable
if "--version" in sys.argv:
out.write(__version__)
raise ExpectedExit()

# TODO(bearomorphism): mark `cz version --report` as deprecated after `cz version` feature is stable
if "--report" in sys.argv:
out.write(f"Commitizen Version: {__version__}")
out.write(f"Python Version: {sys.version}")
out.write(f"Operating System: {platform.system()}")
raise ExpectedExit()

# This is for the command required constraint in 2.0
try:
args, unknown_args = parser.parse_known_args()
Expand Down Expand Up @@ -673,7 +699,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
19 changes: 19 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pytest_mock import MockFixture

from commitizen import cli
from commitizen.__version__ import __version__
from commitizen.exceptions import (
ConfigFileNotFound,
ExpectedExit,
Expand Down Expand Up @@ -37,6 +38,24 @@ 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_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_name(util: UtilFixture, capsys):
util.run_cli("-n", "cz_jira", "example")
out, _ = capsys.readouterr()
Expand Down