-
-
Notifications
You must be signed in to change notification settings - Fork 305
/
test_plugin_type_manager.py
197 lines (152 loc) · 6.9 KB
/
test_plugin_type_manager.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""Tests for flake8.plugins.manager.PluginTypeManager."""
import mock
import pytest
from flake8 import exceptions
from flake8.plugins import manager
TEST_NAMESPACE = "testing.plugin-type-manager"
def create_plugin_mock(raise_exception=False):
"""Create an auto-spec'd mock of a flake8 Plugin."""
plugin = mock.create_autospec(manager.Plugin, instance=True)
if raise_exception:
plugin.load_plugin.side_effect = exceptions.FailedToLoadPlugin(
plugin_name='T101',
exception=ValueError('Test failure'),
)
return plugin
def create_mapping_manager_mock(plugins):
"""Create a mock for the PluginManager."""
# Have a function that will actually call the method underneath
def fake_map(func):
for plugin in plugins:
yield func(plugin)
# Mock out the PluginManager instance
manager_mock = mock.Mock(spec=['map'])
# Replace the map method
manager_mock.map = fake_map
return manager_mock
def create_manager_with_plugins(plugins):
"""Create a fake PluginManager with a plugins dictionary."""
manager_mock = mock.create_autospec(manager.PluginManager)
manager_mock.plugins = plugins
return manager_mock
class FakeTestType(manager.PluginTypeManager):
"""Fake PluginTypeManager."""
namespace = TEST_NAMESPACE
@mock.patch('flake8.plugins.manager.PluginManager')
def test_instantiates_a_manager(PluginManager): # noqa: N803
"""Verify we create a PluginManager on instantiation."""
FakeTestType()
PluginManager.assert_called_once_with(TEST_NAMESPACE, local_plugins=None)
@mock.patch('flake8.plugins.manager.PluginManager')
def test_proxies_names_to_manager(PluginManager): # noqa: N803
"""Verify we proxy the names attribute."""
PluginManager.return_value = mock.Mock(names=[
'T100', 'T200', 'T300'
])
type_mgr = FakeTestType()
assert type_mgr.names == ['T100', 'T200', 'T300']
@mock.patch('flake8.plugins.manager.PluginManager')
def test_proxies_plugins_to_manager(PluginManager): # noqa: N803
"""Verify we proxy the plugins attribute."""
PluginManager.return_value = mock.Mock(plugins=[
'T100', 'T200', 'T300'
])
type_mgr = FakeTestType()
assert type_mgr.plugins == ['T100', 'T200', 'T300']
def test_generate_call_function():
"""Verify the function we generate."""
optmanager = object()
plugin = mock.Mock(method_name=lambda x: x)
func = manager.PluginTypeManager._generate_call_function(
'method_name', optmanager,
)
assert callable(func)
assert func(plugin) is optmanager
@mock.patch('flake8.plugins.manager.PluginManager')
def test_load_plugins(PluginManager): # noqa: N803
"""Verify load plugins loads *every* plugin."""
# Create a bunch of fake plugins
plugins = [create_plugin_mock(), create_plugin_mock(),
create_plugin_mock(), create_plugin_mock(),
create_plugin_mock(), create_plugin_mock(),
create_plugin_mock(), create_plugin_mock()]
# Return our PluginManager mock
PluginManager.return_value = create_mapping_manager_mock(plugins)
type_mgr = FakeTestType()
# Load the tests (do what we're actually testing)
assert len(type_mgr.load_plugins()) == 8
# Assert that our closure does what we think it does
for plugin in plugins:
plugin.load_plugin.assert_called_once_with()
assert type_mgr.plugins_loaded is True
@mock.patch('flake8.plugins.manager.PluginManager')
def test_load_plugins_fails(PluginManager): # noqa: N803
"""Verify load plugins bubbles up exceptions."""
plugins = [create_plugin_mock(), create_plugin_mock(True),
create_plugin_mock(), create_plugin_mock(),
create_plugin_mock(), create_plugin_mock(),
create_plugin_mock(), create_plugin_mock()]
# Return our PluginManager mock
PluginManager.return_value = create_mapping_manager_mock(plugins)
type_mgr = FakeTestType()
with pytest.raises(exceptions.FailedToLoadPlugin):
type_mgr.load_plugins()
# Assert we didn't finish loading plugins
assert type_mgr.plugins_loaded is False
# Assert the first two plugins had their load_plugin method called
plugins[0].load_plugin.assert_called_once_with()
plugins[1].load_plugin.assert_called_once_with()
# Assert the rest of the plugins were not loaded
for plugin in plugins[2:]:
assert plugin.load_plugin.called is False
@mock.patch('flake8.plugins.manager.PluginManager')
def test_register_options(PluginManager): # noqa: N803
"""Test that we map over every plugin to register options."""
plugins = [create_plugin_mock(), create_plugin_mock(),
create_plugin_mock(), create_plugin_mock(),
create_plugin_mock(), create_plugin_mock(),
create_plugin_mock(), create_plugin_mock()]
# Return our PluginManager mock
PluginManager.return_value = create_mapping_manager_mock(plugins)
optmanager = object()
type_mgr = FakeTestType()
type_mgr.register_options(optmanager)
for plugin in plugins:
plugin.register_options.assert_called_with(optmanager)
@mock.patch('flake8.plugins.manager.PluginManager')
def test_provide_options(PluginManager): # noqa: N803
"""Test that we map over every plugin to provide parsed options."""
plugins = [create_plugin_mock(), create_plugin_mock(),
create_plugin_mock(), create_plugin_mock(),
create_plugin_mock(), create_plugin_mock(),
create_plugin_mock(), create_plugin_mock()]
# Return our PluginManager mock
PluginManager.return_value = create_mapping_manager_mock(plugins)
optmanager = object()
options = object()
type_mgr = FakeTestType()
type_mgr.provide_options(optmanager, options, [])
for plugin in plugins:
plugin.provide_options.assert_called_with(optmanager,
options,
[])
@mock.patch('flake8.plugins.manager.PluginManager')
def test_proxy_contains_to_managers_plugins_dict(PluginManager): # noqa: N803
"""Verify that we proxy __contains__ to the manager's dictionary."""
plugins = {'T10%i' % i: create_plugin_mock() for i in range(8)}
# Return our PluginManager mock
PluginManager.return_value = create_manager_with_plugins(plugins)
type_mgr = FakeTestType()
for i in range(8):
key = 'T10%i' % i
assert key in type_mgr
@mock.patch('flake8.plugins.manager.PluginManager')
def test_proxies_getitem_to_managers_plugins_dict(PluginManager): # noqa: N803
"""Verify that we can use the PluginTypeManager like a dictionary."""
plugins = {'T10%i' % i: create_plugin_mock() for i in range(8)}
# Return our PluginManager mock
PluginManager.return_value = create_manager_with_plugins(plugins)
type_mgr = FakeTestType()
for i in range(8):
key = 'T10%i' % i
assert type_mgr[key] is plugins[key]