"""Helpers for the tests that start a client process and stop it with a signal.""" from __future__ import annotations import subprocess import sys from collections.abc import Callable, Iterator from pathlib import Path from types import SimpleNamespace from typing import Any import pytest from tests.remote.unit.test_remote_loopback import KEY from tests.remote.unit.test_session_lifetime import ROOT from unshackle.core.config import config posix = pytest.mark.skipif(sys.platform == "win32", reason="POSIX signals") BOUND = 8.0 @pytest.fixture def run(tmp_path: Path) -> Iterator[Callable[..., subprocess.Popen[str]]]: """Start a process in a session or a console group of its own; a fixture so that no process outlives its test.""" started: list[subprocess.Popen[str]] = [] def begin(*argv: str, **options: Any) -> subprocess.Popen[str]: defaults: dict[str, Any] = {"stdin": subprocess.DEVNULL, "stdout": subprocess.PIPE, "stderr": subprocess.STDOUT} if sys.platform == "win32": defaults["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP else: defaults["start_new_session"] = True argv = (sys.executable, *argv) options = {**defaults, **options} started.append(subprocess.Popen(argv, cwd=ROOT, text=True, encoding="utf-8", errors="replace", **options)) return started[-1] yield begin for process in started: process.kill() process.wait() if process.stdout: process.stdout.close() def wait_line(process: subprocess.Popen[str], text: str) -> str: assert process.stdout is not None for line in process.stdout: if text in line: return line pytest.fail(f"the client did not print {text!r}") def settings(url: str, path: Path) -> dict[str, Any]: """The config of a client process that reaches the test server and nothing else.""" folders = ("cache", "cookies", "downloads", "temp", "logs", "services", "wvds", "prds") return { "remote_services": {"test": {"url": url, "api_key": KEY}}, "directories": {name: str(path / name) for name in folders}, "update_checks": False, "title_cache_enabled": False, "output_template": {"movies": "{title}", "series": "{title}", "songs": "{title}"}, } @pytest.fixture(autouse=True) def lasting_sessions(server: SimpleNamespace) -> None: # noqa: F811 """A session must not expire on the server while a test holds the DELETE back.""" config.serve["session_ttl"] = 300