"""Tests run against a throwaway home: progress, config and caches are module-level paths from platformdirs, read at import time, so the environment is set before anything from diora_tui is imported.""" import os import tempfile import zipfile from pathlib import Path _HOME = tempfile.mkdtemp(prefix="diora-tui-test-") for var in ("LOCALAPPDATA", "APPDATA", "XDG_DATA_HOME", "XDG_CONFIG_HOME", "XDG_CACHE_HOME"): os.environ[var] = _HOME os.environ["HOME"] = _HOME os.environ["DIORA_TUI_HOME"] = _HOME # platformdirs ignores the variables above on Windows import pytest # noqa: E402 CONTAINER = """ """ def make_epub(path: Path, chapters: list[str], title: str = "Testbuch") -> Path: """A minimal EPUB whose spine is `chapters` (each one the full XHTML of a document).""" items = "".join(f'' for i in range(len(chapters))) spine = "".join(f'' for i in range(len(chapters))) opf = f""" {title}Autor test-{title}de {items}{spine}""" with zipfile.ZipFile(path, "w") as z: z.writestr("mimetype", "application/epub+zip") z.writestr("META-INF/container.xml", CONTAINER) z.writestr("OEBPS/content.opf", opf) for i, body in enumerate(chapters): z.writestr(f"OEBPS/c{i}.xhtml", f'x{body}') return path @pytest.fixture def epub(tmp_path): def build(chapters, title="Testbuch"): return make_epub(tmp_path / f"{title}.epub", chapters, title) return build @pytest.fixture(autouse=True) def clean_library_meta(): """library.json is one file in the shared test home: every test starts without it.""" from diora_tui import library_meta library_meta._META_FILE.unlink(missing_ok=True) yield library_meta._META_FILE.unlink(missing_ok=True)