"""Shared fixtures and path setup for the searxng-cli test suite. This conftest is loaded by pytest before any test module is imported, so the ``sys.path`` insertion below makes the scripts/ directory importable as top-level modules (``search``, ``fetch``, ``common``, ``cache``, ``_config``) without requiring a package install. """ import sys from pathlib import Path # Make scripts/ importable as top-level modules. SCRIPTS_DIR = Path(__file__).resolve().parent.parent / "scripts" if str(SCRIPTS_DIR) not in sys.path: sys.path.insert(0, str(SCRIPTS_DIR)) import pytest @pytest.fixture(autouse=True) def force_stdlib_http_path(monkeypatch): """强制所有测试走 stdlib HTTP 路径(v2.5.0)。 search.py 的 search_json/search_html 自 v2.5.0 起在 requests 可用时 走模块级 Session(连接池复用),但既有测试 mock 的是 ``urllib.request.urlopen``——本机装了 requests 时这些 mock 会失效。 统一强制 stdlib 路径,让既有测试原样工作且互不干扰;requests 路径 的回归由端到端/专项测试覆盖。 """ import search as _search import fetch as _fetch monkeypatch.setattr(_search, "_HAS_REQUESTS", False) monkeypatch.setattr(_fetch, "_HAS_REQUESTS", False) @pytest.fixture def isolated_cache(monkeypatch, tmp_path): """Redirect the SQLite cache to a per-test temp directory. ``cache._cache_path()`` reads ``$SEARXNG_CACHE_DIR``; pointing it at a fresh ``tmp_path`` keeps each test hermetic and prevents cross-test contamination from leftover entries. """ monkeypatch.setenv("SEARXNG_CACHE_DIR", str(tmp_path)) return tmp_path