| 1 |
import os |
| 2 |
import re |
| 3 |
import subprocess |
| 4 |
import sys |
| 5 |
import types |
| 6 |
from functools import partial |
| 7 |
|
| 8 |
import pytest |
| 9 |
from pytest_mock import MockFixture |
| 10 |
|
| 11 |
from commitizen import cli |
| 12 |
from commitizen.exceptions import ( |
| 13 |
ConfigFileNotFound, |
| 14 |
ExpectedExit, |
| 15 |
InvalidCommandArgumentError, |
| 16 |
NoCommandFoundError, |
| 17 |
NotAGitProjectError, |
| 18 |
) |
| 19 |
from tests.utils import UtilFixture |
| 20 |
|
| 21 |
ARGPARSE_CHOICES_PATTERN = re.compile(r"\(choose from (?P<choices>[^)]*)\)") |
| 22 |
ARGPARSE_QUOTED_CHOICE_PATTERN = re.compile(r"'([^']*)'") |
| 23 |
|
| 24 |
|
| 25 |
def normalize_argparse_choice_quotes(text: str) -> str: |
| 26 |
def normalize_match(match: re.Match[str]) -> str: |
| 27 |
choices = ARGPARSE_QUOTED_CHOICE_PATTERN.sub(r"\1", match.group("choices")) |
| 28 |
return f"(choose from {choices})" |
| 29 |
|
| 30 |
return ARGPARSE_CHOICES_PATTERN.sub(normalize_match, text) |
| 31 |
|
| 32 |
|
| 33 |
def test_normalize_argparse_choice_quotes(): |
| 34 |
text = ( |
| 35 |
"cz: error: argument {init,commit}: invalid choice: 'invalidCommand' " |
| 36 |
"(choose from 'init', 'commit')" |
| 37 |
) |
| 38 |
|
| 39 |
assert normalize_argparse_choice_quotes(text) == ( |
| 40 |
"cz: error: argument {init,commit}: invalid choice: 'invalidCommand' " |
| 41 |
"(choose from init, commit)" |
| 42 |
) |
| 43 |
|
| 44 |
|
| 45 |
@pytest.mark.usefixtures("python_version", "consistent_terminal_output") |
| 46 |
def test_no_argv(util: UtilFixture, capsys, file_regression): |
| 47 |
with pytest.raises(ExpectedExit): |
| 48 |
util.run_cli() |
| 49 |
out, err = capsys.readouterr() |
| 50 |
assert out == "" |
| 51 |
file_regression.check(err, extension=".txt") |
| 52 |
|
| 53 |
|
| 54 |
@pytest.mark.parametrize( |
| 55 |
"arg", |
| 56 |
["--invalid-arg", "invalidCommand"], |
| 57 |
) |
| 58 |
@pytest.mark.usefixtures("python_version", "consistent_terminal_output") |
| 59 |
def test_invalid_command(util: UtilFixture, capsys, file_regression, arg): |
| 60 |
with pytest.raises(NoCommandFoundError): |
| 61 |
util.run_cli(arg) |
| 62 |
out, err = capsys.readouterr() |
| 63 |
assert out == "" |
| 64 |
if arg == "invalidCommand": |
| 65 |
err = normalize_argparse_choice_quotes(err) |
| 66 |
file_regression.check(err, extension=".txt") |
| 67 |
|
| 68 |
|
| 69 |
def test_cz_config_file_without_correct_file_path(util: UtilFixture): |
| 70 |
with pytest.raises(ConfigFileNotFound, match="Cannot found the config file"): |
| 71 |
util.run_cli("--config", "./config/pyproject.toml", "example") |
| 72 |
|
| 73 |
|
| 74 |
def test_cz_with_arg_but_without_command(util: UtilFixture): |
| 75 |
with pytest.raises(NoCommandFoundError, match="Command is required"): |
| 76 |
util.run_cli("--name", "cz_jira") |
| 77 |
|
| 78 |
|
| 79 |
def test_name(util: UtilFixture, capsys): |
| 80 |
util.run_cli("-n", "cz_jira", "example") |
| 81 |
out, _ = capsys.readouterr() |
| 82 |
assert out.startswith("JRA") |
| 83 |
|
| 84 |
|
| 85 |
@pytest.mark.usefixtures("tmp_git_project") |
| 86 |
def test_name_default_value(util: UtilFixture, capsys): |
| 87 |
util.run_cli("example") |
| 88 |
out, _ = capsys.readouterr() |
| 89 |
assert out.startswith("fix: correct minor typos in code") |
| 90 |
|
| 91 |
|
| 92 |
def test_ls(util: UtilFixture, capsys): |
| 93 |
util.run_cli("-n", "cz_jira", "ls") |
| 94 |
out, _ = capsys.readouterr() |
| 95 |
|
| 96 |
assert "cz_conventional_commits" in out |
| 97 |
assert isinstance(out, str) |
| 98 |
|
| 99 |
|
| 100 |
def test_arg_debug(util: UtilFixture): |
| 101 |
util.run_cli("--debug", "info") |
| 102 |
excepthook = sys.excepthook |
| 103 |
# `sys.excepthook` is replaced by a `partial` in `cli.main` |
| 104 |
# it's not a normal function |
| 105 |
assert isinstance(excepthook, partial) |
| 106 |
assert excepthook.keywords.get("debug") is True |
| 107 |
|
| 108 |
|
| 109 |
def test_commitizen_excepthook(): |
| 110 |
with pytest.raises(SystemExit) as excinfo: |
| 111 |
cli.commitizen_excepthook(NotAGitProjectError, NotAGitProjectError(), "") |
| 112 |
|
| 113 |
assert excinfo.type is SystemExit |
| 114 |
assert excinfo.value.code == NotAGitProjectError.exit_code |
| 115 |
|
| 116 |
|
| 117 |
def test_commitizen_debug_excepthook(): |
| 118 |
with pytest.raises(SystemExit) as excinfo: |
| 119 |
cli.commitizen_excepthook( |
| 120 |
NotAGitProjectError, |
| 121 |
NotAGitProjectError(), |
| 122 |
"", |
| 123 |
debug=True, |
| 124 |
) |
| 125 |
|
| 126 |
assert excinfo.value.code == NotAGitProjectError.exit_code |
| 127 |
assert "NotAGitProjectError" in str(excinfo.traceback[0]) |
| 128 |
|
| 129 |
|
| 130 |
@pytest.mark.skipif( |
| 131 |
os.name == "nt", |
| 132 |
reason="`argcomplete` does not support Git Bash on Windows.", |
| 133 |
) |
| 134 |
def test_argcomplete_activation(): |
| 135 |
""" |
| 136 |
This function is testing the one-time activation of argcomplete for |
| 137 |
commitizen only. |
| 138 |
|
| 139 |
Equivalent to run: |
| 140 |
$ eval "$(register-python-argcomplete pytest)" |
| 141 |
""" |
| 142 |
subprocess.run(["register-python-argcomplete", "cz"], check=True) |
| 143 |
|
| 144 |
|
| 145 |
def test_commitizen_excepthook_no_raises(): |
| 146 |
with pytest.raises(SystemExit) as excinfo: |
| 147 |
cli.commitizen_excepthook( |
| 148 |
NotAGitProjectError, |
| 149 |
NotAGitProjectError(), |
| 150 |
"", |
| 151 |
no_raise=[NotAGitProjectError.exit_code], |
| 152 |
) |
| 153 |
|
| 154 |
assert excinfo.type is SystemExit |
| 155 |
assert excinfo.value.code == 0 |
| 156 |
|
| 157 |
|
| 158 |
@pytest.mark.parametrize( |
| 159 |
("input_str", "expected_result"), |
| 160 |
[ |
| 161 |
pytest.param("1", [1], id="single_code"), |
| 162 |
pytest.param("1,2,3", [1, 2, 3], id="multiple_number_codes"), |
| 163 |
pytest.param( |
| 164 |
"NO_COMMITIZEN_FOUND,NO_COMMITS_FOUND,NO_PATTERN_MAP", |
| 165 |
[1, 3, 5], |
| 166 |
id="string_codes", |
| 167 |
), |
| 168 |
pytest.param( |
| 169 |
"NO_COMMITIZEN_FOUND,2,NO_COMMITS_FOUND,4", |
| 170 |
[1, 2, 3, 4], |
| 171 |
id="number_and_string_codes", |
| 172 |
), |
| 173 |
pytest.param( |
| 174 |
"NO_COMMITIZEN_FOUND,2,nothing,4", |
| 175 |
[1, 2, 4], |
| 176 |
id="number_and_string_codes_and_invalid_code", |
| 177 |
), |
| 178 |
], |
| 179 |
) |
| 180 |
def test_parse_no_raise(input_str, expected_result): |
| 181 |
result = cli.parse_no_raise(input_str) |
| 182 |
assert result == expected_result |
| 183 |
|
| 184 |
|
| 185 |
def test_unknown_args_raises(util: UtilFixture): |
| 186 |
with pytest.raises( |
| 187 |
InvalidCommandArgumentError, match="Invalid commitizen arguments were found" |
| 188 |
): |
| 189 |
util.run_cli("c", "-this_arg_is_not_supported") |
| 190 |
|
| 191 |
|
| 192 |
def test_unknown_args_before_double_dash_raises(util: UtilFixture): |
| 193 |
with pytest.raises( |
| 194 |
InvalidCommandArgumentError, |
| 195 |
match="Invalid commitizen arguments were found before -- separator", |
| 196 |
): |
| 197 |
util.run_cli("c", "-this_arg_is_not_supported", "--") |
| 198 |
|
| 199 |
|
| 200 |
def test_commitizen_excepthook_non_commitizen_exception(mocker: MockFixture): |
| 201 |
"""Test that commitizen_excepthook delegates to sys.__excepthook__ for non-CommitizenException.""" |
| 202 |
# Mock the original excepthook |
| 203 |
mock_original_excepthook = mocker.Mock() |
| 204 |
mocker.patch("commitizen.cli.sys.__excepthook__", mock_original_excepthook) |
| 205 |
|
| 206 |
# Create a regular exception |
| 207 |
test_exception = ValueError("test error") |
| 208 |
|
| 209 |
# Call commitizen_excepthook with the regular exception |
| 210 |
cli.commitizen_excepthook(ValueError, test_exception, None) |
| 211 |
|
| 212 |
# Verify sys.__excepthook__ was called with correct arguments |
| 213 |
mock_original_excepthook.assert_called_once_with(ValueError, test_exception, None) |
| 214 |
|
| 215 |
|
| 216 |
def test_commitizen_excepthook_non_commitizen_exception_with_traceback( |
| 217 |
mocker: MockFixture, |
| 218 |
): |
| 219 |
"""Test that commitizen_excepthook handles traceback correctly for non-CommitizenException.""" |
| 220 |
# Mock the original excepthook |
| 221 |
mock_original_excepthook = mocker.Mock() |
| 222 |
mocker.patch("commitizen.cli.sys.__excepthook__", mock_original_excepthook) |
| 223 |
|
| 224 |
# Create a regular exception with a traceback |
| 225 |
test_exception = ValueError("test error") |
| 226 |
test_traceback = mocker.Mock(spec=types.TracebackType) |
| 227 |
|
| 228 |
# Call commitizen_excepthook with the regular exception and traceback |
| 229 |
cli.commitizen_excepthook(ValueError, test_exception, test_traceback) |
| 230 |
|
| 231 |
# Verify sys.__excepthook__ was called with correct arguments including traceback |
| 232 |
mock_original_excepthook.assert_called_once_with( |
| 233 |
ValueError, test_exception, test_traceback |
| 234 |
) |
| 235 |
|
| 236 |
|
| 237 |
def test_commitizen_excepthook_non_commitizen_exception_with_invalid_traceback( |
| 238 |
mocker: MockFixture, |
| 239 |
): |
| 240 |
"""Test that commitizen_excepthook handles invalid traceback correctly for non-CommitizenException.""" |
| 241 |
# Mock the original excepthook |
| 242 |
mock_original_excepthook = mocker.Mock() |
| 243 |
mocker.patch("commitizen.cli.sys.__excepthook__", mock_original_excepthook) |
| 244 |
|
| 245 |
# Create a regular exception with an invalid traceback |
| 246 |
test_exception = ValueError("test error") |
| 247 |
test_traceback = mocker.Mock() # Not a TracebackType |
| 248 |
|
| 249 |
# Call commitizen_excepthook with the regular exception and invalid traceback |
| 250 |
cli.commitizen_excepthook(ValueError, test_exception, test_traceback) |
| 251 |
|
| 252 |
# Verify sys.__excepthook__ was called with None as traceback |
| 253 |
mock_original_excepthook.assert_called_once_with(ValueError, test_exception, None) |