| 1 |
import re |
| 2 |
import sys |
| 3 |
from importlib import metadata |
| 4 |
from textwrap import dedent |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from commitizen import BaseCommitizen, defaults, factory, git |
| 9 |
from commitizen.config import BaseConfig |
| 10 |
from commitizen.cz import discover_plugins |
| 11 |
from commitizen.cz.conventional_commits import ConventionalCommitsCz |
| 12 |
from commitizen.cz.customize import CustomizeCommitsCz |
| 13 |
from commitizen.cz.jira import JiraSmartCz |
| 14 |
from commitizen.exceptions import NoCommitizenFoundException |
| 15 |
|
| 16 |
|
| 17 |
class Plugin: |
| 18 |
pass |
| 19 |
|
| 20 |
|
| 21 |
class OtherPlugin: |
| 22 |
pass |
| 23 |
|
| 24 |
|
| 25 |
def test_factory(): |
| 26 |
config = BaseConfig() |
| 27 |
config.settings.update({"name": defaults.DEFAULT_SETTINGS["name"]}) |
| 28 |
r = factory.committer_factory(config) |
| 29 |
assert isinstance(r, BaseCommitizen) |
| 30 |
|
| 31 |
|
| 32 |
def test_default_commit_filters_keep_all_commits(config: BaseConfig): |
| 33 |
"""Default filtering hooks preserve the original commit list.""" |
| 34 |
cz = ConventionalCommitsCz(config) |
| 35 |
commits = [git.GitCommit(rev="1", title="feat: add filtering")] |
| 36 |
|
| 37 |
assert cz.filter_commits(commits) is commits |
| 38 |
assert cz.filter_commits_before_bump(commits) is commits |
| 39 |
assert cz.filter_commits_before_changelog(commits) is commits |
| 40 |
|
| 41 |
|
| 42 |
def test_operation_commit_filters_delegate_to_shared_filter(config: BaseConfig, mocker): |
| 43 |
"""Operation-specific hooks delegate to a shared custom filter.""" |
| 44 |
cz = ConventionalCommitsCz(config) |
| 45 |
commits = [ |
| 46 |
git.GitCommit(rev="1", title="feat: app a"), |
| 47 |
git.GitCommit(rev="2", title="feat: app b"), |
| 48 |
] |
| 49 |
filtered_commits = commits[:1] |
| 50 |
filter_commits = mocker.patch.object( |
| 51 |
cz, "filter_commits", return_value=filtered_commits |
| 52 |
) |
| 53 |
|
| 54 |
assert cz.filter_commits_before_bump(commits) is filtered_commits |
| 55 |
assert cz.filter_commits_before_changelog(commits) is filtered_commits |
| 56 |
assert filter_commits.call_count == 2 |
| 57 |
filter_commits.assert_any_call(commits) |
| 58 |
|
| 59 |
|
| 60 |
def test_factory_fails(): |
| 61 |
config = BaseConfig() |
| 62 |
config.settings.update({"name": "Nothing"}) |
| 63 |
with pytest.raises( |
| 64 |
NoCommitizenFoundException, |
| 65 |
match=re.escape("The committer has not been found in the system."), |
| 66 |
): |
| 67 |
factory.committer_factory(config) |
| 68 |
|
| 69 |
|
| 70 |
def test_discover_plugins(tmp_path): |
| 71 |
legacy_plugin_folder = tmp_path / "cz_legacy" |
| 72 |
legacy_plugin_folder.mkdir() |
| 73 |
init_file = legacy_plugin_folder / "__init__.py" |
| 74 |
init_file.write_text( |
| 75 |
dedent( |
| 76 |
"""\ |
| 77 |
class Plugin: pass |
| 78 |
|
| 79 |
discover_this = Plugin |
| 80 |
""" |
| 81 |
) |
| 82 |
) |
| 83 |
|
| 84 |
sys.path.append(tmp_path.as_posix()) |
| 85 |
with pytest.warns( |
| 86 |
UserWarning, |
| 87 |
match="Legacy plugin 'cz_legacy' has been ignored: please expose it the 'commitizen.plugin' entrypoint", |
| 88 |
): |
| 89 |
discovered_plugins = discover_plugins([tmp_path.as_posix()]) |
| 90 |
sys.path.pop() |
| 91 |
|
| 92 |
assert "cz_legacy" not in discovered_plugins |
| 93 |
|
| 94 |
|
| 95 |
def test_discover_external_plugin(mocker): |
| 96 |
ep_plugin = metadata.EntryPoint( |
| 97 |
"test", "tests.test_factory:Plugin", "commitizen.plugin" |
| 98 |
) |
| 99 |
ep_other_plugin = metadata.EntryPoint( |
| 100 |
"not-selected", "tests.test_factory::OtherPlugin", "commitizen.not_a_plugin" |
| 101 |
) |
| 102 |
eps = [ep_plugin, ep_other_plugin] |
| 103 |
|
| 104 |
def mock_entrypoints(**kwargs): |
| 105 |
group = kwargs.get("group") |
| 106 |
return metadata.EntryPoints(ep for ep in eps if ep.group == group) |
| 107 |
|
| 108 |
mocker.patch.object(metadata, "entry_points", side_effect=mock_entrypoints) |
| 109 |
|
| 110 |
assert discover_plugins() == {"test": Plugin} |
| 111 |
|
| 112 |
|
| 113 |
def test_discover_internal_plugins(): |
| 114 |
expected = { |
| 115 |
"cz_conventional_commits": ConventionalCommitsCz, |
| 116 |
"cz_jira": JiraSmartCz, |
| 117 |
"cz_customize": CustomizeCommitsCz, |
| 118 |
} |
| 119 |
|
| 120 |
discovered = discover_plugins() |
| 121 |
|
| 122 |
assert set(expected.items()).issubset(set(discovered.items())) |