2020-01-12 00:07:58 +09:00
|
|
|
"""Test the EventManager class."""
|
|
|
|
|
|
2024-11-22 21:54:26 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2025-01-10 13:25:32 +00:00
|
|
|
from types import SimpleNamespace
|
2025-03-20 15:49:51 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2025-01-10 13:25:32 +00:00
|
|
|
|
2020-05-17 18:53:34 +09:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from sphinx.errors import ExtensionError
|
2020-01-12 00:07:58 +09:00
|
|
|
from sphinx.events import EventManager
|
|
|
|
|
|
2025-03-20 15:49:51 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from typing import NoReturn
|
|
|
|
|
|
|
|
|
|
from sphinx.application import Sphinx
|
|
|
|
|
|
2020-01-12 00:07:58 +09:00
|
|
|
|
2025-02-15 23:23:56 +00:00
|
|
|
def test_event_priority() -> None:
|
2020-01-12 00:07:58 +09:00
|
|
|
result = []
|
2025-06-07 01:34:48 +01:00
|
|
|
app = SimpleNamespace(pdb=False) # pass a dummy object as an app
|
2024-10-03 20:17:40 +01:00
|
|
|
events = EventManager(app) # type: ignore[arg-type]
|
2023-12-28 06:41:32 +00:00
|
|
|
events.connect('builder-inited', lambda app: result.append(1), priority=500)
|
|
|
|
|
events.connect('builder-inited', lambda app: result.append(2), priority=500)
|
2024-08-11 14:58:56 +01:00
|
|
|
# earlier
|
|
|
|
|
events.connect('builder-inited', lambda app: result.append(3), priority=200)
|
|
|
|
|
# later
|
|
|
|
|
events.connect('builder-inited', lambda app: result.append(4), priority=700)
|
2023-12-28 06:41:32 +00:00
|
|
|
events.connect('builder-inited', lambda app: result.append(5), priority=500)
|
2020-01-12 00:07:58 +09:00
|
|
|
|
|
|
|
|
events.emit('builder-inited')
|
|
|
|
|
assert result == [3, 1, 2, 5, 4]
|
2020-05-17 18:53:34 +09:00
|
|
|
|
|
|
|
|
|
2025-02-15 23:23:56 +00:00
|
|
|
def test_event_allowed_exceptions() -> None:
|
2025-03-20 15:49:51 +00:00
|
|
|
def raise_error(app: Sphinx) -> NoReturn:
|
2020-05-17 18:53:34 +09:00
|
|
|
raise RuntimeError
|
|
|
|
|
|
2025-01-10 13:25:32 +00:00
|
|
|
app = SimpleNamespace(pdb=False) # pass a dummy object as an app
|
2024-10-03 20:17:40 +01:00
|
|
|
events = EventManager(app) # type: ignore[arg-type]
|
2020-05-17 18:53:34 +09:00
|
|
|
events.connect('builder-inited', raise_error, priority=500)
|
|
|
|
|
|
2021-09-08 02:15:25 +02:00
|
|
|
# all errors are converted to ExtensionError
|
2020-05-17 18:53:34 +09:00
|
|
|
with pytest.raises(ExtensionError):
|
|
|
|
|
events.emit('builder-inited')
|
|
|
|
|
|
|
|
|
|
# Allow RuntimeError (pass-through)
|
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
|
events.emit('builder-inited', allowed_exceptions=(RuntimeError,))
|
2022-07-03 17:56:14 -07:00
|
|
|
|
|
|
|
|
|
2025-02-15 23:23:56 +00:00
|
|
|
def test_event_pdb() -> None:
|
2025-03-20 15:49:51 +00:00
|
|
|
def raise_error(app: Sphinx) -> NoReturn:
|
2022-07-03 17:56:14 -07:00
|
|
|
raise RuntimeError
|
|
|
|
|
|
2025-01-10 13:25:32 +00:00
|
|
|
app = SimpleNamespace(pdb=True) # pass a dummy object as an app
|
2024-10-03 20:17:40 +01:00
|
|
|
events = EventManager(app) # type: ignore[arg-type]
|
2022-07-03 17:56:14 -07:00
|
|
|
events.connect('builder-inited', raise_error, priority=500)
|
|
|
|
|
|
|
|
|
|
# errors aren't converted
|
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
|
events.emit('builder-inited')
|
|
|
|
|
|
|
|
|
|
# Allow RuntimeError (pass-through)
|
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
|
events.emit('builder-inited', allowed_exceptions=(RuntimeError,))
|