|
| 1 | +"""Tests for executable custom-plugin examples published in the docs.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import importlib.util |
| 6 | +from pathlib import Path |
| 7 | +from typing import TYPE_CHECKING, cast |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from commitizen import git |
| 12 | +from commitizen.cz import registry |
| 13 | +from commitizen.exceptions import NoneIncrementExit |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from pytest_mock import MockFixture |
| 17 | + |
| 18 | + from commitizen.cz.base import BaseCommitizen |
| 19 | + from tests.utils import UtilFixture |
| 20 | + |
| 21 | + |
| 22 | +DOCUMENTED_PLUGIN_PATH = ( |
| 23 | + Path(__file__).resolve().parents[2] / "docs" / "examples" / "cz_docs_only.py" |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +def _load_documented_plugin() -> type[BaseCommitizen]: |
| 28 | + """Load the custom plugin example from the file embedded in the docs.""" |
| 29 | + spec = importlib.util.spec_from_file_location( |
| 30 | + "tests_documented_cz_docs_only", DOCUMENTED_PLUGIN_PATH |
| 31 | + ) |
| 32 | + assert spec is not None |
| 33 | + assert spec.loader is not None |
| 34 | + |
| 35 | + module = importlib.util.module_from_spec(spec) |
| 36 | + spec.loader.exec_module(module) |
| 37 | + |
| 38 | + return cast("type[BaseCommitizen]", getattr(module, "DocsOnlyPatchCommitizen")) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture |
| 42 | +def documented_plugin_name(mocker: MockFixture) -> str: |
| 43 | + """Expose the documented example through Commitizen's plugin registry.""" |
| 44 | + plugin_name = "cz_docs_only" |
| 45 | + plugin_class = _load_documented_plugin() |
| 46 | + mocker.patch.dict("commitizen.cz.registry", {**registry, plugin_name: plugin_class}) |
| 47 | + return plugin_name |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("major_version_zero", [False, True]) |
| 51 | +@pytest.mark.parametrize( |
| 52 | + "commit_message", |
| 53 | + ["fix: ship executable docs example", "fix(api): ship executable docs example"], |
| 54 | +) |
| 55 | +@pytest.mark.usefixtures("tmp_commitizen_project") |
| 56 | +def test_documented_python_plugin_fix_commit_bumps_patch( |
| 57 | + util: UtilFixture, |
| 58 | + documented_plugin_name: str, |
| 59 | + major_version_zero: bool, |
| 60 | + commit_message: str, |
| 61 | +) -> None: |
| 62 | + """The documented example bumps a fix commit as a patch release.""" |
| 63 | + util.create_file_and_commit(commit_message) |
| 64 | + |
| 65 | + args = ["--name", documented_plugin_name, "bump", "--yes"] |
| 66 | + if major_version_zero: |
| 67 | + args.append("--major-version-zero") |
| 68 | + util.run_cli(*args) |
| 69 | + |
| 70 | + assert git.tag_exist("0.1.1") is True |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize("major_version_zero", [False, True]) |
| 74 | +@pytest.mark.parametrize( |
| 75 | + "commit_message", |
| 76 | + ["docs: expand plugin guide", "docs(api): expand plugin guide"], |
| 77 | +) |
| 78 | +@pytest.mark.usefixtures("tmp_commitizen_project") |
| 79 | +def test_documented_python_plugin_docs_commit_does_not_bump( |
| 80 | + util: UtilFixture, |
| 81 | + documented_plugin_name: str, |
| 82 | + major_version_zero: bool, |
| 83 | + commit_message: str, |
| 84 | +) -> None: |
| 85 | + """The documented example treats docs commits as a no-bump match.""" |
| 86 | + first_bump_args = ["--name", documented_plugin_name, "bump", "--yes"] |
| 87 | + if major_version_zero: |
| 88 | + first_bump_args.append("--major-version-zero") |
| 89 | + |
| 90 | + util.create_file_and_commit("fix: seed release") |
| 91 | + util.run_cli(*first_bump_args) |
| 92 | + util.create_file_and_commit(commit_message) |
| 93 | + |
| 94 | + with pytest.raises(NoneIncrementExit): |
| 95 | + util.run_cli(*first_bump_args) |
| 96 | + |
| 97 | + assert git.tag_exist("0.1.2") is False |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize( |
| 101 | + "commit_message", |
| 102 | + [ |
| 103 | + "fix!: drop old API", |
| 104 | + "fixup! rebase cleanup", |
| 105 | + "fixture: rename helper", |
| 106 | + ], |
| 107 | +) |
| 108 | +@pytest.mark.usefixtures("tmp_commitizen_project") |
| 109 | +def test_documented_python_plugin_ignores_nonmatching_fix_prefixes( |
| 110 | + util: UtilFixture, documented_plugin_name: str, commit_message: str |
| 111 | +) -> None: |
| 112 | + """Only plain fix commits with an optional scope participate in bumping.""" |
| 113 | + util.create_file_and_commit("fix: seed release") |
| 114 | + util.run_cli("--name", documented_plugin_name, "bump", "--yes") |
| 115 | + util.create_file_and_commit(commit_message) |
| 116 | + |
| 117 | + with pytest.raises(NoneIncrementExit): |
| 118 | + util.run_cli("--name", documented_plugin_name, "bump", "--yes") |
| 119 | + |
| 120 | + assert git.tag_exist("0.1.2") is False |
0 commit comments