rework plugin loading

This commit is contained in:
Anthony Sottile 2021-12-31 13:11:00 -08:00
parent 38c5eceda9
commit 50d69150c1
36 changed files with 1277 additions and 1505 deletions

View file

@ -4,9 +4,6 @@
class ExtensionTestPlugin2:
"""Extension test plugin in its own directory."""
name = "ExtensionTestPlugin2"
version = "1.0.0"
def __init__(self, tree):
"""Construct an instance of test plugin."""

View file

@ -6,28 +6,19 @@ import pytest
from flake8 import checker
from flake8._compat import importlib_metadata
from flake8.plugins import manager
from flake8.plugins import finder
from flake8.processor import FileProcessor
PHYSICAL_LINE = "# Physical line content"
EXPECTED_REPORT = (1, 1, "T000 Expected Message")
EXPECTED_REPORT_PHYSICAL_LINE = (1, "T000 Expected Message")
EXPECTED_RESULT_PHYSICAL_LINE = (
"T000",
0,
1,
"Expected Message",
None,
)
EXPECTED_RESULT_PHYSICAL_LINE = ("T000", 0, 1, "Expected Message", None)
class PluginClass:
"""Simple file plugin class yielding the expected report."""
name = "test"
version = "1.0.0"
def __init__(self, tree):
"""Construct a dummy object to provide mandatory parameter."""
pass
@ -37,87 +28,78 @@ class PluginClass:
yield EXPECTED_REPORT + (type(self),)
def plugin_func(func):
"""Decorate file plugins which are implemented as functions."""
func.name = "test"
func.version = "1.0.0"
return func
@plugin_func
def plugin_func_gen(tree):
"""Yield the expected report."""
yield EXPECTED_REPORT + (type(plugin_func_gen),)
@plugin_func
def plugin_func_list(tree):
"""Return a list of expected reports."""
return [EXPECTED_REPORT + (type(plugin_func_list),)]
@plugin_func
def plugin_func_physical_ret(physical_line):
"""Expect report from a physical_line. Single return."""
return EXPECTED_REPORT_PHYSICAL_LINE
@plugin_func
def plugin_func_physical_none(physical_line):
"""Expect report from a physical_line. No results."""
return None
@plugin_func
def plugin_func_physical_list_single(physical_line):
"""Expect report from a physical_line. List of single result."""
return [EXPECTED_REPORT_PHYSICAL_LINE]
@plugin_func
def plugin_func_physical_list_multiple(physical_line):
"""Expect report from a physical_line. List of multiple results."""
return [EXPECTED_REPORT_PHYSICAL_LINE] * 2
@plugin_func
def plugin_func_physical_gen_single(physical_line):
"""Expect report from a physical_line. Generator of single result."""
yield EXPECTED_REPORT_PHYSICAL_LINE
@plugin_func
def plugin_func_physical_gen_multiple(physical_line):
"""Expect report from a physical_line. Generator of multiple results."""
for _ in range(3):
yield EXPECTED_REPORT_PHYSICAL_LINE
def plugin_func_out_of_bounds(logical_line):
"""This produces an error out of bounds."""
yield 10000, "L100 test"
def mock_file_checker_with_plugin(plugin_target):
"""Get a mock FileChecker class with plugin_target registered.
Useful as a starting point for mocking reports/results.
"""
# Mock an entry point returning the plugin target
entry_point = mock.Mock(spec=["load"])
entry_point.name = plugin_target.name
entry_point.load.return_value = plugin_target
entry_point.value = "mocked:value"
# Load the checker plugins using the entry point mock
with mock.patch.object(
importlib_metadata,
"entry_points",
return_value={"flake8.extension": [entry_point]},
):
checks = manager.Checkers()
to_load = [
finder.Plugin(
"flake-package",
"9001",
importlib_metadata.EntryPoint(
"Q",
f"{plugin_target.__module__}:{plugin_target.__name__}",
"flake8.extension",
),
),
]
plugins = finder.load_plugins(to_load, [])
# Prevent it from reading lines from stdin or somewhere else
with mock.patch(
"flake8.processor.FileProcessor.read_lines", return_value=["Line 1"]
):
file_checker = checker.FileChecker(
"-", checks.to_dictionary(), mock.MagicMock()
filename="-",
plugins=plugins.checkers,
options=mock.MagicMock(),
)
return file_checker
@ -173,11 +155,7 @@ def test_line_check_results(plugin_target, len_results):
def test_logical_line_offset_out_of_bounds():
"""Ensure that logical line offsets that are out of bounds do not crash."""
@plugin_func
def _logical_line_out_of_bounds(logical_line):
yield 10000, "L100 test"
file_checker = mock_file_checker_with_plugin(_logical_line_out_of_bounds)
file_checker = mock_file_checker_with_plugin(plugin_func_out_of_bounds)
logical_ret = (
"",
@ -293,7 +271,7 @@ def test_report_order(results, expected_order):
# Create a placeholder manager without arguments or plugins
# Just add one custom file checker which just provides the results
manager = checker.Manager(style_guide, [])
manager = checker.Manager(style_guide, finder.Checkers([], [], []))
manager.checkers = manager._all_checkers = [file_checker]
# _handle_results is the first place which gets the sorted result
@ -357,6 +335,10 @@ def test_handling_syntaxerrors_across_pythons():
"invalid syntax", ("<unknown>", 2, 1, "bad python:\n", 2, 11)
)
expected = (2, 1)
file_checker = checker.FileChecker("-", {}, mock.MagicMock())
file_checker = checker.FileChecker(
filename="-",
plugins=finder.Checkers([], [], []),
options=mock.MagicMock(),
)
actual = file_checker._extract_syntax_information(err)
assert actual == expected

View file

@ -1,17 +1,18 @@
"""Integration tests for plugin loading."""
from flake8.main import application
from flake8.main.cli import main
import pytest
LOCAL_PLUGIN_CONFIG = "tests/fixtures/config_files/local-plugin.ini"
LOCAL_PLUGIN_PATH_CONFIG = "tests/fixtures/config_files/local-plugin-path.ini"
from flake8.main.cli import main
from flake8.main.options import register_default_options
from flake8.main.options import stage1_arg_parser
from flake8.options import aggregator
from flake8.options import config
from flake8.options.manager import OptionManager
from flake8.plugins import finder
class ExtensionTestPlugin:
"""Extension test plugin."""
name = "ExtensionTestPlugin"
version = "1.0.0"
def __init__(self, tree):
"""Construct an instance of test plugin."""
@ -27,9 +28,6 @@ class ExtensionTestPlugin:
class ReportTestPlugin:
"""Report test plugin."""
name = "ReportTestPlugin"
version = "1.0.0"
def __init__(self, tree):
"""Construct an instance of test plugin."""
@ -37,41 +35,69 @@ class ReportTestPlugin:
"""Do nothing."""
def test_enable_local_plugin_from_config():
@pytest.fixture
def local_config(tmp_path):
cfg_s = f"""\
[flake8:local-plugins]
extension =
XE = {ExtensionTestPlugin.__module__}:{ExtensionTestPlugin.__name__}
report =
XR = {ReportTestPlugin.__module__}:{ReportTestPlugin.__name__}
"""
cfg = tmp_path.joinpath("tox.ini")
cfg.write_text(cfg_s)
return str(cfg)
def test_enable_local_plugin_from_config(local_config):
"""App can load a local plugin from config file."""
app = application.Application()
app.initialize(["flake8", "--config", LOCAL_PLUGIN_CONFIG])
cfg, cfg_dir = config.load_config(local_config, [], isolated=False)
plugins = finder.find_plugins(cfg)
plugin_paths = finder.find_local_plugin_paths(cfg, cfg_dir)
loaded_plugins = finder.load_plugins(plugins, plugin_paths)
assert app.check_plugins is not None
assert app.check_plugins["XE"].plugin is ExtensionTestPlugin
assert app.formatting_plugins is not None
assert app.formatting_plugins["XR"].plugin is ReportTestPlugin
(custom_extension,) = (
loaded
for loaded in loaded_plugins.checkers.tree
if loaded.entry_name == "XE"
)
custom_report = loaded_plugins.reporters["XR"]
assert custom_extension.obj is ExtensionTestPlugin
assert custom_report.obj is ReportTestPlugin
def test_local_plugin_can_add_option():
def test_local_plugin_can_add_option(local_config):
"""A local plugin can add a CLI option."""
app = application.Application()
app.initialize(
["flake8", "--config", LOCAL_PLUGIN_CONFIG, "--anopt", "foo"]
argv = ["--config", local_config, "--anopt", "foo"]
stage1_parser = stage1_arg_parser()
stage1_args, rest = stage1_parser.parse_known_args(argv)
cfg, cfg_dir = config.load_config(
config=stage1_args.config, extra=[], isolated=False
)
assert app.options is not None
assert app.options.anopt == "foo"
plugins = finder.find_plugins(cfg)
plugin_paths = finder.find_local_plugin_paths(cfg, cfg_dir)
loaded_plugins = finder.load_plugins(plugins, plugin_paths)
option_manager = OptionManager(
version="123",
plugin_versions="",
parents=[stage1_parser],
)
register_default_options(option_manager)
option_manager.register_plugins(loaded_plugins)
def test_enable_local_plugin_at_non_installed_path():
"""Can add a paths option in local-plugins config section for finding."""
app = application.Application()
app.initialize(["flake8", "--config", LOCAL_PLUGIN_PATH_CONFIG])
args = aggregator.aggregate_options(option_manager, cfg, cfg_dir, argv)
assert app.check_plugins is not None
assert app.check_plugins["XE"].plugin.name == "ExtensionTestPlugin2"
assert args.anopt == "foo"
class AlwaysErrors:
name = "AlwaysError"
version = "1"
def __init__(self, tree):
pass