mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-10 14:54:17 +00:00
Write more PluginTypeManager tests
This commit is contained in:
parent
3b64ff2a1f
commit
6e8e2b9669
1 changed files with 79 additions and 1 deletions
|
|
@ -1,12 +1,26 @@
|
||||||
"""Tests for flake8.plugins.manager.PluginTypeManager."""
|
"""Tests for flake8.plugins.manager.PluginTypeManager."""
|
||||||
import collections
|
import collections
|
||||||
import mock
|
|
||||||
|
|
||||||
|
import mock
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from flake8 import exceptions
|
||||||
from flake8.plugins import manager
|
from flake8.plugins import manager
|
||||||
|
|
||||||
TEST_NAMESPACE = "testing.plugin-type-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=mock.Mock(name='T101'),
|
||||||
|
exception=ValueError('Test failure'),
|
||||||
|
)
|
||||||
|
return plugin
|
||||||
|
|
||||||
|
|
||||||
class TestType(manager.PluginTypeManager):
|
class TestType(manager.PluginTypeManager):
|
||||||
"""Fake PluginTypeManager."""
|
"""Fake PluginTypeManager."""
|
||||||
|
|
||||||
|
|
@ -53,3 +67,67 @@ def test_generate_call_function():
|
||||||
|
|
||||||
assert isinstance(func, collections.Callable)
|
assert isinstance(func, collections.Callable)
|
||||||
assert func(plugin) is optmanager
|
assert func(plugin) is optmanager
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch('flake8.plugins.manager.PluginManager')
|
||||||
|
def test_load_plugins(PluginManager):
|
||||||
|
"""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()]
|
||||||
|
|
||||||
|
# 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 our PluginManager mock
|
||||||
|
PluginManager.return_value = manager_mock
|
||||||
|
|
||||||
|
type_mgr = TestType()
|
||||||
|
# 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):
|
||||||
|
"""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()]
|
||||||
|
|
||||||
|
# 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 our PluginManager mock
|
||||||
|
PluginManager.return_value = manager_mock
|
||||||
|
|
||||||
|
type_mgr = TestType()
|
||||||
|
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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue