Skip to content

Commit 79ae08a

Browse files
committed
Test exceptions from loading via entry_points
1 parent 92e08da commit 79ae08a

2 files changed

Lines changed: 44 additions & 9 deletions

File tree

tests/test_config.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44

55
from typing import Any
66
from unittest import TestCase
7+
from unittest.mock import Mock, patch
78

89
import pytest
910
import xarray as xr
1011

11-
from xrlint.config import Config, ConfigObject, get_entry_point_plugins
12+
from xrlint.config import (
13+
Config,
14+
ConfigObject,
15+
get_entry_point_plugins,
16+
plugins_from_entry_points,
17+
)
1218
from xrlint.constants import CORE_PLUGIN_NAME
1319
from xrlint.plugin import Plugin, new_plugin
1420
from xrlint.processor import ProcessorOp, define_processor
@@ -50,6 +56,43 @@ def test_get_rule(self):
5056
with pytest.raises(ValueError, match="unknown rule 'foo'"):
5157
config_obj.get_rule("foo")
5258

59+
def test_plugins_from_entry_points_load_failure(self):
60+
"""Test that plugins_from_entry_points raises ValueError when entry point fails to load."""
61+
# Create a mock entry point that raises an exception when loaded
62+
mock_entry_point = Mock()
63+
mock_entry_point.name = "failing-plugin"
64+
mock_entry_point.load.side_effect = ImportError("Module not found")
65+
66+
# Mock the entry_points function to return our failing entry point
67+
with patch("importlib.metadata.entry_points") as mock_entry_points:
68+
mock_entry_points.return_value = [mock_entry_point]
69+
70+
# Verify it raises the expected ValueError with the proper message
71+
with pytest.raises(
72+
ValueError,
73+
match=r"failed to load xrlint plugin from entry point 'failing-plugin'",
74+
):
75+
plugins_from_entry_points()
76+
77+
def test_plugins_from_entry_points_export_failure(self):
78+
"""Test when the plugin module loads but export_plugin() fails."""
79+
# Create a mock module that loads successfully but export_plugin() fails
80+
mock_module = Mock()
81+
mock_module.export_plugin.side_effect = AttributeError("missing attribute")
82+
83+
mock_entry_point = Mock()
84+
mock_entry_point.name = "bad-export-plugin"
85+
mock_entry_point.load.return_value = mock_module
86+
87+
with patch("importlib.metadata.entry_points") as mock_entry_points:
88+
mock_entry_points.return_value = [mock_entry_point]
89+
90+
with pytest.raises(
91+
ValueError,
92+
match=r"failed to load xrlint plugin from entry point 'bad-export-plugin'",
93+
):
94+
plugins_from_entry_points()
95+
5396
def test_get_processor_op(self):
5497
class MyProc(ProcessorOp):
5598
def preprocess(

xrlint/config.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,6 @@
4444
"""
4545

4646

47-
def get_core_plugin() -> "Plugin":
48-
"""Get the fully imported, populated core plugin."""
49-
from xrlint.plugins.core import export_plugin
50-
51-
return export_plugin()
52-
53-
5447
def plugins_from_entry_points() -> dict[str, "Plugin"]:
5548
"""Load plugins from entry points.
5649
@@ -67,7 +60,6 @@ def plugins_from_entry_points() -> dict[str, "Plugin"]:
6760
plugin = plugin_module.export_plugin()
6861
plugins[plugin.meta.name] = plugin
6962
except Exception as e:
70-
breakpoint()
7163
raise ValueError(
7264
f"failed to load xrlint plugin from entry point {ep.name!r}: {e}"
7365
) from e

0 commit comments

Comments
 (0)