1
1
import sys
2
+ from textwrap import dedent
2
3
3
4
import pytest
4
5
5
6
from commitizen import BaseCommitizen , defaults , factory
7
+ from commitizen ._compat import metadata
6
8
from commitizen .config import BaseConfig
7
9
from commitizen .cz import discover_plugins
10
+ from commitizen .cz .conventional_commits import ConventionalCommitsCz
11
+ from commitizen .cz .customize import CustomizeCommitsCz
12
+ from commitizen .cz .jira import JiraSmartCz
8
13
from commitizen .exceptions import NoCommitizenFoundException
9
14
10
15
@@ -24,17 +29,19 @@ def test_factory_fails():
24
29
assert "The committer has not been found in the system." in str (excinfo )
25
30
26
31
27
- @pytest .mark .parametrize (
28
- "module_content, plugin_name, expected_plugins" ,
29
- [
30
- ("" , "cz_no_plugin" , {}),
31
- ],
32
- )
33
- def test_discover_plugins (module_content , plugin_name , expected_plugins , tmp_path ):
34
- no_plugin_folder = tmp_path / plugin_name
35
- no_plugin_folder .mkdir ()
36
- init_file = no_plugin_folder / "__init__.py"
37
- init_file .write_text (module_content )
32
+ def test_discover_plugins (tmp_path ):
33
+ legacy_plugin_folder = tmp_path / "cz_legacy"
34
+ legacy_plugin_folder .mkdir ()
35
+ init_file = legacy_plugin_folder / "__init__.py"
36
+ init_file .write_text (
37
+ dedent (
38
+ """\
39
+ class Plugin: pass
40
+
41
+ discover_this = Plugin
42
+ """
43
+ )
44
+ )
38
45
39
46
sys .path .append (tmp_path .as_posix ())
40
47
with pytest .warns (UserWarning ) as record :
@@ -43,6 +50,43 @@ def test_discover_plugins(module_content, plugin_name, expected_plugins, tmp_pat
43
50
44
51
assert (
45
52
record [0 ].message .args [0 ]
46
- == f"module '{ plugin_name } ' has no attribute 'discover_this'"
53
+ == "Legacy plugin 'cz_legacy' has been ignored: please expose it the 'commitizen.plugin' entrypoint"
54
+ )
55
+ assert "cz_legacy" not in discovered_plugins
56
+
57
+
58
+ def test_discover_external_plugin (mocker ):
59
+ class Plugin :
60
+ pass
61
+
62
+ class OtherPlugin :
63
+ pass
64
+
65
+ ep_plugin = metadata .EntryPoint ("test" , "some:Plugin" , "commitizen.plugin" )
66
+ ep_other_plugin = metadata .EntryPoint (
67
+ "not-selected" , "some:OtherPlugin" , "commitizen.not_a_plugin"
47
68
)
48
- assert expected_plugins == discovered_plugins
69
+ eps = [ep_plugin , ep_other_plugin ]
70
+
71
+ mocker .patch .object (ep_plugin , "load" , return_value = Plugin )
72
+ mocker .patch .object (ep_other_plugin , "load" , return_value = OtherPlugin )
73
+
74
+ def mock_entrypoints (** kwargs ):
75
+ group = kwargs .get ("group" )
76
+ return metadata .EntryPoints (ep for ep in eps if ep .group == group )
77
+
78
+ mocker .patch .object (metadata , "entry_points" , side_effect = mock_entrypoints )
79
+
80
+ assert discover_plugins () == {"test" : Plugin }
81
+
82
+
83
+ def test_discover_internal_plugins ():
84
+ expected = {
85
+ "cz_conventional_commits" : ConventionalCommitsCz ,
86
+ "cz_jira" : JiraSmartCz ,
87
+ "cz_customize" : CustomizeCommitsCz ,
88
+ }
89
+
90
+ discovered = discover_plugins ()
91
+
92
+ assert set (expected .items ()).issubset (set (discovered .items ()))
0 commit comments