Skip to content

Commit 55841d6

Browse files
authored
Merge pull request #1 from mmartinortiz/fix-ignore-packages-that-are-not-plugins
fix: Ignore packages that are not plugins
2 parents 258f7c9 + fe5e089 commit 55841d6

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

commitizen/cz/__init__.py

+26-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
import importlib
22
import pkgutil
3-
from typing import Dict, Type
3+
import warnings
4+
from pathlib import Path
5+
from typing import Dict, Iterable, Type
46

57
from commitizen.cz.base import BaseCommitizen
68
from commitizen.cz.conventional_commits import ConventionalCommitsCz
79
from commitizen.cz.customize import CustomizeCommitsCz
810
from commitizen.cz.jira import JiraSmartCz
911

12+
13+
def discover_plugins(path: Iterable[Path] = None) -> Dict[str, Type[BaseCommitizen]]:
14+
"""Discover commitizen plugins on the path
15+
16+
Args:
17+
path (Path, optional): If provided, 'path' should be either None or a list of paths to look for
18+
modules in. If path is None, all top-level modules on sys.path.. Defaults to None.
19+
20+
Returns:
21+
Dict[str, Type[BaseCommitizen]]: Registry with found plugins
22+
"""
23+
plugins = {}
24+
for finder, name, ispkg in pkgutil.iter_modules(path):
25+
try:
26+
if name.startswith("cz_"):
27+
plugins[name] = importlib.import_module(name).discover_this
28+
except AttributeError as e:
29+
warnings.warn(UserWarning(e.args[0]))
30+
continue
31+
return plugins
32+
33+
1034
registry: Dict[str, Type[BaseCommitizen]] = {
1135
"cz_conventional_commits": ConventionalCommitsCz,
1236
"cz_jira": JiraSmartCz,
1337
"cz_customize": CustomizeCommitsCz,
1438
}
15-
plugins = {
16-
name: importlib.import_module(name).discover_this # type: ignore
17-
for finder, name, ispkg in pkgutil.iter_modules()
18-
if name.startswith("cz_")
19-
}
2039

21-
registry.update(plugins)
40+
registry.update(discover_plugins())

tests/test_factory.py

+27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import sys
2+
13
import pytest
24

35
from commitizen import BaseCommitizen, defaults, factory
46
from commitizen.config import BaseConfig
7+
from commitizen.cz import discover_plugins
58
from commitizen.exceptions import NoCommitizenFoundException
69

710

@@ -19,3 +22,27 @@ def test_factory_fails():
1922
factory.commiter_factory(config)
2023

2124
assert "The committer has not been found in the system." in str(excinfo)
25+
26+
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)
38+
39+
sys.path.append(tmp_path.as_posix())
40+
with pytest.warns(UserWarning) as record:
41+
discovered_plugins = discover_plugins([tmp_path])
42+
sys.path.pop()
43+
44+
assert (
45+
record[0].message.args[0]
46+
== f"module '{plugin_name}' has no attribute 'discover_this'"
47+
)
48+
assert expected_plugins == discovered_plugins

0 commit comments

Comments
 (0)