Skip to content

Commit 43e53ac

Browse files
committed
Test exceptions from loading via entry_points
1 parent 92e08da commit 43e53ac

2 files changed

Lines changed: 44 additions & 2 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ def plugins_from_entry_points() -> dict[str, "Plugin"]:
6767
plugin = plugin_module.export_plugin()
6868
plugins[plugin.meta.name] = plugin
6969
except Exception as e:
70-
breakpoint()
7170
raise ValueError(
7271
f"failed to load xrlint plugin from entry point {ep.name!r}: {e}"
7372
) from e

0 commit comments

Comments
 (0)