Replace setuptools with entrypoints

This commit is contained in:
Anthony Sottile 2018-11-05 11:06:25 -08:00
parent 4439ea2025
commit ff15ba0865
9 changed files with 60 additions and 116 deletions

View file

@ -51,13 +51,12 @@ def plugin_func_list(tree):
def test_handle_file_plugins(plugin_target):
"""Test the FileChecker class handling different file plugin types."""
# Mock an entry point returning the plugin target
entry_point = mock.Mock(spec=['require', 'resolve', 'load'])
entry_point = mock.Mock(spec=['load'])
entry_point.name = plugin_target.name
entry_point.resolve.return_value = plugin_target
entry_point.load.return_value = plugin_target
# Load the checker plugins using the entry point mock
with mock.patch('pkg_resources.iter_entry_points',
return_value=[entry_point]):
with mock.patch('entrypoints.get_group_all', return_value=[entry_point]):
checks = manager.Checkers()
# Prevent it from reading lines from stdin or somewhere else

View file

@ -4,7 +4,6 @@ import optparse
import mock
import pytest
from flake8 import exceptions
from flake8.main import application as app
@ -61,14 +60,6 @@ def test_exit_does_raise(result_count, catastrophic, exit_zero, value,
assert excinfo.value.args[0] is value
def test_missing_default_formatter(application):
"""Verify we raise an ExecutionError when there's no default formatter."""
application.formatting_plugins = {}
with pytest.raises(exceptions.ExecutionError):
application.formatter_for('fake-plugin-name')
def test_warns_on_unknown_formatter_plugin_name(application):
"""Verify we log a warning with an unfound plugin."""
default = mock.Mock()

View file

@ -1,7 +1,7 @@
"""Tests for our debugging module."""
import entrypoints
import mock
import pytest
import setuptools
from flake8.main import debug
from flake8.options import manager
@ -9,8 +9,8 @@ from flake8.options import manager
def test_dependencies():
"""Verify that we format our dependencies appropriately."""
expected = [{'dependency': 'setuptools',
'version': setuptools.__version__}]
expected = [{'dependency': 'entrypoints',
'version': entrypoints.__version__}]
assert expected == debug.dependencies()
@ -46,8 +46,8 @@ def test_information(system, pyversion, pyimpl):
'is_local': False},
{'plugin': 'pycodestyle', 'version': '2.0.0',
'is_local': False}],
'dependencies': [{'dependency': 'setuptools',
'version': setuptools.__version__}],
'dependencies': [{'dependency': 'entrypoints',
'version': entrypoints.__version__}],
'platform': {
'python_implementation': 'CPython',
'python_version': '3.5.3',

View file

@ -14,48 +14,24 @@ def test_load_plugin_fallsback_on_old_setuptools():
plugin = manager.Plugin('T000', entry_point)
plugin.load_plugin()
entry_point.load.assert_called_once_with(require=False)
def test_load_plugin_avoids_deprecated_entry_point_methods():
"""Verify we use the preferred methods on new versions of setuptools."""
entry_point = mock.Mock(spec=['require', 'resolve', 'load'])
plugin = manager.Plugin('T000', entry_point)
plugin.load_plugin(verify_requirements=True)
assert entry_point.load.called is False
entry_point.require.assert_called_once_with()
entry_point.resolve.assert_called_once_with()
entry_point.load.assert_called_once_with()
def test_load_plugin_is_idempotent():
"""Verify we use the preferred methods on new versions of setuptools."""
entry_point = mock.Mock(spec=['require', 'resolve', 'load'])
plugin = manager.Plugin('T000', entry_point)
plugin.load_plugin(verify_requirements=True)
plugin.load_plugin(verify_requirements=True)
plugin.load_plugin()
assert entry_point.load.called is False
entry_point.require.assert_called_once_with()
entry_point.resolve.assert_called_once_with()
def test_load_plugin_only_calls_require_when_verifying_requirements():
"""Verify we do not call require when verify_requirements is False."""
entry_point = mock.Mock(spec=['require', 'resolve', 'load'])
entry_point = mock.Mock(spec=['load'])
plugin = manager.Plugin('T000', entry_point)
plugin.load_plugin()
assert entry_point.load.called is False
assert entry_point.require.called is False
entry_point.resolve.assert_called_once_with()
plugin.load_plugin()
plugin.load_plugin()
entry_point.load.assert_called_once_with()
def test_load_plugin_catches_and_reraises_exceptions():
"""Verify we raise our own FailedToLoadPlugin."""
entry_point = mock.Mock(spec=['require', 'resolve'])
entry_point.resolve.side_effect = ValueError('Test failure')
entry_point = mock.Mock(spec=['load'])
entry_point.load.side_effect = ValueError('Test failure')
plugin = manager.Plugin('T000', entry_point)
with pytest.raises(exceptions.FailedToLoadPlugin):
@ -64,27 +40,27 @@ def test_load_plugin_catches_and_reraises_exceptions():
def test_load_noncallable_plugin():
"""Verify that we do not load a non-callable plugin."""
entry_point = mock.Mock(spec=['require', 'resolve', 'load'])
entry_point.resolve.return_value = mock.NonCallableMock()
entry_point = mock.Mock(spec=['load'])
entry_point.load.return_value = mock.NonCallableMock()
plugin = manager.Plugin('T000', entry_point)
with pytest.raises(exceptions.FailedToLoadPlugin):
plugin.load_plugin()
entry_point.resolve.assert_called_once_with()
entry_point.load.assert_called_once_with()
def test_plugin_property_loads_plugin_on_first_use():
"""Verify that we load our plugin when we first try to use it."""
entry_point = mock.Mock(spec=['require', 'resolve', 'load'])
entry_point = mock.Mock(spec=['load'])
plugin = manager.Plugin('T000', entry_point)
assert plugin.plugin is not None
entry_point.resolve.assert_called_once_with()
entry_point.load.assert_called_once_with()
def test_execute_calls_plugin_with_passed_arguments():
"""Verify that we pass arguments directly to the plugin."""
entry_point = mock.Mock(spec=['require', 'resolve', 'load'])
entry_point = mock.Mock(spec=['load'])
plugin_obj = mock.Mock()
plugin = manager.Plugin('T000', entry_point)
plugin._plugin = plugin_obj
@ -96,13 +72,11 @@ def test_execute_calls_plugin_with_passed_arguments():
# Extra assertions
assert entry_point.load.called is False
assert entry_point.require.called is False
assert entry_point.resolve.called is False
def test_version_proxies_to_the_plugin():
"""Verify that we pass arguments directly to the plugin."""
entry_point = mock.Mock(spec=['require', 'resolve', 'load'])
entry_point = mock.Mock(spec=['load'])
plugin_obj = mock.Mock(spec_set=['version'])
plugin_obj.version = 'a.b.c'
plugin = manager.Plugin('T000', entry_point)
@ -114,7 +88,7 @@ def test_version_proxies_to_the_plugin():
def test_register_options():
"""Verify we call add_options on the plugin only if it exists."""
# Set up our mocks and Plugin object
entry_point = mock.Mock(spec=['require', 'resolve', 'load'])
entry_point = mock.Mock(spec=['load'])
plugin_obj = mock.Mock(spec_set=['name', 'version', 'add_options',
'parse_options'])
option_manager = mock.Mock(spec=['register_plugin'])
@ -131,7 +105,7 @@ def test_register_options():
def test_register_options_checks_plugin_for_method():
"""Verify we call add_options on the plugin only if it exists."""
# Set up our mocks and Plugin object
entry_point = mock.Mock(spec=['require', 'resolve', 'load'])
entry_point = mock.Mock(spec=['load'])
plugin_obj = mock.Mock(spec_set=['name', 'version', 'parse_options'])
option_manager = mock.Mock(spec=['register_plugin'])
plugin = manager.Plugin('T000', entry_point)
@ -147,7 +121,7 @@ def test_register_options_checks_plugin_for_method():
def test_provide_options():
"""Verify we call add_options on the plugin only if it exists."""
# Set up our mocks and Plugin object
entry_point = mock.Mock(spec=['require', 'resolve', 'load'])
entry_point = mock.Mock(spec=['load'])
plugin_obj = mock.Mock(spec_set=['name', 'version', 'add_options',
'parse_options'])
option_values = optparse.Values({'enable_extensions': []})

View file

@ -11,51 +11,51 @@ def create_entry_point_mock(name):
return ep
@mock.patch('pkg_resources.iter_entry_points')
def test_calls_pkg_resources_on_instantiation(iter_entry_points):
"""Verify that we call iter_entry_points when we create a manager."""
iter_entry_points.return_value = []
manager.PluginManager(namespace='testing.pkg_resources')
@mock.patch('entrypoints.get_group_all')
def test_calls_entrypoints_on_instantiation(get_group_all):
"""Verify that we call get_group_all when we create a manager."""
get_group_all.return_value = []
manager.PluginManager(namespace='testing.entrypoints')
iter_entry_points.assert_called_once_with('testing.pkg_resources')
get_group_all.assert_called_once_with('testing.entrypoints')
@mock.patch('pkg_resources.iter_entry_points')
def test_calls_pkg_resources_creates_plugins_automaticaly(iter_entry_points):
@mock.patch('entrypoints.get_group_all')
def test_calls_entrypoints_creates_plugins_automaticaly(get_group_all):
"""Verify that we create Plugins on instantiation."""
iter_entry_points.return_value = [
get_group_all.return_value = [
create_entry_point_mock('T100'),
create_entry_point_mock('T200'),
]
plugin_mgr = manager.PluginManager(namespace='testing.pkg_resources')
plugin_mgr = manager.PluginManager(namespace='testing.entrypoints')
iter_entry_points.assert_called_once_with('testing.pkg_resources')
get_group_all.assert_called_once_with('testing.entrypoints')
assert 'T100' in plugin_mgr.plugins
assert 'T200' in plugin_mgr.plugins
assert isinstance(plugin_mgr.plugins['T100'], manager.Plugin)
assert isinstance(plugin_mgr.plugins['T200'], manager.Plugin)
@mock.patch('pkg_resources.iter_entry_points')
def test_handles_mapping_functions_across_plugins(iter_entry_points):
@mock.patch('entrypoints.get_group_all')
def test_handles_mapping_functions_across_plugins(get_group_all):
"""Verify we can use the PluginManager call functions on all plugins."""
entry_point_mocks = [
create_entry_point_mock('T100'),
create_entry_point_mock('T200'),
]
iter_entry_points.return_value = entry_point_mocks
plugin_mgr = manager.PluginManager(namespace='testing.pkg_resources')
get_group_all.return_value = entry_point_mocks
plugin_mgr = manager.PluginManager(namespace='testing.entrypoints')
plugins = [plugin_mgr.plugins[name] for name in plugin_mgr.names]
assert list(plugin_mgr.map(lambda x: x)) == plugins
@mock.patch('pkg_resources.iter_entry_points')
def test_local_plugins(iter_entry_points):
@mock.patch('entrypoints.get_group_all')
def test_local_plugins(get_group_all):
"""Verify PluginManager can load given local plugins."""
iter_entry_points.return_value = []
get_group_all.return_value = []
plugin_mgr = manager.PluginManager(
namespace='testing.pkg_resources',
namespace='testing.entrypoints',
local_plugins=['X = path.to:Plugin']
)