Skip to content

Commit fe5e089

Browse files
committed
test: Checking that warnings are risen
1 parent 0498938 commit fe5e089

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

commitizen/cz/__init__.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
import importlib
2-
import logging
32
import pkgutil
4-
from typing import Dict, Type
3+
import warnings
4+
from pathlib import Path
5+
from typing import Dict, Iterable, Type
56

67
from commitizen.cz.base import BaseCommitizen
78
from commitizen.cz.conventional_commits import ConventionalCommitsCz
89
from commitizen.cz.customize import CustomizeCommitsCz
910
from commitizen.cz.jira import JiraSmartCz
1011

11-
logger = logging.getLogger(__name__)
1212

13+
def discover_plugins(path: Iterable[Path] = None) -> Dict[str, Type[BaseCommitizen]]:
14+
"""Discover commitizen plugins on the path
1315
14-
def discover_plugins():
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+
"""
1523
plugins = {}
16-
for finder, name, ispkg in pkgutil.iter_modules():
24+
for finder, name, ispkg in pkgutil.iter_modules(path):
1725
try:
1826
if name.startswith("cz_"):
1927
plugins[name] = importlib.import_module(name).discover_this
2028
except AttributeError as e:
21-
logger.warning(e.args[0])
29+
warnings.warn(UserWarning(e.args[0]))
2230
continue
2331
return plugins
2432

tests/test_factory.py

Lines changed: 27 additions & 0 deletions
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)