|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from commitizen.exceptions import CommitMessageLengthExceededError, DryRunExit |
| 8 | +from tests.utils import UtilFixture |
| 9 | + |
| 10 | + |
| 11 | +def _write_pyproject_with_message_length_limit( |
| 12 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, message_length_limit: int |
| 13 | +) -> None: |
| 14 | + (tmp_path / "pyproject.toml").write_text( |
| 15 | + "[tool.commitizen]\n" |
| 16 | + 'name = "cz_conventional_commits"\n' |
| 17 | + f"message_length_limit = {message_length_limit}\n", |
| 18 | + encoding="utf-8", |
| 19 | + ) |
| 20 | + monkeypatch.chdir(tmp_path) |
| 21 | + |
| 22 | + |
| 23 | +def _mock_commit_prompt(mocker, *, subject: str) -> None: |
| 24 | + mocker.patch( |
| 25 | + "questionary.prompt", |
| 26 | + return_value={ |
| 27 | + "prefix": "feat", |
| 28 | + "subject": subject, |
| 29 | + "scope": "", |
| 30 | + "is_breaking_change": False, |
| 31 | + "body": "", |
| 32 | + "footer": "", |
| 33 | + }, |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.usefixtures("tmp_commitizen_project") |
| 38 | +def test_cli_check_reads_message_length_limit_from_pyproject( |
| 39 | + util: UtilFixture, monkeypatch: pytest.MonkeyPatch, tmp_path: Path |
| 40 | +): |
| 41 | + _write_pyproject_with_message_length_limit(tmp_path, monkeypatch, 10) |
| 42 | + |
| 43 | + long_message_file = tmp_path / "long_message.txt" |
| 44 | + long_message_file.write_text("feat: this is definitely too long", encoding="utf-8") |
| 45 | + |
| 46 | + with pytest.raises(CommitMessageLengthExceededError): |
| 47 | + util.run_cli("check", "--commit-msg-file", str(long_message_file)) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.usefixtures("tmp_commitizen_project") |
| 51 | +def test_cli_commit_reads_message_length_limit_from_pyproject( |
| 52 | + util: UtilFixture, |
| 53 | + monkeypatch: pytest.MonkeyPatch, |
| 54 | + mocker, |
| 55 | + tmp_path: Path, |
| 56 | +): |
| 57 | + _write_pyproject_with_message_length_limit(tmp_path, monkeypatch, 10) |
| 58 | + _mock_commit_prompt(mocker, subject="this is definitely too long") |
| 59 | + mocker.patch("commitizen.git.is_staging_clean", return_value=False) |
| 60 | + |
| 61 | + with pytest.raises(CommitMessageLengthExceededError): |
| 62 | + util.run_cli("commit", "--dry-run") |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.usefixtures("tmp_commitizen_project") |
| 66 | +def test_cli_check_cli_overrides_message_length_limit_from_pyproject( |
| 67 | + util: UtilFixture, monkeypatch: pytest.MonkeyPatch, tmp_path: Path |
| 68 | +): |
| 69 | + _write_pyproject_with_message_length_limit(tmp_path, monkeypatch, 10) |
| 70 | + |
| 71 | + util.run_cli("check", "-l", "0", "--message", "feat: this is definitely too long") |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.usefixtures("tmp_commitizen_project") |
| 75 | +def test_cli_commit_cli_overrides_message_length_limit_from_pyproject( |
| 76 | + util: UtilFixture, |
| 77 | + monkeypatch: pytest.MonkeyPatch, |
| 78 | + mocker, |
| 79 | + tmp_path: Path, |
| 80 | +): |
| 81 | + _write_pyproject_with_message_length_limit(tmp_path, monkeypatch, 10) |
| 82 | + _mock_commit_prompt(mocker, subject="this is definitely too long") |
| 83 | + mocker.patch("commitizen.git.is_staging_clean", return_value=False) |
| 84 | + |
| 85 | + with pytest.raises(DryRunExit): |
| 86 | + util.run_cli("commit", "--dry-run", "-l", "100") |
| 87 | + |
0 commit comments