Debian Codegraph @

tests/test_factory.py

raw
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
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_factory_fails():
33 config = BaseConfig()
34 config.settings.update({"name": "Nothing"})
35 with pytest.raises(
36 NoCommitizenFoundException,
37 match=re.escape("The committer has not been found in the system."),
38 ):
39 factory.committer_factory(config)
40
41
42 def test_discover_plugins(tmp_path):
43 legacy_plugin_folder = tmp_path / "cz_legacy"
44 legacy_plugin_folder.mkdir()
45 init_file = legacy_plugin_folder / "__init__.py"
46 init_file.write_text(
47 dedent(
48 """\
49 class Plugin: pass
50
51 discover_this = Plugin
52 """
53 )
54 )
55
56 sys.path.append(tmp_path.as_posix())
57 with pytest.warns(
58 UserWarning,
59 match="Legacy plugin 'cz_legacy' has been ignored: please expose it the 'commitizen.plugin' entrypoint",
60 ):
61 discovered_plugins = discover_plugins([tmp_path.as_posix()])
62 sys.path.pop()
63
64 assert "cz_legacy" not in discovered_plugins
65
66
67 def test_discover_external_plugin(mocker):
68 ep_plugin = metadata.EntryPoint(
69 "test", "tests.test_factory:Plugin", "commitizen.plugin"
70 )
71 ep_other_plugin = metadata.EntryPoint(
72 "not-selected", "tests.test_factory::OtherPlugin", "commitizen.not_a_plugin"
73 )
74 eps = [ep_plugin, ep_other_plugin]
75
76 def mock_entrypoints(**kwargs):
77 group = kwargs.get("group")
78 return metadata.EntryPoints(ep for ep in eps if ep.group == group)
79
80 mocker.patch.object(metadata, "entry_points", side_effect=mock_entrypoints)
81
82 assert discover_plugins() == {"test": Plugin}
83
84
85 def test_discover_internal_plugins():
86 expected = {
87 "cz_conventional_commits": ConventionalCommitsCz,
88 "cz_jira": JiraSmartCz,
89 "cz_customize": CustomizeCommitsCz,
90 }
91
92 discovered = discover_plugins()
93
94 assert set(expected.items()).issubset(set(discovered.items()))