"""Shared pytest fixtures for the ki18n tests.""" from __future__ import annotations import logging import pytest from ki18n.ki18n import Ki18n from support import LOGGER, Env @pytest.fixture(autouse=True) def _reset_singleton(): """Make sure no test sees (or leaks) a ``Ki18n`` singleton.""" Ki18n._instance = None yield Ki18n._instance = None @pytest.fixture def env(tmp_path, monkeypatch) -> Env: """Isolated application and user directories. ``XDG_DATA_HOME`` and ``LOCALAPPDATA`` point into the temporary directory, so real user data is never touched. """ base = tmp_path.resolve() environment = Env(base) monkeypatch.setenv("XDG_DATA_HOME", str(environment.data_home)) monkeypatch.setenv("LOCALAPPDATA", str(environment.data_home)) return environment @pytest.fixture def manager(env: Env) -> Ki18n: """Manager with the installed example languages ``en`` and ``de``.""" env.install_defaults() return env.make() @pytest.fixture def installed(env: Env) -> Env: """Environment with the installed example languages ``en`` and ``de``. Unlike ``manager`` this does not create the ``Ki18n`` instance yet, so a test can still add (user) resources before constructing it with ``installed.make()``. """ env.install_defaults() return env @pytest.fixture def log_warnings(caplog): """Return a function listing the library's WARNING messages so far.""" caplog.set_level(logging.WARNING, logger=LOGGER) def messages() -> list[str]: return [ record.getMessage() for record in caplog.records if record.name == LOGGER and record.levelno >= logging.WARNING ] return messages