mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-10 14:54:17 +00:00
Merge pull request #1426 from mxr/fix-return
Remove usage of self.manager.map() in load_plugins()
This commit is contained in:
commit
bcb88c4c3e
2 changed files with 20 additions and 56 deletions
|
|
@ -415,14 +415,11 @@ class PluginTypeManager:
|
||||||
if self.plugins_loaded:
|
if self.plugins_loaded:
|
||||||
return
|
return
|
||||||
|
|
||||||
def load_plugin(plugin):
|
for plugin in self.plugins.values():
|
||||||
"""Call each plugin's load_plugin method."""
|
plugin.load_plugin()
|
||||||
return plugin.load_plugin()
|
|
||||||
|
|
||||||
plugins = list(self.manager.map(load_plugin))
|
|
||||||
# Do not set plugins_loaded if we run into an exception
|
# Do not set plugins_loaded if we run into an exception
|
||||||
self.plugins_loaded = True
|
self.plugins_loaded = True
|
||||||
return plugins
|
|
||||||
|
|
||||||
def register_plugin_versions(self, optmanager):
|
def register_plugin_versions(self, optmanager):
|
||||||
"""Register the plugins and their versions with the OptionManager."""
|
"""Register the plugins and their versions with the OptionManager."""
|
||||||
|
|
|
||||||
|
|
@ -24,13 +24,15 @@ def create_mapping_manager_mock(plugins):
|
||||||
"""Create a mock for the PluginManager."""
|
"""Create a mock for the PluginManager."""
|
||||||
# Have a function that will actually call the method underneath
|
# Have a function that will actually call the method underneath
|
||||||
def fake_map(func):
|
def fake_map(func):
|
||||||
for plugin in plugins:
|
for plugin in plugins.values():
|
||||||
yield func(plugin)
|
yield func(plugin)
|
||||||
|
|
||||||
# Mock out the PluginManager instance
|
# Mock out the PluginManager instance
|
||||||
manager_mock = mock.Mock(spec=["map"])
|
manager_mock = mock.Mock(spec=["map"])
|
||||||
# Replace the map method
|
# Replace the map method
|
||||||
manager_mock.map = fake_map
|
manager_mock.map = fake_map
|
||||||
|
# Store the plugins
|
||||||
|
manager_mock.plugins = plugins
|
||||||
return manager_mock
|
return manager_mock
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -83,24 +85,15 @@ def test_generate_call_function():
|
||||||
def test_load_plugins(PluginManager): # noqa: N803
|
def test_load_plugins(PluginManager): # noqa: N803
|
||||||
"""Verify load plugins loads *every* plugin."""
|
"""Verify load plugins loads *every* plugin."""
|
||||||
# Create a bunch of fake plugins
|
# Create a bunch of fake plugins
|
||||||
plugins = [
|
plugins = {"T10%i" % i: create_plugin_mock() for i in range(8)}
|
||||||
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
|
# Return our PluginManager mock
|
||||||
PluginManager.return_value = create_mapping_manager_mock(plugins)
|
PluginManager.return_value.plugins = plugins
|
||||||
|
|
||||||
type_mgr = FakeTestType()
|
type_mgr = FakeTestType()
|
||||||
# Load the tests (do what we're actually testing)
|
# Load the plugins (do what we're actually testing)
|
||||||
assert len(type_mgr.load_plugins()) == 8
|
type_mgr.load_plugins()
|
||||||
# Assert that our closure does what we think it does
|
# Assert that our closure does what we think it does
|
||||||
for plugin in plugins:
|
for plugin in plugins.values():
|
||||||
plugin.load_plugin.assert_called_once_with()
|
plugin.load_plugin.assert_called_once_with()
|
||||||
assert type_mgr.plugins_loaded is True
|
assert type_mgr.plugins_loaded is True
|
||||||
|
|
||||||
|
|
@ -108,18 +101,10 @@ def test_load_plugins(PluginManager): # noqa: N803
|
||||||
@mock.patch("flake8.plugins.manager.PluginManager")
|
@mock.patch("flake8.plugins.manager.PluginManager")
|
||||||
def test_load_plugins_fails(PluginManager): # noqa: N803
|
def test_load_plugins_fails(PluginManager): # noqa: N803
|
||||||
"""Verify load plugins bubbles up exceptions."""
|
"""Verify load plugins bubbles up exceptions."""
|
||||||
plugins = [
|
plugins_list = [create_plugin_mock(i == 1) for i in range(8)]
|
||||||
create_plugin_mock(),
|
plugins = {"T10%i" % i: plugin for i, plugin in enumerate(plugins_list)}
|
||||||
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
|
# Return our PluginManager mock
|
||||||
PluginManager.return_value = create_mapping_manager_mock(plugins)
|
PluginManager.return_value.plugins = plugins
|
||||||
|
|
||||||
type_mgr = FakeTestType()
|
type_mgr = FakeTestType()
|
||||||
with pytest.raises(exceptions.FailedToLoadPlugin):
|
with pytest.raises(exceptions.FailedToLoadPlugin):
|
||||||
|
|
@ -128,26 +113,17 @@ def test_load_plugins_fails(PluginManager): # noqa: N803
|
||||||
# Assert we didn't finish loading plugins
|
# Assert we didn't finish loading plugins
|
||||||
assert type_mgr.plugins_loaded is False
|
assert type_mgr.plugins_loaded is False
|
||||||
# Assert the first two plugins had their load_plugin method called
|
# Assert the first two plugins had their load_plugin method called
|
||||||
plugins[0].load_plugin.assert_called_once_with()
|
plugins_list[0].load_plugin.assert_called_once_with()
|
||||||
plugins[1].load_plugin.assert_called_once_with()
|
plugins_list[1].load_plugin.assert_called_once_with()
|
||||||
# Assert the rest of the plugins were not loaded
|
# Assert the rest of the plugins were not loaded
|
||||||
for plugin in plugins[2:]:
|
for plugin in plugins_list[2:]:
|
||||||
assert plugin.load_plugin.called is False
|
assert plugin.load_plugin.called is False
|
||||||
|
|
||||||
|
|
||||||
@mock.patch("flake8.plugins.manager.PluginManager")
|
@mock.patch("flake8.plugins.manager.PluginManager")
|
||||||
def test_register_options(PluginManager): # noqa: N803
|
def test_register_options(PluginManager): # noqa: N803
|
||||||
"""Test that we map over every plugin to register options."""
|
"""Test that we map over every plugin to register options."""
|
||||||
plugins = [
|
plugins = {"T10%i" % i: create_plugin_mock() for i in range(8)}
|
||||||
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
|
# Return our PluginManager mock
|
||||||
PluginManager.return_value = create_mapping_manager_mock(plugins)
|
PluginManager.return_value = create_mapping_manager_mock(plugins)
|
||||||
optmanager = object()
|
optmanager = object()
|
||||||
|
|
@ -155,23 +131,14 @@ def test_register_options(PluginManager): # noqa: N803
|
||||||
type_mgr = FakeTestType()
|
type_mgr = FakeTestType()
|
||||||
type_mgr.register_options(optmanager)
|
type_mgr.register_options(optmanager)
|
||||||
|
|
||||||
for plugin in plugins:
|
for plugin in plugins.values():
|
||||||
plugin.register_options.assert_called_with(optmanager)
|
plugin.register_options.assert_called_with(optmanager)
|
||||||
|
|
||||||
|
|
||||||
@mock.patch("flake8.plugins.manager.PluginManager")
|
@mock.patch("flake8.plugins.manager.PluginManager")
|
||||||
def test_provide_options(PluginManager): # noqa: N803
|
def test_provide_options(PluginManager): # noqa: N803
|
||||||
"""Test that we map over every plugin to provide parsed options."""
|
"""Test that we map over every plugin to provide parsed options."""
|
||||||
plugins = [
|
plugins = {"T10%i" % i: create_plugin_mock() for i in range(8)}
|
||||||
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
|
# Return our PluginManager mock
|
||||||
PluginManager.return_value = create_mapping_manager_mock(plugins)
|
PluginManager.return_value = create_mapping_manager_mock(plugins)
|
||||||
optmanager = object()
|
optmanager = object()
|
||||||
|
|
@ -180,7 +147,7 @@ def test_provide_options(PluginManager): # noqa: N803
|
||||||
type_mgr = FakeTestType()
|
type_mgr = FakeTestType()
|
||||||
type_mgr.provide_options(optmanager, options, [])
|
type_mgr.provide_options(optmanager, options, [])
|
||||||
|
|
||||||
for plugin in plugins:
|
for plugin in plugins.values():
|
||||||
plugin.provide_options.assert_called_with(optmanager, options, [])
|
plugin.provide_options.assert_called_with(optmanager, options, [])
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue