Skip to content

Commit 9964f6e

Browse files
authored
feat: simplify plugin tests; fix plugin resetting (#819)
1 parent a1f1ad7 commit 9964f6e

11 files changed

Lines changed: 45 additions & 129 deletions

test/plugins/conftest.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Shared fixtures for plugin tests."""
2+
3+
import pytest
4+
5+
from mellea.plugins.manager import has_plugins, shutdown_plugins
6+
7+
8+
@pytest.fixture(autouse=True, scope="package")
9+
async def _restore_plugins_after_package(request):
10+
"""Re-register acceptance sets once after all plugin tests finish.
11+
12+
Plugin tests freely shut down the manager for isolation. This package-scoped
13+
fixture captures whether plugins were active at the start and restores them in
14+
teardown so that test modules collected *after* ``test/plugins/`` still see the
15+
session-scoped acceptance sets registered by ``auto_register_acceptance_sets``.
16+
"""
17+
plugins_disabled = request.config.getoption(
18+
"--disable-default-mellea-plugins", default=False
19+
)
20+
was_enabled = has_plugins()
21+
yield
22+
if was_enabled and not plugins_disabled:
23+
from mellea.plugins import register
24+
from test.plugins._acceptance_sets import ALL_ACCEPTANCE_SETS
25+
26+
for ps in ALL_ACCEPTANCE_SETS:
27+
register(ps)
28+
29+
30+
@pytest.fixture(autouse=True)
31+
async def reset_plugins():
32+
"""Shut down plugins before and after each test for isolation."""
33+
await shutdown_plugins()
34+
yield
35+
await shutdown_plugins()

test/plugins/test_blocking.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from mellea.plugins.base import PluginViolationError
1919
from mellea.plugins.context import build_global_context
2020
from mellea.plugins.hooks.session import SessionPreInitPayload
21-
from mellea.plugins.manager import ensure_plugin_manager, invoke_hook, shutdown_plugins
21+
from mellea.plugins.manager import ensure_plugin_manager, invoke_hook
2222
from mellea.plugins.types import HookType
2323

2424
# ---------------------------------------------------------------------------
@@ -52,18 +52,6 @@ async def _invoke_no_raise(payload: SessionPreInitPayload):
5252
return result
5353

5454

55-
# ---------------------------------------------------------------------------
56-
# Fixtures
57-
# ---------------------------------------------------------------------------
58-
59-
60-
@pytest.fixture(autouse=True)
61-
async def reset_plugins():
62-
"""Shut down and reset the plugin manager after every test."""
63-
yield
64-
await shutdown_plugins()
65-
66-
6755
# ---------------------------------------------------------------------------
6856
# TestBlockHelper
6957
# ---------------------------------------------------------------------------

test/plugins/test_execution_modes.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from mellea.plugins.base import PluginViolationError
3232
from mellea.plugins.hooks.generation import GenerationPreCallPayload
3333
from mellea.plugins.hooks.session import SessionPreInitPayload
34-
from mellea.plugins.manager import invoke_hook, shutdown_plugins
34+
from mellea.plugins.manager import invoke_hook
3535
from mellea.plugins.types import HookType, PluginMode
3636

3737
# ---------------------------------------------------------------------------
@@ -53,18 +53,6 @@ def _generation_payload(**kwargs) -> GenerationPreCallPayload:
5353
return GenerationPreCallPayload(**defaults)
5454

5555

56-
# ---------------------------------------------------------------------------
57-
# Fixtures
58-
# ---------------------------------------------------------------------------
59-
60-
61-
@pytest.fixture(autouse=True)
62-
async def cleanup_plugins():
63-
"""Reset plugin manager state after every test."""
64-
yield
65-
await shutdown_plugins()
66-
67-
6856
# ---------------------------------------------------------------------------
6957
# Enforce mode
7058
# ---------------------------------------------------------------------------

test/plugins/test_hook_call_sites.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
ModelOutputThunk,
3030
)
3131
from mellea.plugins import PluginResult, hook, register
32-
from mellea.plugins.manager import shutdown_plugins
3332
from mellea.stdlib.context import SimpleContext
3433

3534
# ---------------------------------------------------------------------------
@@ -89,18 +88,6 @@ def _make_thunk():
8988
return mot
9089

9190

92-
# ---------------------------------------------------------------------------
93-
# Fixtures
94-
# ---------------------------------------------------------------------------
95-
96-
97-
@pytest.fixture(autouse=True)
98-
async def reset_plugins():
99-
"""Shut down and reset the plugin manager after every test."""
100-
yield
101-
await shutdown_plugins()
102-
103-
10491
# ---------------------------------------------------------------------------
10592
# Generation hook call sites
10693
# ---------------------------------------------------------------------------

test/plugins/test_manager.py

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,24 @@
33
import pytest
44

55
from mellea.plugins.base import MelleaBasePayload
6-
from mellea.plugins.manager import (
7-
ensure_plugin_manager,
8-
has_plugins,
9-
invoke_hook,
10-
shutdown_plugins,
11-
)
6+
from mellea.plugins.manager import ensure_plugin_manager, has_plugins, invoke_hook
127
from mellea.plugins.types import HookType, PluginMode
138

149
# These tests require the contextforge plugin framework
1510
pytest.importorskip("cpex.framework")
1611

1712

18-
@pytest.fixture(autouse=True)
19-
async def cleanup_plugins():
20-
"""Ensure plugin manager is shut down after each test."""
21-
yield
22-
await shutdown_plugins()
23-
24-
2513
class TestNoOpGuards:
2614
@pytest.mark.asyncio
27-
async def test_invoke_hook_noop_when_no_plugins(self, request):
15+
async def test_invoke_hook_noop_when_no_plugins(self):
2816
"""When no plugins are registered, invoke_hook returns (None, original_payload)."""
29-
plugins_disabled = request.config.getoption(
30-
"--disable-default-mellea-plugins", default=False
31-
)
32-
if not plugins_disabled:
33-
pytest.skip("must pass --disable-default-mellea-plugins for this test")
3417
payload = MelleaBasePayload(request_id="test-123")
3518
result, returned_payload = await invoke_hook(HookType.SESSION_PRE_INIT, payload)
3619
assert result is None
3720
assert returned_payload is payload
3821

39-
def test_has_plugins_false_by_default(self, request):
22+
def test_has_plugins_false_by_default(self):
4023
"""has_plugins() returns False when no plugins have been registered."""
41-
plugins_disabled = request.config.getoption(
42-
"--disable-default-mellea-plugins", default=False
43-
)
44-
if not plugins_disabled:
45-
pytest.skip("must pass --disable-default-mellea-plugins for this test")
46-
# After shutdown, plugins should not be enabled
4724
assert not has_plugins()
4825

4926

test/plugins/test_mellea_plugin.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,10 @@
1111

1212
from mellea.plugins import Plugin, hook, register
1313
from mellea.plugins.hooks.session import SessionPreInitPayload
14-
from mellea.plugins.manager import invoke_hook, shutdown_plugins
14+
from mellea.plugins.manager import invoke_hook
1515
from mellea.plugins.types import HookType
1616

1717

18-
@pytest.fixture(autouse=True)
19-
async def reset_plugins():
20-
yield
21-
await shutdown_plugins()
22-
23-
2418
def _make_payload() -> SessionPreInitPayload:
2519
return SessionPreInitPayload(
2620
backend_name="test", model_id="test-model", model_options=None

test/plugins/test_policy_enforcement.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from mellea.plugins.hooks.generation import GenerationPreCallPayload
1818
from mellea.plugins.hooks.sampling import SamplingIterationPayload
1919
from mellea.plugins.hooks.session import SessionPreInitPayload
20-
from mellea.plugins.manager import invoke_hook, shutdown_plugins
20+
from mellea.plugins.manager import invoke_hook
2121
from mellea.plugins.types import HookType
2222

2323
# ---------------------------------------------------------------------------
@@ -39,18 +39,6 @@ def _generation_payload(**kwargs) -> GenerationPreCallPayload:
3939
return GenerationPreCallPayload(**defaults)
4040

4141

42-
# ---------------------------------------------------------------------------
43-
# Fixtures
44-
# ---------------------------------------------------------------------------
45-
46-
47-
@pytest.fixture(autouse=True)
48-
async def reset_plugins():
49-
"""Shut down and reset the plugin manager after every test."""
50-
yield
51-
await shutdown_plugins()
52-
53-
5442
# ---------------------------------------------------------------------------
5543
# TestWritableFieldAccepted
5644
# ---------------------------------------------------------------------------

test/plugins/test_priority_ordering.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from mellea.plugins import Plugin, PluginSet, hook, register
1919
from mellea.plugins.hooks.session import SessionPreInitPayload
20-
from mellea.plugins.manager import invoke_hook, shutdown_plugins
20+
from mellea.plugins.manager import invoke_hook
2121
from mellea.plugins.types import HookType
2222

2323
# ---------------------------------------------------------------------------
@@ -31,18 +31,6 @@ def _session_payload() -> SessionPreInitPayload:
3131
)
3232

3333

34-
# ---------------------------------------------------------------------------
35-
# Fixtures
36-
# ---------------------------------------------------------------------------
37-
38-
39-
@pytest.fixture(autouse=True)
40-
async def cleanup_plugins():
41-
"""Reset plugin manager state after every test."""
42-
yield
43-
await shutdown_plugins()
44-
45-
4634
# ---------------------------------------------------------------------------
4735
# Priority ordering
4836
# ---------------------------------------------------------------------------

test/plugins/test_scoping.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,10 @@
99
from mellea.plugins import Plugin, PluginSet, hook, plugin_scope, register
1010
from mellea.plugins.base import MelleaPlugin, PluginResult
1111
from mellea.plugins.hooks.session import SessionPreInitPayload
12-
from mellea.plugins.manager import (
13-
deregister_session_plugins,
14-
invoke_hook,
15-
shutdown_plugins,
16-
)
12+
from mellea.plugins.manager import deregister_session_plugins, invoke_hook
1713
from mellea.plugins.types import HookType
1814

1915

20-
@pytest.fixture(autouse=True)
21-
async def reset_plugins():
22-
"""Reset the plugin manager to a clean state after every test."""
23-
yield
24-
await shutdown_plugins()
25-
26-
2716
def _payload() -> SessionPreInitPayload:
2817
return SessionPreInitPayload(
2918
backend_name="test-backend", model_id="test-model", model_options=None

test/plugins/test_tool_hooks_redaction.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
from mellea.core.base import AbstractMelleaTool, ModelOutputThunk, ModelToolCall
2121
from mellea.plugins import PluginResult, hook, register
22-
from mellea.plugins.manager import shutdown_plugins
2322
from mellea.plugins.types import HookType
2423
from mellea.stdlib.functional import _acall_tools
2524

@@ -52,17 +51,6 @@ def _make_result(tool_call: ModelToolCall) -> ModelOutputThunk:
5251
return mot
5352

5453

55-
# ---------------------------------------------------------------------------
56-
# Fixtures
57-
# ---------------------------------------------------------------------------
58-
59-
60-
@pytest.fixture(autouse=True)
61-
async def reset_plugins():
62-
yield
63-
await shutdown_plugins()
64-
65-
6654
# ---------------------------------------------------------------------------
6755
# Tests
6856
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)