mirror of
https://github.com/PyCQA/flake8.git
synced 2026-03-30 02:46:52 +00:00
Move __contains__ and __getitem__ to proper class
This commit is contained in:
parent
91327c75e0
commit
0645ec3ef7
3 changed files with 44 additions and 37 deletions
|
|
@ -152,16 +152,6 @@ class PluginManager(object):
|
|||
self.names = []
|
||||
self._load_all_plugins()
|
||||
|
||||
def __contains__(self, name):
|
||||
"""Check if the entry-point name is in this plugin manager."""
|
||||
LOG.debug('Checking for "%s" in plugin manager.', name)
|
||||
return name in self.plugins
|
||||
|
||||
def __getitem__(self, name):
|
||||
"""Retrieve a plugin by its entry-point name."""
|
||||
LOG.debug('Retrieving plugin for "%s".', name)
|
||||
return self.plugins[name]
|
||||
|
||||
def _load_all_plugins(self):
|
||||
LOG.debug('Loading entry-points for "%s".', self.namespace)
|
||||
for entry_point in pkg_resources.iter_entry_points(self.namespace):
|
||||
|
|
@ -203,6 +193,16 @@ class PluginTypeManager(object):
|
|||
self.manager = PluginManager(self.namespace)
|
||||
self.plugins_loaded = False
|
||||
|
||||
def __contains__(self, name):
|
||||
"""Check if the entry-point name is in this plugin type manager."""
|
||||
LOG.debug('Checking for "%s" in plugin type manager.', name)
|
||||
return name in self.plugins
|
||||
|
||||
def __getitem__(self, name):
|
||||
"""Retrieve a plugin by its name."""
|
||||
LOG.debug('Retrieving plugin for "%s".', name)
|
||||
return self.plugins[name]
|
||||
|
||||
@property
|
||||
def names(self):
|
||||
"""Proxy attribute to underlying manager."""
|
||||
|
|
|
|||
|
|
@ -36,32 +36,6 @@ def test_calls_pkg_resources_creates_plugins_automaticaly(iter_entry_points):
|
|||
assert isinstance(plugin_mgr.plugins['T200'], manager.Plugin)
|
||||
|
||||
|
||||
@mock.patch('pkg_resources.iter_entry_points')
|
||||
def test_proxies_contains_to_plugins_dictionary(iter_entry_points):
|
||||
"""Verify that we can use the PluginManager like a dictionary."""
|
||||
iter_entry_points.return_value = [
|
||||
create_entry_point_mock('T100'),
|
||||
create_entry_point_mock('T200'),
|
||||
]
|
||||
plugin_mgr = manager.PluginManager(namespace='testing.pkg_resources')
|
||||
|
||||
assert 'T100' in plugin_mgr
|
||||
assert 'T200' in plugin_mgr
|
||||
|
||||
|
||||
@mock.patch('pkg_resources.iter_entry_points')
|
||||
def test_proxies_getitem_to_plugins_dictionary(iter_entry_points):
|
||||
"""Verify that we can use the PluginManager like a dictionary."""
|
||||
iter_entry_points.return_value = [
|
||||
create_entry_point_mock('T100'),
|
||||
create_entry_point_mock('T200'),
|
||||
]
|
||||
plugin_mgr = manager.PluginManager(namespace='testing.pkg_resources')
|
||||
|
||||
assert isinstance(plugin_mgr['T100'], manager.Plugin)
|
||||
assert isinstance(plugin_mgr['T200'], manager.Plugin)
|
||||
|
||||
|
||||
@mock.patch('pkg_resources.iter_entry_points')
|
||||
def test_handles_mapping_functions_across_plugins(iter_entry_points):
|
||||
"""Verify we can use the PluginManager call functions on all plugins."""
|
||||
|
|
@ -71,6 +45,6 @@ def test_handles_mapping_functions_across_plugins(iter_entry_points):
|
|||
]
|
||||
iter_entry_points.return_value = entry_point_mocks
|
||||
plugin_mgr = manager.PluginManager(namespace='testing.pkg_resources')
|
||||
plugins = [plugin_mgr[name] for name in plugin_mgr.names]
|
||||
plugins = [plugin_mgr.plugins[name] for name in plugin_mgr.names]
|
||||
|
||||
assert list(plugin_mgr.map(lambda x: x)) == plugins
|
||||
|
|
|
|||
|
|
@ -35,6 +35,13 @@ def create_mapping_manager_mock(plugins):
|
|||
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 TestType(manager.PluginTypeManager):
|
||||
"""Fake PluginTypeManager."""
|
||||
|
||||
|
|
@ -167,6 +174,32 @@ def test_provide_options(PluginManager):
|
|||
extra_args)
|
||||
|
||||
|
||||
@mock.patch('flake8.plugins.manager.PluginManager')
|
||||
def test_proxy_contains_to_managers_plugins_dict(PluginManager):
|
||||
"""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 = TestType()
|
||||
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_dictionary(PluginManager):
|
||||
"""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 = TestType()
|
||||
for i in range(8):
|
||||
key = 'T10%i' % i
|
||||
assert type_mgr[key] is plugins[key]
|
||||
|
||||
|
||||
class FakePluginTypeManager(manager.NotifierBuilder):
|
||||
"""Provide an easy way to test the NotifierBuilder."""
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue