1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from __future__ import annotations
from types import TracebackType
from typing import Self
import pytest
from fastapi import Request
from starlette.responses import Response
from pygitweb.api.action import Action
from pygitweb.plugin_registry import PluginHandle, PluginRegistry, current_plugin_id, plugin_load_context
def test_plugin_load_context_sets_current_plugin() -> None:
previous = current_plugin_id()
with plugin_load_context("demo"):
assert current_plugin_id() == "demo"
assert current_plugin_id() == previous
def test_plugin_handle_unload_exits_context() -> None:
events: list[str] = []
class Tracked(Action):
def __enter__(self) -> Self:
events.append("enter")
return self
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> bool | None:
events.append("exit")
return None
def action_name(self) -> str:
return "tracked"
def action(self, project: str, req: Request | None = None) -> Response | None:
return None
import contextlib
stack = contextlib.ExitStack()
stack.enter_context(plugin_load_context("tracked"))
instance = stack.enter_context(Tracked())
handle = PluginHandle(plugin_id="tracked", _stack=stack, instance=instance)
assert current_plugin_id() == "tracked"
assert events == ["enter"]
handle.unload()
assert events == ["enter", "exit"]
assert current_plugin_id() != "tracked"
def test_plugin_registry_load_and_unload() -> None:
registry = PluginRegistry()
registry.load_all()
registry.unload_all()