Repository navigation
Expand file tree
/
Copy pathtest_plugin_loader.py
More file actions
105 lines (82 loc) · 3.53 KB
/
Copy pathtest_plugin_loader.py
File metadata and controls
105 lines (82 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from unittest.mock import patch
@patch("pkgutil.get_importer")
def test_load_from_pyinstaller(pkgutil_get_importer):
sample_toc = set(["pyexcel_io", "pyexcel_xls", "blah", "test.dot.module"])
pkgutil_get_importer.return_value.toc = sample_toc
from lml.loader import scan_from_pyinstaller
module_names = scan_from_pyinstaller("pyexcel_", "path")
expected = ["pyexcel_io", "pyexcel_xls"]
assert sorted(list(module_names)) == sorted(expected)
@patch("pkgutil.get_importer")
def test_load_from_pyinstaller_with_regex(pkgutil_get_importer):
sample_toc = set(["pyexcel_io", "pyexcel_xls", "blah"])
pkgutil_get_importer.return_value.toc = sample_toc
from lml.loader import scan_from_pyinstaller
module_names = scan_from_pyinstaller("^.+cel_.+$", "path")
expected = ["pyexcel_io", "pyexcel_xls"]
assert sorted(list(module_names)) == sorted(expected)
@patch("pkgutil.get_importer")
@patch("pkgutil.iter_modules")
def test_load_plugins(pkgutil_iter_modules, pkgutil_get_importer):
test_module_name = "pyexcel_test"
sample_toc = set(["pyexcel_io"])
pkgutil_get_importer.return_value.toc = sample_toc
pkgutil_iter_modules.return_value = [("not used", test_module_name, True)]
from lml.loader import scan_plugins
scan_plugins("pyexcel_", ".", ["pyexcel_io"])
from lml.plugin import CACHED_PLUGIN_INFO
info = CACHED_PLUGIN_INFO["test_io"][0]
assert info.plugin_type == "test_io"
assert info.absolute_import_path == "pyexcel_test.x"
@patch("pkgutil.get_importer")
@patch("pkgutil.iter_modules")
def test_load_plugins_without_pyinstaller(
pkgutil_iter_modules, pkgutil_get_importer
):
test_module_name = "pyexcel_test"
sample_toc = set()
pkgutil_get_importer.return_value.toc = sample_toc
# mock iter modules
pkgutil_iter_modules.return_value = [("not used", test_module_name, True)]
from lml.loader import scan_plugins
scan_plugins("pyexcel_", ".", ["pyexcel_io"])
from lml.plugin import CACHED_PLUGIN_INFO
info = CACHED_PLUGIN_INFO["test_io"][0]
assert info.plugin_type == "test_io"
assert info.absolute_import_path == "pyexcel_test.x"
@patch("pkgutil.get_importer")
@patch("pkgutil.iter_modules")
@patch("lml.plugin._load_me_later")
def test_load_plugins_without_any_plugins(
mocked_load_me_later, pkgutil_iter_modules, pkgutil_get_importer
):
sample_toc = set()
pkgutil_get_importer.return_value.toc = sample_toc
pkgutil_iter_modules.return_value = []
from lml.loader import scan_plugins
scan_plugins("pyexcel_", ".", ["pyexcel_io"])
assert not mocked_load_me_later.called
@patch("pkgutil.get_importer")
@patch("pkgutil.iter_modules")
@patch("lml.plugin._load_me_later")
def test_load_plugins_without_black_list(
mocked_load_me_later, pkgutil_iter_modules, pkgutil_get_importer
):
sample_toc = set()
pkgutil_get_importer.return_value.toc = sample_toc
pkgutil_iter_modules.return_value = []
from lml.loader import scan_plugins
scan_plugins("pyexcel_", ".")
assert not mocked_load_me_later.called
@patch("pkgutil.get_importer")
@patch("pkgutil.iter_modules")
@patch("lml.plugin._load_me_later")
def test_load_plugins_import_error(
mocked_load_me_later, pkgutil_iter_modules, pkgutil_get_importer
):
sample_toc = set(["test_non_existent_module"])
pkgutil_get_importer.return_value.toc = sample_toc
pkgutil_iter_modules.return_value = [("not used", "pyexcel_xls", False)]
from lml.loader import scan_plugins
scan_plugins("test_", ".", ["pyexcel_io"])
assert not mocked_load_me_later.called