mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-15 08:44:46 +00:00
Update our plugin registration for debugging
This allows us to report whether or not a plugin is local when users provide `flake8 --bug-report` output.
This commit is contained in:
parent
d5dfd1180d
commit
156f90369f
6 changed files with 65 additions and 48 deletions
|
|
@ -53,8 +53,14 @@ def information(option_manager):
|
||||||
|
|
||||||
def plugins_from(option_manager):
|
def plugins_from(option_manager):
|
||||||
"""Generate the list of plugins installed."""
|
"""Generate the list of plugins installed."""
|
||||||
return [{'plugin': plugin, 'version': version}
|
return [
|
||||||
for (plugin, version) in sorted(option_manager.registered_plugins)]
|
{
|
||||||
|
'plugin': plugin.name,
|
||||||
|
'version': plugin.version,
|
||||||
|
'is_local': plugin.local,
|
||||||
|
}
|
||||||
|
for plugin in sorted(option_manager.registered_plugins)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def dependencies():
|
def dependencies():
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
"""Option handling and Option management logic."""
|
"""Option handling and Option management logic."""
|
||||||
|
import collections
|
||||||
import logging
|
import logging
|
||||||
import optparse # pylint: disable=deprecated-module
|
import optparse # pylint: disable=deprecated-module
|
||||||
|
|
||||||
|
|
@ -153,6 +154,10 @@ class Option(object):
|
||||||
return self._opt
|
return self._opt
|
||||||
|
|
||||||
|
|
||||||
|
PluginVersion = collections.namedtuple("PluginVersion",
|
||||||
|
["name", "version", "local"])
|
||||||
|
|
||||||
|
|
||||||
class OptionManager(object):
|
class OptionManager(object):
|
||||||
"""Manage Options and OptionParser while adding post-processing."""
|
"""Manage Options and OptionParser while adding post-processing."""
|
||||||
|
|
||||||
|
|
@ -178,9 +183,9 @@ class OptionManager(object):
|
||||||
self.extended_default_select = set()
|
self.extended_default_select = set()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def format_plugin(plugin_tuple):
|
def format_plugin(plugin):
|
||||||
"""Convert a plugin tuple into a dictionary mapping name to value."""
|
"""Convert a PluginVersion into a dictionary mapping name to value."""
|
||||||
return dict(zip(["name", "version"], plugin_tuple))
|
return {attr: getattr(plugin, attr) for attr in ["name", "version"]}
|
||||||
|
|
||||||
def add_option(self, *args, **kwargs):
|
def add_option(self, *args, **kwargs):
|
||||||
"""Create and register a new option.
|
"""Create and register a new option.
|
||||||
|
|
@ -306,7 +311,7 @@ class OptionManager(object):
|
||||||
self._normalize(options)
|
self._normalize(options)
|
||||||
return options, xargs
|
return options, xargs
|
||||||
|
|
||||||
def register_plugin(self, name, version):
|
def register_plugin(self, name, version, local=False):
|
||||||
"""Register a plugin relying on the OptionManager.
|
"""Register a plugin relying on the OptionManager.
|
||||||
|
|
||||||
:param str name:
|
:param str name:
|
||||||
|
|
@ -314,5 +319,7 @@ class OptionManager(object):
|
||||||
attribute of the class or function loaded from the entry-point.
|
attribute of the class or function loaded from the entry-point.
|
||||||
:param str version:
|
:param str version:
|
||||||
The version of the checker that we're using.
|
The version of the checker that we're using.
|
||||||
|
:param bool local:
|
||||||
|
Whether the plugin is local to the project/repository or not.
|
||||||
"""
|
"""
|
||||||
self.registered_plugins.add((name, version))
|
self.registered_plugins.add(PluginVersion(name, version, local))
|
||||||
|
|
|
||||||
|
|
@ -115,8 +115,6 @@ class Plugin(object):
|
||||||
self._version = version_for(self)
|
self._version = version_for(self)
|
||||||
else:
|
else:
|
||||||
self._version = self.plugin.version
|
self._version = self.plugin.version
|
||||||
if self.local:
|
|
||||||
self._version += ' [local]'
|
|
||||||
|
|
||||||
return self._version
|
return self._version
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import pytest
|
||||||
import setuptools
|
import setuptools
|
||||||
|
|
||||||
from flake8.main import debug
|
from flake8.main import debug
|
||||||
|
from flake8.options import manager
|
||||||
|
|
||||||
|
|
||||||
def test_dependencies():
|
def test_dependencies():
|
||||||
|
|
@ -15,11 +16,18 @@ def test_dependencies():
|
||||||
|
|
||||||
@pytest.mark.parametrize('plugins, expected', [
|
@pytest.mark.parametrize('plugins, expected', [
|
||||||
([], []),
|
([], []),
|
||||||
([('pycodestyle', '2.0.0')], [{'plugin': 'pycodestyle',
|
([manager.PluginVersion('pycodestyle', '2.0.0', False)],
|
||||||
'version': '2.0.0'}]),
|
[{'plugin': 'pycodestyle', 'version': '2.0.0', 'is_local': False}]),
|
||||||
([('pycodestyle', '2.0.0'), ('mccabe', '0.5.9')],
|
([manager.PluginVersion('pycodestyle', '2.0.0', False),
|
||||||
[{'plugin': 'mccabe', 'version': '0.5.9'},
|
manager.PluginVersion('mccabe', '0.5.9', False)],
|
||||||
{'plugin': 'pycodestyle', 'version': '2.0.0'}]),
|
[{'plugin': 'mccabe', 'version': '0.5.9', 'is_local': False},
|
||||||
|
{'plugin': 'pycodestyle', 'version': '2.0.0', 'is_local': False}]),
|
||||||
|
([manager.PluginVersion('pycodestyle', '2.0.0', False),
|
||||||
|
manager.PluginVersion('my-local', '0.0.1', True),
|
||||||
|
manager.PluginVersion('mccabe', '0.5.9', False)],
|
||||||
|
[{'plugin': 'mccabe', 'version': '0.5.9', 'is_local': False},
|
||||||
|
{'plugin': 'my-local', 'version': '0.0.1', 'is_local': True},
|
||||||
|
{'plugin': 'pycodestyle', 'version': '2.0.0', 'is_local': False}]),
|
||||||
])
|
])
|
||||||
def test_plugins_from(plugins, expected):
|
def test_plugins_from(plugins, expected):
|
||||||
"""Test that we format plugins appropriately."""
|
"""Test that we format plugins appropriately."""
|
||||||
|
|
@ -34,8 +42,10 @@ def test_information(system, pyversion, pyimpl):
|
||||||
"""Verify that we return all the information we care about."""
|
"""Verify that we return all the information we care about."""
|
||||||
expected = {
|
expected = {
|
||||||
'version': '3.1.0',
|
'version': '3.1.0',
|
||||||
'plugins': [{'plugin': 'mccabe', 'version': '0.5.9'},
|
'plugins': [{'plugin': 'mccabe', 'version': '0.5.9',
|
||||||
{'plugin': 'pycodestyle', 'version': '2.0.0'}],
|
'is_local': False},
|
||||||
|
{'plugin': 'pycodestyle', 'version': '2.0.0',
|
||||||
|
'is_local': False}],
|
||||||
'dependencies': [{'dependency': 'setuptools',
|
'dependencies': [{'dependency': 'setuptools',
|
||||||
'version': setuptools.__version__}],
|
'version': setuptools.__version__}],
|
||||||
'platform': {
|
'platform': {
|
||||||
|
|
@ -45,8 +55,10 @@ def test_information(system, pyversion, pyimpl):
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
option_manager = mock.Mock(
|
option_manager = mock.Mock(
|
||||||
registered_plugins={('pycodestyle', '2.0.0'),
|
registered_plugins={
|
||||||
('mccabe', '0.5.9')},
|
manager.PluginVersion('pycodestyle', '2.0.0', False),
|
||||||
|
manager.PluginVersion('mccabe', '0.5.9', False),
|
||||||
|
},
|
||||||
version='3.1.0',
|
version='3.1.0',
|
||||||
)
|
)
|
||||||
assert expected == debug.information(option_manager)
|
assert expected == debug.information(option_manager)
|
||||||
|
|
@ -75,7 +87,10 @@ def test_print_information_no_plugins(dumps, information, print_mock):
|
||||||
@mock.patch('json.dumps', return_value='{}')
|
@mock.patch('json.dumps', return_value='{}')
|
||||||
def test_print_information(dumps, information, print_mock):
|
def test_print_information(dumps, information, print_mock):
|
||||||
"""Verify we print and exit only when we have plugins."""
|
"""Verify we print and exit only when we have plugins."""
|
||||||
plugins = [('pycodestyle', '2.0.0'), ('mccabe', '0.5.9')]
|
plugins = [
|
||||||
|
manager.PluginVersion('pycodestyle', '2.0.0', False),
|
||||||
|
manager.PluginVersion('mccabe', '0.5.9', False),
|
||||||
|
]
|
||||||
option_manager = mock.Mock(registered_plugins=set(plugins))
|
option_manager = mock.Mock(registered_plugins=set(plugins))
|
||||||
with pytest.raises(SystemExit):
|
with pytest.raises(SystemExit):
|
||||||
debug.print_information(
|
debug.print_information(
|
||||||
|
|
|
||||||
|
|
@ -121,7 +121,9 @@ def test_parse_args_normalize_paths(optmanager):
|
||||||
|
|
||||||
def test_format_plugin():
|
def test_format_plugin():
|
||||||
"""Verify that format_plugin turns a tuple into a dictionary."""
|
"""Verify that format_plugin turns a tuple into a dictionary."""
|
||||||
plugin = manager.OptionManager.format_plugin(('Testing', '0.0.0'))
|
plugin = manager.OptionManager.format_plugin(
|
||||||
|
manager.PluginVersion('Testing', '0.0.0', False)
|
||||||
|
)
|
||||||
assert plugin['name'] == 'Testing'
|
assert plugin['name'] == 'Testing'
|
||||||
assert plugin['version'] == '0.0.0'
|
assert plugin['version'] == '0.0.0'
|
||||||
|
|
||||||
|
|
@ -129,9 +131,9 @@ def test_format_plugin():
|
||||||
def test_generate_versions(optmanager):
|
def test_generate_versions(optmanager):
|
||||||
"""Verify a comma-separated string is generated of registered plugins."""
|
"""Verify a comma-separated string is generated of registered plugins."""
|
||||||
optmanager.registered_plugins = [
|
optmanager.registered_plugins = [
|
||||||
('Testing 100', '0.0.0'),
|
manager.PluginVersion('Testing 100', '0.0.0', False),
|
||||||
('Testing 101', '0.0.0'),
|
manager.PluginVersion('Testing 101', '0.0.0', False),
|
||||||
('Testing 300', '0.0.0'),
|
manager.PluginVersion('Testing 300', '0.0.0', True),
|
||||||
]
|
]
|
||||||
assert (optmanager.generate_versions() ==
|
assert (optmanager.generate_versions() ==
|
||||||
'Testing 100: 0.0.0, Testing 101: 0.0.0, Testing 300: 0.0.0')
|
'Testing 100: 0.0.0, Testing 101: 0.0.0, Testing 300: 0.0.0')
|
||||||
|
|
@ -140,11 +142,11 @@ def test_generate_versions(optmanager):
|
||||||
def test_plugins_are_sorted_in_generate_versions(optmanager):
|
def test_plugins_are_sorted_in_generate_versions(optmanager):
|
||||||
"""Verify we sort before joining strings in generate_versions."""
|
"""Verify we sort before joining strings in generate_versions."""
|
||||||
optmanager.registered_plugins = [
|
optmanager.registered_plugins = [
|
||||||
('pyflakes', '1.5.0'),
|
manager.PluginVersion('pyflakes', '1.5.0', False),
|
||||||
('mccabe', '0.7.0'),
|
manager.PluginVersion('mccabe', '0.7.0', False),
|
||||||
('pycodestyle', '2.2.0'),
|
manager.PluginVersion('pycodestyle', '2.2.0', False),
|
||||||
('flake8-docstrings', '0.6.1'),
|
manager.PluginVersion('flake8-docstrings', '0.6.1', False),
|
||||||
('flake8-bugbear', '2016.12.1'),
|
manager.PluginVersion('flake8-bugbear', '2016.12.1', False),
|
||||||
]
|
]
|
||||||
assert (optmanager.generate_versions() ==
|
assert (optmanager.generate_versions() ==
|
||||||
'flake8-bugbear: 2016.12.1, '
|
'flake8-bugbear: 2016.12.1, '
|
||||||
|
|
@ -157,9 +159,9 @@ def test_plugins_are_sorted_in_generate_versions(optmanager):
|
||||||
def test_generate_versions_with_format_string(optmanager):
|
def test_generate_versions_with_format_string(optmanager):
|
||||||
"""Verify a comma-separated string is generated of registered plugins."""
|
"""Verify a comma-separated string is generated of registered plugins."""
|
||||||
optmanager.registered_plugins.update([
|
optmanager.registered_plugins.update([
|
||||||
('Testing', '0.0.0'),
|
manager.PluginVersion('Testing', '0.0.0', False),
|
||||||
('Testing', '0.0.0'),
|
manager.PluginVersion('Testing', '0.0.0', False),
|
||||||
('Testing', '0.0.0'),
|
manager.PluginVersion('Testing', '0.0.0', False),
|
||||||
])
|
])
|
||||||
assert (
|
assert (
|
||||||
optmanager.generate_versions() == 'Testing: 0.0.0'
|
optmanager.generate_versions() == 'Testing: 0.0.0'
|
||||||
|
|
@ -172,9 +174,9 @@ def test_update_version_string(optmanager):
|
||||||
assert optmanager.parser.version == TEST_VERSION
|
assert optmanager.parser.version == TEST_VERSION
|
||||||
|
|
||||||
optmanager.registered_plugins = [
|
optmanager.registered_plugins = [
|
||||||
('Testing 100', '0.0.0'),
|
manager.PluginVersion('Testing 100', '0.0.0', False),
|
||||||
('Testing 101', '0.0.0'),
|
manager.PluginVersion('Testing 101', '0.0.0', False),
|
||||||
('Testing 300', '0.0.0'),
|
manager.PluginVersion('Testing 300', '0.0.0', False),
|
||||||
]
|
]
|
||||||
|
|
||||||
optmanager.update_version_string()
|
optmanager.update_version_string()
|
||||||
|
|
@ -190,9 +192,9 @@ def test_generate_epilog(optmanager):
|
||||||
assert optmanager.parser.epilog is None
|
assert optmanager.parser.epilog is None
|
||||||
|
|
||||||
optmanager.registered_plugins = [
|
optmanager.registered_plugins = [
|
||||||
('Testing 100', '0.0.0'),
|
manager.PluginVersion('Testing 100', '0.0.0', False),
|
||||||
('Testing 101', '0.0.0'),
|
manager.PluginVersion('Testing 101', '0.0.0', False),
|
||||||
('Testing 300', '0.0.0'),
|
manager.PluginVersion('Testing 300', '0.0.0', False),
|
||||||
]
|
]
|
||||||
|
|
||||||
expected_value = (
|
expected_value = (
|
||||||
|
|
|
||||||
|
|
@ -111,17 +111,6 @@ def test_version_proxies_to_the_plugin():
|
||||||
assert plugin.version == 'a.b.c'
|
assert plugin.version == 'a.b.c'
|
||||||
|
|
||||||
|
|
||||||
def test_local_plugin_version():
|
|
||||||
"""Verify that local plugins have [local] appended to version."""
|
|
||||||
entry_point = mock.Mock(spec=['require', 'resolve', 'load'])
|
|
||||||
plugin_obj = mock.Mock(spec_set=['version'])
|
|
||||||
plugin_obj.version = 'a.b.c'
|
|
||||||
plugin = manager.Plugin('T000', entry_point, local=True)
|
|
||||||
plugin._plugin = plugin_obj
|
|
||||||
|
|
||||||
assert plugin.version == 'a.b.c [local]'
|
|
||||||
|
|
||||||
|
|
||||||
def test_register_options():
|
def test_register_options():
|
||||||
"""Verify we call add_options on the plugin only if it exists."""
|
"""Verify we call add_options on the plugin only if it exists."""
|
||||||
# Set up our mocks and Plugin object
|
# Set up our mocks and Plugin object
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue