Skip to content

Commit

Permalink
dev(hansbug): add better plugin mock argument
Browse files Browse the repository at this point in the history
  • Loading branch information
HansBug committed Sep 7, 2022
1 parent c270d4e commit 3061172
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion potc/config/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
__TITLE__ = "potc"

#: Version of this project.
__VERSION__ = "0.1.0"
__VERSION__ = "0.1.1"

#: Short description of the project, will be included in ``setup.py``.
__DESCRIPTION__ = 'Python object to code module.'
Expand Down
21 changes: 18 additions & 3 deletions potc/testing/plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from contextlib import contextmanager
from functools import wraps
from itertools import chain
from unittest.mock import patch, MagicMock

import pkg_resources
Expand Down Expand Up @@ -44,12 +45,13 @@ def _to_plugin(v) -> FakeEntryPoint:


@contextmanager
def mock_potc_plugins(*plugins):
def mock_potc_plugins(*plugins, clear=False):
"""
Overview:
Mock potc plugins for unittest.
:param plugins: String of plugin module, such as ``potc_dict.plugin``, which can be auto-imported.
:param clear: Only use the mocked plugins. Default is ``False``, which means the installed plugins will be kept.
Examples::
>>> from potc import transobj
Expand All @@ -66,13 +68,26 @@ def mock_potc_plugins(*plugins):
>>> print(transobj({'a': 1, 'b': [3, 'dfgk']})) # again
dict(a=1, b=[3, 'dfgk'])
"""
from pkg_resources import iter_entry_points as _origin_iep

@wraps(pkg_resources.iter_entry_points)
def _new_iter_func(group, name=None):
_exist_names = set()

def _check_name(x) -> bool:
if (name is None or x.name == name) and name not in _exist_names:
_exist_names.add(name)
return True
else:
return False

if group == POTC_PLUGIN_GROUP:
yield from filter(lambda x: (name is None or x.name == name), map(_to_plugin, plugins))
mocked = map(_to_plugin, plugins)
if not clear:
mocked = chain(mocked, _origin_iep(group, name))
yield from filter(_check_name, mocked)
else:
yield from pkg_resources.iter_entry_points(group, name) # pragma: no cover
yield from _origin_iep(group, name) # pragma: no cover

with patch('pkg_resources.iter_entry_points', MagicMock(side_effect=_new_iter_func)):
yield
2 changes: 1 addition & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ def with_pythonpath(*path: str):

@pytest.fixture(autouse=True)
def no_plugin():
with mock_potc_plugins():
with mock_potc_plugins(clear=True):
yield

0 comments on commit 3061172

Please sign in to comment.