| 1 |
import sys |
| 2 |
from unittest.mock import Mock |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from doit.plugin import PluginEntry, PluginDict |
| 7 |
|
| 8 |
|
| 9 |
if sys.version_info < (3, 10): |
| 10 |
from importlib_metadata import EntryPoint |
| 11 |
else: |
| 12 |
from importlib.metadata import EntryPoint |
| 13 |
|
| 14 |
|
| 15 |
class TestPluginEntry(object): |
| 16 |
def test_repr(self): |
| 17 |
plugin = PluginEntry('category1', 'name1', 'mock:Mock') |
| 18 |
assert "PluginEntry('category1', 'name1', 'mock:Mock')" == repr(plugin) |
| 19 |
|
| 20 |
def test_get(self): |
| 21 |
plugin = PluginEntry('category1', 'name1', 'unittest.mock:Mock') |
| 22 |
got = plugin.get() |
| 23 |
assert got is Mock |
| 24 |
|
| 25 |
def test_load_error_module_not_found(self): |
| 26 |
plugin = PluginEntry('category1', 'name1', 'i_dont:exist') |
| 27 |
with pytest.raises(Exception) as exc_info: |
| 28 |
plugin.load() |
| 29 |
assert 'Plugin category1 module `i_dont`' in str(exc_info.value) |
| 30 |
|
| 31 |
def test_load_error_obj_not_found(self): |
| 32 |
plugin = PluginEntry('category1', 'name1', |
| 33 |
'unittest.mock:i_dont_exist') |
| 34 |
with pytest.raises(Exception) as exc_info: |
| 35 |
plugin.load() |
| 36 |
assert ('Plugin category1:name1 module `unittest.mock`' in |
| 37 |
str(exc_info.value)) |
| 38 |
assert 'i_dont_exist' in str(exc_info.value) |
| 39 |
|
| 40 |
|
| 41 |
class TestPluginDict(object): |
| 42 |
|
| 43 |
@pytest.fixture |
| 44 |
def plugins(self): |
| 45 |
plugins = PluginDict() |
| 46 |
config_dict = {'name1': 'pytest:raises', |
| 47 |
'name2': 'unittest.mock:Mock'} |
| 48 |
plugins.add_plugins({'category1': config_dict}, 'category1') |
| 49 |
return plugins |
| 50 |
|
| 51 |
def test_add_plugins_from_dict(self, plugins): |
| 52 |
assert len(plugins) == 2 |
| 53 |
name1 = plugins['name1'] |
| 54 |
assert isinstance(name1, PluginEntry) |
| 55 |
assert name1.category == 'category1' |
| 56 |
assert name1.name == 'name1' |
| 57 |
assert name1.location == 'pytest:raises' |
| 58 |
|
| 59 |
def test_add_plugins_from_pkg_resources(self, monkeypatch): |
| 60 |
|
| 61 |
from doit import plugin |
| 62 |
def fake_entries(group): |
| 63 |
yield EntryPoint(name='name1', value='pytest:raises', group=group) |
| 64 |
monkeypatch.setattr(plugin, 'entry_points_impl', lambda: fake_entries) |
| 65 |
|
| 66 |
plugins = PluginDict() |
| 67 |
plugins.add_plugins({}, 'category2') |
| 68 |
name1 = plugins['name1'] |
| 69 |
assert isinstance(name1, PluginEntry) |
| 70 |
assert name1.category == 'category2' |
| 71 |
assert name1.name == 'name1' |
| 72 |
assert name1.location == 'pytest:raises' |
| 73 |
|
| 74 |
def test_get_plugin_actual_plugin(self, plugins): |
| 75 |
assert plugins.get_plugin('name2') is Mock |
| 76 |
|
| 77 |
def test_get_plugin_not_a_plugin(self, plugins): |
| 78 |
my_val = 4 |
| 79 |
plugins['builtin-item'] = my_val |
| 80 |
assert plugins.get_plugin('builtin-item') is my_val |
| 81 |
|
| 82 |
def test_to_dict(self, plugins): |
| 83 |
expected = {'name1': pytest.raises, |
| 84 |
'name2': Mock} |
| 85 |
assert plugins.to_dict() == expected |