| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import json |
| 4 |
from pathlib import Path |
| 5 |
from typing import TYPE_CHECKING, Any |
| 6 |
|
| 7 |
import pytest |
| 8 |
import yaml |
| 9 |
|
| 10 |
from commitizen import cmd, commands |
| 11 |
from commitizen.__version__ import __version__ |
| 12 |
from commitizen.exceptions import InitFailedError, NoAnswersError |
| 13 |
|
| 14 |
if TYPE_CHECKING: |
| 15 |
from pytest_mock import MockFixture |
| 16 |
|
| 17 |
from commitizen.config.base_config import BaseConfig |
| 18 |
|
| 19 |
|
| 20 |
class FakeQuestion: |
| 21 |
def __init__(self, expected_return): |
| 22 |
self.expected_return = expected_return |
| 23 |
|
| 24 |
def ask(self): |
| 25 |
return self.expected_return |
| 26 |
|
| 27 |
def unsafe_ask(self): |
| 28 |
return self.expected_return |
| 29 |
|
| 30 |
|
| 31 |
pre_commit_config_filename = ".pre-commit-config.yaml" |
| 32 |
cz_hook_config = { |
| 33 |
"repo": "https://github.com/commitizen-tools/commitizen", |
| 34 |
"rev": f"v{__version__}", |
| 35 |
"hooks": [ |
| 36 |
{"id": "commitizen"}, |
| 37 |
{"id": "commitizen-branch", "stages": ["pre-push"]}, |
| 38 |
], |
| 39 |
} |
| 40 |
|
| 41 |
expected_config = ( |
| 42 |
"[tool.commitizen]\n" |
| 43 |
'name = "cz_conventional_commits"\n' |
| 44 |
'tag_format = "$version"\n' |
| 45 |
'version_scheme = "semver"\n' |
| 46 |
'version = "0.0.1"\n' |
| 47 |
"update_changelog_on_bump = true\n" |
| 48 |
"major_version_zero = true\n" |
| 49 |
) |
| 50 |
|
| 51 |
EXPECTED_DICT_CONFIG = { |
| 52 |
"commitizen": { |
| 53 |
"name": "cz_conventional_commits", |
| 54 |
"tag_format": "$version", |
| 55 |
"version_scheme": "semver", |
| 56 |
"version": "0.0.1", |
| 57 |
"update_changelog_on_bump": True, |
| 58 |
"major_version_zero": True, |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
def test_init_without_setup_pre_commit_hook( |
| 64 |
tmp_path, monkeypatch, mocker: MockFixture, config: BaseConfig |
| 65 |
): |
| 66 |
mocker.patch( |
| 67 |
"questionary.select", |
| 68 |
side_effect=[ |
| 69 |
FakeQuestion("pyproject.toml"), |
| 70 |
FakeQuestion("cz_conventional_commits"), |
| 71 |
FakeQuestion("commitizen"), |
| 72 |
FakeQuestion("semver"), |
| 73 |
], |
| 74 |
) |
| 75 |
mocker.patch("questionary.confirm", return_value=FakeQuestion(True)) |
| 76 |
mocker.patch("questionary.text", return_value=FakeQuestion("$version")) |
| 77 |
# Return None to skip hook installation |
| 78 |
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None)) |
| 79 |
monkeypatch.chdir(tmp_path) |
| 80 |
commands.Init(config)() |
| 81 |
|
| 82 |
config_data = Path("pyproject.toml").read_text(encoding="utf-8") |
| 83 |
assert config_data == expected_config |
| 84 |
|
| 85 |
assert not Path(pre_commit_config_filename).exists() |
| 86 |
|
| 87 |
|
| 88 |
def test_init_when_config_already_exists(config: BaseConfig, capsys): |
| 89 |
# Set config path |
| 90 |
path = Path("tests") / "pyproject.toml" |
| 91 |
config.path = path |
| 92 |
|
| 93 |
commands.Init(config)() |
| 94 |
captured = capsys.readouterr() |
| 95 |
assert captured.out == f"Config file {path} already exists\n" |
| 96 |
|
| 97 |
|
| 98 |
def test_init_without_choosing_tag( |
| 99 |
config: BaseConfig, mocker: MockFixture, tmp_path, monkeypatch |
| 100 |
): |
| 101 |
mocker.patch( |
| 102 |
"commitizen.commands.init.get_tag_names", return_value=["0.0.2", "0.0.1"] |
| 103 |
) |
| 104 |
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value="0.0.2") |
| 105 |
mocker.patch( |
| 106 |
"questionary.select", |
| 107 |
side_effect=[ |
| 108 |
FakeQuestion("pyproject.toml"), |
| 109 |
FakeQuestion("cz_conventional_commits"), |
| 110 |
FakeQuestion("commitizen"), |
| 111 |
FakeQuestion(""), |
| 112 |
], |
| 113 |
) |
| 114 |
mocker.patch("questionary.confirm", return_value=FakeQuestion(False)) |
| 115 |
mocker.patch("questionary.text", return_value=FakeQuestion("y")) |
| 116 |
|
| 117 |
monkeypatch.chdir(tmp_path) |
| 118 |
with pytest.raises(NoAnswersError): |
| 119 |
commands.Init(config)() |
| 120 |
|
| 121 |
|
| 122 |
@pytest.fixture |
| 123 |
def pre_commit_installed(mocker: MockFixture): |
| 124 |
# Assume only `pre-commit` is installed |
| 125 |
mocker.patch( |
| 126 |
"commitizen.project_info.available_hook_installers", |
| 127 |
return_value=["pre-commit"], |
| 128 |
) |
| 129 |
# And installation success (i.e. no exception raised) |
| 130 |
mocker.patch( |
| 131 |
"commitizen.cmd.run", |
| 132 |
return_value=cmd.Command("0.0.1", "", b"", b"", 0), |
| 133 |
) |
| 134 |
|
| 135 |
|
| 136 |
@pytest.fixture(params=["pyproject.toml", ".cz.json", ".cz.yaml"]) |
| 137 |
def default_choice(request, mocker: MockFixture): |
| 138 |
mocker.patch( |
| 139 |
"questionary.select", |
| 140 |
side_effect=[ |
| 141 |
FakeQuestion(request.param), |
| 142 |
FakeQuestion("cz_conventional_commits"), |
| 143 |
FakeQuestion("commitizen"), |
| 144 |
FakeQuestion("semver"), |
| 145 |
], |
| 146 |
) |
| 147 |
mocker.patch("questionary.confirm", return_value=FakeQuestion(True)) |
| 148 |
mocker.patch("questionary.text", return_value=FakeQuestion("$version")) |
| 149 |
mocker.patch( |
| 150 |
"questionary.checkbox", |
| 151 |
return_value=FakeQuestion(["commit-msg", "pre-push"]), |
| 152 |
) |
| 153 |
return request.param |
| 154 |
|
| 155 |
|
| 156 |
def check_cz_config(config_filepath: str): |
| 157 |
""" |
| 158 |
Check the content of commitizen config is as expected |
| 159 |
""" |
| 160 |
content = Path(config_filepath).read_text(encoding="utf-8") |
| 161 |
if "json" in config_filepath: |
| 162 |
assert json.loads(content) == EXPECTED_DICT_CONFIG |
| 163 |
elif "yaml" in config_filepath: |
| 164 |
assert yaml.load(content, Loader=yaml.FullLoader) == EXPECTED_DICT_CONFIG |
| 165 |
else: |
| 166 |
assert content == expected_config |
| 167 |
|
| 168 |
|
| 169 |
def check_pre_commit_config(expected: list[dict[str, Any]]): |
| 170 |
""" |
| 171 |
Check the content of pre-commit config is as expected |
| 172 |
""" |
| 173 |
pre_commit_config_data = yaml.safe_load( |
| 174 |
Path(pre_commit_config_filename).read_text(encoding="utf-8") |
| 175 |
) |
| 176 |
assert pre_commit_config_data == {"repos": expected} |
| 177 |
|
| 178 |
|
| 179 |
@pytest.mark.usefixtures("pre_commit_installed") |
| 180 |
class TestPreCommitCases: |
| 181 |
def test_no_existing_pre_commit_config( |
| 182 |
self, default_choice: str, tmp_path, monkeypatch, config: BaseConfig |
| 183 |
): |
| 184 |
monkeypatch.chdir(tmp_path) |
| 185 |
commands.Init(config)() |
| 186 |
check_cz_config(default_choice) |
| 187 |
check_pre_commit_config([cz_hook_config]) |
| 188 |
|
| 189 |
def test_empty_pre_commit_config( |
| 190 |
self, default_choice: str, tmp_path, monkeypatch, config: BaseConfig |
| 191 |
): |
| 192 |
monkeypatch.chdir(tmp_path) |
| 193 |
p = tmp_path / pre_commit_config_filename |
| 194 |
p.write_text("") |
| 195 |
|
| 196 |
commands.Init(config)() |
| 197 |
check_cz_config(default_choice) |
| 198 |
check_pre_commit_config([cz_hook_config]) |
| 199 |
|
| 200 |
def test_pre_commit_config_without_cz_hook( |
| 201 |
self, default_choice: str, tmp_path, monkeypatch, config: BaseConfig |
| 202 |
): |
| 203 |
existing_hook_config = { |
| 204 |
"repo": "https://github.com/pre-commit/pre-commit-hooks", |
| 205 |
"rev": "v1.2.3", |
| 206 |
"hooks": [{"id", "trailing-whitespace"}], |
| 207 |
} |
| 208 |
|
| 209 |
monkeypatch.chdir(tmp_path) |
| 210 |
p = tmp_path / pre_commit_config_filename |
| 211 |
p.write_text(yaml.safe_dump({"repos": [existing_hook_config]})) |
| 212 |
|
| 213 |
commands.Init(config)() |
| 214 |
check_cz_config(default_choice) |
| 215 |
check_pre_commit_config([existing_hook_config, cz_hook_config]) |
| 216 |
|
| 217 |
def test_cz_hook_exists_in_pre_commit_config( |
| 218 |
self, default_choice: str, tmp_path, monkeypatch, config: BaseConfig |
| 219 |
): |
| 220 |
monkeypatch.chdir(tmp_path) |
| 221 |
p = tmp_path / pre_commit_config_filename |
| 222 |
p.write_text(yaml.safe_dump({"repos": [cz_hook_config]})) |
| 223 |
|
| 224 |
commands.Init(config)() |
| 225 |
check_cz_config(default_choice) |
| 226 |
# check that config is not duplicated |
| 227 |
check_pre_commit_config([cz_hook_config]) |
| 228 |
|
| 229 |
|
| 230 |
class TestNoPreCommitInstalled: |
| 231 |
def test_skips_hook_question_when_neither_installer_is_installed( |
| 232 |
self, mocker: MockFixture, config: BaseConfig, tmp_path, monkeypatch, capsys |
| 233 |
): |
| 234 |
mocker.patch( |
| 235 |
"questionary.select", |
| 236 |
side_effect=[ |
| 237 |
FakeQuestion("pyproject.toml"), |
| 238 |
FakeQuestion("cz_conventional_commits"), |
| 239 |
FakeQuestion("commitizen"), |
| 240 |
FakeQuestion("semver"), |
| 241 |
], |
| 242 |
) |
| 243 |
mocker.patch("questionary.confirm", return_value=FakeQuestion(True)) |
| 244 |
mocker.patch("questionary.text", return_value=FakeQuestion("$version")) |
| 245 |
checkbox = mocker.patch("questionary.checkbox") |
| 246 |
mocker.patch( |
| 247 |
"commitizen.project_info.available_hook_installers", |
| 248 |
return_value=[], |
| 249 |
) |
| 250 |
monkeypatch.chdir(tmp_path) |
| 251 |
|
| 252 |
commands.Init(config)() |
| 253 |
|
| 254 |
checkbox.assert_not_called() |
| 255 |
captured = capsys.readouterr() |
| 256 |
assert "No pre-commit hook detected, skipping question" in captured.out |
| 257 |
assert Path("pyproject.toml").read_text(encoding="utf-8") == expected_config |
| 258 |
assert not Path(pre_commit_config_filename).exists() |
| 259 |
|
| 260 |
|
| 261 |
def _init_hook_answers(mocker: MockFixture) -> None: |
| 262 |
"""Stub the interactive init prompts and select hook installation.""" |
| 263 |
mocker.patch( |
| 264 |
"questionary.select", |
| 265 |
side_effect=[ |
| 266 |
FakeQuestion("pyproject.toml"), |
| 267 |
FakeQuestion("cz_conventional_commits"), |
| 268 |
FakeQuestion("commitizen"), |
| 269 |
FakeQuestion("semver"), |
| 270 |
], |
| 271 |
) |
| 272 |
mocker.patch("questionary.confirm", return_value=FakeQuestion(True)) |
| 273 |
mocker.patch("questionary.text", return_value=FakeQuestion("$version")) |
| 274 |
mocker.patch( |
| 275 |
"questionary.checkbox", |
| 276 |
return_value=FakeQuestion(["commit-msg", "pre-push"]), |
| 277 |
) |
| 278 |
|
| 279 |
|
| 280 |
class TestHookInstallerSelection: |
| 281 |
def test_uses_prek_when_only_prek_is_installed( |
| 282 |
self, mocker: MockFixture, config: BaseConfig, tmp_path, monkeypatch |
| 283 |
): |
| 284 |
_init_hook_answers(mocker) |
| 285 |
mocker.patch( |
| 286 |
"commitizen.project_info.available_hook_installers", |
| 287 |
return_value=["prek"], |
| 288 |
) |
| 289 |
run = mocker.patch( |
| 290 |
"commitizen.cmd.run", |
| 291 |
return_value=cmd.Command("", "", b"", b"", 0), |
| 292 |
) |
| 293 |
monkeypatch.chdir(tmp_path) |
| 294 |
|
| 295 |
commands.Init(config)() |
| 296 |
|
| 297 |
run.assert_any_call( |
| 298 |
["prek", "install", "--hook-type", "commit-msg", "--hook-type", "pre-push"] |
| 299 |
) |
| 300 |
|
| 301 |
def test_asks_when_both_installers_are_present( |
| 302 |
self, mocker: MockFixture, config: BaseConfig, tmp_path, monkeypatch |
| 303 |
): |
| 304 |
mocker.patch( |
| 305 |
"questionary.select", |
| 306 |
side_effect=[ |
| 307 |
FakeQuestion("pyproject.toml"), |
| 308 |
FakeQuestion("cz_conventional_commits"), |
| 309 |
FakeQuestion("commitizen"), |
| 310 |
FakeQuestion("semver"), |
| 311 |
FakeQuestion("prek"), |
| 312 |
], |
| 313 |
) |
| 314 |
mocker.patch("questionary.confirm", return_value=FakeQuestion(True)) |
| 315 |
mocker.patch("questionary.text", return_value=FakeQuestion("$version")) |
| 316 |
mocker.patch( |
| 317 |
"questionary.checkbox", |
| 318 |
return_value=FakeQuestion(["commit-msg"]), |
| 319 |
) |
| 320 |
mocker.patch( |
| 321 |
"commitizen.project_info.available_hook_installers", |
| 322 |
return_value=["pre-commit", "prek"], |
| 323 |
) |
| 324 |
run = mocker.patch( |
| 325 |
"commitizen.cmd.run", |
| 326 |
return_value=cmd.Command("", "", b"", b"", 0), |
| 327 |
) |
| 328 |
monkeypatch.chdir(tmp_path) |
| 329 |
|
| 330 |
commands.Init(config)() |
| 331 |
|
| 332 |
run.assert_any_call(["prek", "install", "--hook-type", "commit-msg"]) |
| 333 |
|
| 334 |
def test_uses_pre_commit_when_only_pre_commit_is_installed( |
| 335 |
self, mocker: MockFixture, config: BaseConfig, tmp_path, monkeypatch |
| 336 |
): |
| 337 |
_init_hook_answers(mocker) |
| 338 |
mocker.patch( |
| 339 |
"commitizen.project_info.available_hook_installers", |
| 340 |
return_value=["pre-commit"], |
| 341 |
) |
| 342 |
run = mocker.patch( |
| 343 |
"commitizen.cmd.run", |
| 344 |
return_value=cmd.Command("", "", b"", b"", 0), |
| 345 |
) |
| 346 |
monkeypatch.chdir(tmp_path) |
| 347 |
|
| 348 |
commands.Init(config)() |
| 349 |
|
| 350 |
run.assert_any_call( |
| 351 |
[ |
| 352 |
"pre-commit", |
| 353 |
"install", |
| 354 |
"--hook-type", |
| 355 |
"commit-msg", |
| 356 |
"--hook-type", |
| 357 |
"pre-push", |
| 358 |
] |
| 359 |
) |
| 360 |
|
| 361 |
def test_fails_when_installer_disappears_between_prompt_and_install( |
| 362 |
self, mocker: MockFixture, config: BaseConfig, tmp_path, monkeypatch |
| 363 |
): |
| 364 |
_init_hook_answers(mocker) |
| 365 |
# First call (during _ask_hook_types) finds pre-commit; second call |
| 366 |
# (during _ask_hook_installer, after the config is written) finds none, |
| 367 |
# e.g. the tool was uninstalled or the PATH changed in between. |
| 368 |
mocker.patch( |
| 369 |
"commitizen.project_info.available_hook_installers", |
| 370 |
side_effect=[["pre-commit"], []], |
| 371 |
) |
| 372 |
run = mocker.patch( |
| 373 |
"commitizen.cmd.run", |
| 374 |
return_value=cmd.Command("", "", b"", b"", 0), |
| 375 |
) |
| 376 |
monkeypatch.chdir(tmp_path) |
| 377 |
|
| 378 |
with pytest.raises(InitFailedError): |
| 379 |
commands.Init(config)() |
| 380 |
|
| 381 |
# Other subprocess calls (git describe, git config) may still happen, |
| 382 |
# but no hook installer may have been invoked. |
| 383 |
assert not any( |
| 384 |
"--hook-type" in " ".join(call.args[0]) for call in run.call_args_list |
| 385 |
) |
| 386 |
|
| 387 |
|
| 388 |
class TestAskTagFormat: |
| 389 |
def test_confirm_v_tag_format(self, mocker: MockFixture, config: BaseConfig): |
| 390 |
init = commands.Init(config) |
| 391 |
mocker.patch("questionary.confirm", return_value=FakeQuestion(True)) |
| 392 |
|
| 393 |
result = init._ask_tag_format("v1.0.0") |
| 394 |
assert result == r"v$version" |
| 395 |
|
| 396 |
def test_reject_v_tag_format(self, mocker: MockFixture, config: BaseConfig): |
| 397 |
init = commands.Init(config) |
| 398 |
mocker.patch("questionary.confirm", return_value=FakeQuestion(False)) |
| 399 |
mocker.patch("questionary.text", return_value=FakeQuestion("custom-$version")) |
| 400 |
|
| 401 |
result = init._ask_tag_format("v1.0.0") |
| 402 |
assert result == "custom-$version" |
| 403 |
|
| 404 |
def test_non_v_tag_format(self, mocker: MockFixture, config: BaseConfig): |
| 405 |
init = commands.Init(config) |
| 406 |
mocker.patch("questionary.text", return_value=FakeQuestion("custom-$version")) |
| 407 |
|
| 408 |
result = init._ask_tag_format("1.0.0") |
| 409 |
assert result == "custom-$version" |
| 410 |
|
| 411 |
def test_empty_input_returns_default(self, mocker: MockFixture, config: BaseConfig): |
| 412 |
init = commands.Init(config) |
| 413 |
mocker.patch("questionary.confirm", return_value=FakeQuestion(False)) |
| 414 |
mocker.patch("questionary.text", return_value=FakeQuestion("")) |
| 415 |
|
| 416 |
result = init._ask_tag_format("v1.0.0") |
| 417 |
assert result == "$version" # This is the default format from DEFAULT_SETTINGS |
| 418 |
|
| 419 |
|
| 420 |
def test_init_with_confirmed_tag_format( |
| 421 |
config: BaseConfig, mocker: MockFixture, tmp_path, monkeypatch |
| 422 |
): |
| 423 |
mocker.patch( |
| 424 |
"commitizen.commands.init.get_tag_names", return_value=["v0.0.2", "v0.0.1"] |
| 425 |
) |
| 426 |
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value="v0.0.2") |
| 427 |
mocker.patch( |
| 428 |
"questionary.select", |
| 429 |
side_effect=[ |
| 430 |
FakeQuestion("pyproject.toml"), |
| 431 |
FakeQuestion("cz_conventional_commits"), |
| 432 |
FakeQuestion("commitizen"), |
| 433 |
FakeQuestion("semver"), |
| 434 |
], |
| 435 |
) |
| 436 |
mocker.patch("questionary.confirm", return_value=FakeQuestion(True)) |
| 437 |
mocker.patch("questionary.text", return_value=FakeQuestion("$version")) |
| 438 |
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None)) |
| 439 |
|
| 440 |
monkeypatch.chdir(tmp_path) |
| 441 |
commands.Init(config)() |
| 442 |
assert 'tag_format = "v$version"' in Path("pyproject.toml").read_text( |
| 443 |
encoding="utf-8" |
| 444 |
) |
| 445 |
|
| 446 |
|
| 447 |
def test_init_with_no_existing_tags( |
| 448 |
config: BaseConfig, mocker: MockFixture, tmp_path, monkeypatch |
| 449 |
): |
| 450 |
mocker.patch("commitizen.commands.init.get_tag_names", return_value=[]) |
| 451 |
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value="v1.0.0") |
| 452 |
mocker.patch( |
| 453 |
"questionary.select", |
| 454 |
side_effect=[ |
| 455 |
FakeQuestion("pyproject.toml"), |
| 456 |
FakeQuestion("cz_conventional_commits"), |
| 457 |
FakeQuestion("commitizen"), |
| 458 |
FakeQuestion("semver"), |
| 459 |
], |
| 460 |
) |
| 461 |
mocker.patch("questionary.confirm", return_value=FakeQuestion(False)) |
| 462 |
mocker.patch("questionary.text", return_value=FakeQuestion("$version")) |
| 463 |
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None)) |
| 464 |
|
| 465 |
monkeypatch.chdir(tmp_path) |
| 466 |
commands.Init(config)() |
| 467 |
assert 'version = "0.0.1"' in Path("pyproject.toml").read_text(encoding="utf-8") |
| 468 |
|
| 469 |
|
| 470 |
def test_init_with_no_existing_latest_tag( |
| 471 |
config: BaseConfig, mocker: MockFixture, tmp_path, monkeypatch |
| 472 |
): |
| 473 |
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value=None) |
| 474 |
mocker.patch( |
| 475 |
"questionary.select", |
| 476 |
side_effect=[ |
| 477 |
FakeQuestion("pyproject.toml"), |
| 478 |
FakeQuestion("cz_conventional_commits"), |
| 479 |
FakeQuestion("commitizen"), |
| 480 |
FakeQuestion("semver"), |
| 481 |
], |
| 482 |
) |
| 483 |
mocker.patch("questionary.confirm", return_value=FakeQuestion(True)) |
| 484 |
mocker.patch("questionary.text", return_value=FakeQuestion("$version")) |
| 485 |
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None)) |
| 486 |
|
| 487 |
monkeypatch.chdir(tmp_path) |
| 488 |
commands.Init(config)() |
| 489 |
assert 'version = "0.0.1"' in Path("pyproject.toml").read_text(encoding="utf-8") |
| 490 |
|
| 491 |
|
| 492 |
def test_init_with_existing_tags( |
| 493 |
config: BaseConfig, mocker: MockFixture, tmp_path, monkeypatch |
| 494 |
): |
| 495 |
expected_tags = ["v1.0.0", "v0.9.0", "v0.8.0"] |
| 496 |
mocker.patch("commitizen.commands.init.get_tag_names", return_value=expected_tags) |
| 497 |
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value="v1.0.0") |
| 498 |
mocker.patch( |
| 499 |
"questionary.select", |
| 500 |
side_effect=[ |
| 501 |
FakeQuestion("pyproject.toml"), |
| 502 |
FakeQuestion("cz_conventional_commits"), |
| 503 |
FakeQuestion("commitizen"), |
| 504 |
FakeQuestion("semver"), # Select version scheme first |
| 505 |
FakeQuestion("v1.0.0"), # Then select the latest tag |
| 506 |
], |
| 507 |
) |
| 508 |
mocker.patch("questionary.confirm", return_value=FakeQuestion(True)) |
| 509 |
mocker.patch("questionary.text", return_value=FakeQuestion("$version")) |
| 510 |
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None)) |
| 511 |
|
| 512 |
monkeypatch.chdir(tmp_path) |
| 513 |
commands.Init(config)() |
| 514 |
assert 'version = "1.0.0"' in Path("pyproject.toml").read_text(encoding="utf-8") |
| 515 |
|
| 516 |
|
| 517 |
def test_init_with_valid_tag_selection( |
| 518 |
config: BaseConfig, mocker: MockFixture, tmp_path, monkeypatch |
| 519 |
): |
| 520 |
expected_tags = ["v1.0.0", "v0.9.0", "v0.8.0"] |
| 521 |
mocker.patch("commitizen.commands.init.get_tag_names", return_value=expected_tags) |
| 522 |
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value="v1.0.0") |
| 523 |
|
| 524 |
# Mock all questionary.select calls in the exact order they appear in Init.__call__ |
| 525 |
mocker.patch( |
| 526 |
"questionary.select", |
| 527 |
side_effect=[ |
| 528 |
FakeQuestion("pyproject.toml"), # _ask_config_path |
| 529 |
FakeQuestion("cz_conventional_commits"), # _ask_name |
| 530 |
FakeQuestion("commitizen"), # _ask_version_provider |
| 531 |
FakeQuestion("v0.9.0"), # _ask_tag (after confirm=False) |
| 532 |
FakeQuestion("semver"), # _ask_version_scheme |
| 533 |
], |
| 534 |
) |
| 535 |
|
| 536 |
mocker.patch( |
| 537 |
"questionary.confirm", return_value=FakeQuestion(False) |
| 538 |
) # Don't confirm latest tag |
| 539 |
mocker.patch("questionary.text", return_value=FakeQuestion("$version")) |
| 540 |
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None)) |
| 541 |
|
| 542 |
monkeypatch.chdir(tmp_path) |
| 543 |
commands.Init(config)() |
| 544 |
content = Path("pyproject.toml").read_text(encoding="utf-8") |
| 545 |
assert 'version = "0.9.0"' in content |
| 546 |
assert 'version_scheme = "semver"' in content |
| 547 |
|
| 548 |
|
| 549 |
def test_init_configuration_settings( |
| 550 |
tmp_path, monkeypatch, mocker: MockFixture, config: BaseConfig |
| 551 |
): |
| 552 |
"""Test that all configuration settings are properly initialized.""" |
| 553 |
mocker.patch( |
| 554 |
"questionary.select", |
| 555 |
side_effect=[ |
| 556 |
FakeQuestion("pyproject.toml"), |
| 557 |
FakeQuestion("cz_conventional_commits"), |
| 558 |
FakeQuestion("commitizen"), |
| 559 |
FakeQuestion("semver"), |
| 560 |
], |
| 561 |
) |
| 562 |
mocker.patch("questionary.confirm", return_value=FakeQuestion(True)) |
| 563 |
mocker.patch("questionary.text", return_value=FakeQuestion("$version")) |
| 564 |
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None)) |
| 565 |
|
| 566 |
monkeypatch.chdir(tmp_path) |
| 567 |
commands.Init(config)() |
| 568 |
|
| 569 |
config_data = Path("pyproject.toml").read_text(encoding="utf-8") |
| 570 |
|
| 571 |
# Verify all expected settings are present |
| 572 |
assert 'name = "cz_conventional_commits"' in config_data |
| 573 |
assert 'tag_format = "$version"' in config_data |
| 574 |
assert 'version_scheme = "semver"' in config_data |
| 575 |
assert 'version = "0.0.1"' in config_data |
| 576 |
assert "update_changelog_on_bump = true" in config_data |
| 577 |
assert "major_version_zero = true" in config_data |
| 578 |
|
| 579 |
|
| 580 |
def test_init_configuration_with_version_provider( |
| 581 |
tmp_path, monkeypatch, mocker: MockFixture, config: BaseConfig |
| 582 |
): |
| 583 |
"""Test configuration initialization with a different version provider.""" |
| 584 |
mocker.patch( |
| 585 |
"questionary.select", |
| 586 |
side_effect=[ |
| 587 |
FakeQuestion("pyproject.toml"), |
| 588 |
FakeQuestion("cz_conventional_commits"), |
| 589 |
FakeQuestion("pep621"), # Different version provider |
| 590 |
FakeQuestion("semver"), |
| 591 |
], |
| 592 |
) |
| 593 |
mocker.patch("questionary.confirm", return_value=FakeQuestion(True)) |
| 594 |
mocker.patch("questionary.text", return_value=FakeQuestion("$version")) |
| 595 |
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None)) |
| 596 |
|
| 597 |
monkeypatch.chdir(tmp_path) |
| 598 |
commands.Init(config)() |
| 599 |
|
| 600 |
config_data = Path("pyproject.toml").read_text(encoding="utf-8") |
| 601 |
|
| 602 |
# Verify version provider is set instead of version |
| 603 |
assert 'name = "cz_conventional_commits"' in config_data |
| 604 |
assert 'tag_format = "$version"' in config_data |
| 605 |
assert 'version_scheme = "semver"' in config_data |
| 606 |
assert 'version_provider = "pep621"' in config_data |
| 607 |
assert "update_changelog_on_bump = true" in config_data |
| 608 |
assert "major_version_zero = true" in config_data |
| 609 |
assert ( |
| 610 |
"version = " not in config_data |
| 611 |
) # Version should not be set when using version_provider |
| 612 |
|
| 613 |
|
| 614 |
def test_construct_name_choice_from_registry(config: BaseConfig): |
| 615 |
"""Test the construction of cz name choices with descriptions.""" |
| 616 |
choices = commands.Init(config)._construct_name_choices_from_registry() |
| 617 |
assert choices[0].title == "cz_conventional_commits" |
| 618 |
assert choices[0].value == "cz_conventional_commits" |
| 619 |
assert choices[0].description == "<type>(<scope>): <subject>" |
| 620 |
assert choices[1].title == "cz_customize" |
| 621 |
assert choices[1].value == "cz_customize" |
| 622 |
assert choices[1].description is None |
| 623 |
assert choices[2].title == "cz_jira" |
| 624 |
assert choices[2].value == "cz_jira" |
| 625 |
assert ( |
| 626 |
choices[2].description |
| 627 |
== "<ignored text> <ISSUE_KEY> <ignored text> #<COMMAND> <optional COMMAND_ARGUMENTS>" |
| 628 |
) |