Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

6.9 KB
Raw Blame
# allow: no-sut-import: exercises tests/connected/conftest.py's collection
# hook (test infrastructure, not package code) in bare pytest subprocesses;
# the regression must reproduce without the package's dependency tree, so
# there is nothing from local_deep_research to import here.
"""Regression: the connected suite's collection hook must stay scoped (#6301).

``tests/connected/conftest.py`` implements ``pytest_collection_modifyitems``,
which pytest invokes with the WHOLE session's collection, not just items
below the conftest's directory. It used to loop over every item, so merely
loading that conftest in a mixed run stamped ``connected`` onto unrelated
tests (``-m connected`` selected them, ``-m 'not connected'`` excluded
them), and when the SQLCipher availability probe failed it skip-marked them
all, a green pytest exit without executing anything.

Everything here runs pytest in fresh subprocesses so the outer session's
conftests and plugins cannot interfere:

* ``test_real_connected_conftest_does_not_mark_outside_tests`` collects a
  synthetic outside test file together with the real
  ``tests/connected/__init__.py``, since loading the real conftest is enough
  to reproduce the leak (no connected test has to run). ``--confcutdir``
  keeps the heavyweight root ``tests/conftest.py`` out of the subprocess so
  this also works on an interpreter without the package installed.
* the ``mixed_suite`` tests run a pytester-style temp tree whose
  ``tests/connected/conftest.py`` is the REAL file copied verbatim at
  runtime, with one trivial test on each side of the boundary. That makes
  both availability outcomes forceable: ``_sqlcipher_available`` is
  replaced in the subprocess before pytest starts, and pytest resolves the
  conftest to the same ``sys.modules`` entry, and lets connected items
  actually execute or skip without SQLCipher or the package installed.
"""

import os
import shutil
import subprocess
import sys
from pathlib import Path

import pytest

REPO_ROOT = Path(__file__).resolve().parents[2]
REAL_CONNECTED_DIR = REPO_ROOT / "tests" / "connected"

_OUTSIDE_TEST = """\
def test_outside_one():
    value = "outside"
    assert value == "outside"


def test_outside_two():
    assert sorted([2, 1]) == [1, 2]
"""

_CONNECTED_PROBE_TEST = """\
def test_connected_probe():
    assert "connected" in "tests/connected"
"""


def _run_pytest(args, cwd, force_probe=None):
    """Run pytest in a fresh interpreter and capture its output.

    ``force_probe`` (True/False) pre-imports ``tests.connected.conftest``
    relative to *cwd* and replaces ``_sqlcipher_available`` before handing
    control to pytest; pytest then resolves the conftest to that same
    ``sys.modules`` entry, so the patched probe is the one the collection
    hook calls. This forces either availability outcome deterministically,
    without uninstalling anything.
    """
    pytest_args = ["-q", "-p", "no:cacheprovider", *[str(a) for a in args]]
    if force_probe is None:
        cmd = [sys.executable, "-m", "pytest", *pytest_args]
    else:
        script = (
            "import sys\n"
            "import pytest\n"
            "import tests.connected.conftest as connected_conftest\n"
            f"connected_conftest._sqlcipher_available = lambda: {force_probe!r}\n"
            f"sys.exit(int(pytest.main({pytest_args!r})))\n"
        )
        cmd = [sys.executable, "-c", script]
    env = {
        **os.environ,
        # Determinism: no third-party plugin autoload (xdist/cov/timeout/...)
        # and no inherited addopts leaking into the subprocess under test.
        "PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1",
        "PYTEST_ADDOPTS": "",
    }
    return subprocess.run(
        cmd,
        cwd=cwd,
        env=env,
        capture_output=True,
        text=True,
        timeout=120,
        check=False,
    )


def test_real_connected_conftest_does_not_mark_outside_tests(tmp_path):
    """Loading the real connected conftest must leave outside tests unmarked.

    This is the issue's reproduction: before the fix, ``-m connected``
    selected the outside file's tests (exit 0) and ``-m 'not connected'``
    deselected them (exit 5) as soon as ``tests/connected/__init__.py``
    appeared on the command line.
    """
    outside = tmp_path / "test_outside.py"
    outside.write_text(_OUTSIDE_TEST)
    common = [
        outside,
        REAL_CONNECTED_DIR / "__init__.py",
        f"--confcutdir={REAL_CONNECTED_DIR}",
        "--collect-only",
    ]

    # Node ids' path prefix varies with the subprocess's rootdir, so match
    # on the ``::<function>`` part only.
    selected = _run_pytest([*common, "-m", "connected"], cwd=tmp_path)
    assert selected.returncode == pytest.ExitCode.NO_TESTS_COLLECTED, (
        selected.stdout + selected.stderr
    )
    assert "::test_outside_one" not in selected.stdout, selected.stdout

    deselected = _run_pytest([*common, "-m", "not connected"], cwd=tmp_path)
    assert deselected.returncode == pytest.ExitCode.OK, (
        deselected.stdout + deselected.stderr
    )
    assert "::test_outside_one" in deselected.stdout, deselected.stdout


@pytest.fixture
def mixed_suite(tmp_path):
    """A pytester-style mixed tree reusing the REAL connected conftest."""
    tests_pkg = tmp_path / "tests"
    connected = tests_pkg / "connected"
    connected.mkdir(parents=True)
    (tests_pkg / "__init__.py").write_text("")
    (connected / "__init__.py").write_text("")
    shutil.copyfile(
        REAL_CONNECTED_DIR / "conftest.py", connected / "conftest.py"
    )
    (tests_pkg / "test_outside.py").write_text(_OUTSIDE_TEST)
    (connected / "test_probe.py").write_text(_CONNECTED_PROBE_TEST)
    return tmp_path


def test_marker_selection_is_disjoint_with_sqlcipher_available(mixed_suite):
    """With the probe forced True, ``-m`` splits the suites cleanly."""
    connected_run = _run_pytest(
        ["tests", "-m", "connected"], cwd=mixed_suite, force_probe=True
    )
    assert connected_run.returncode == pytest.ExitCode.OK, (
        connected_run.stdout + connected_run.stderr
    )
    assert "1 passed" in connected_run.stdout, connected_run.stdout
    assert "2 deselected" in connected_run.stdout, connected_run.stdout

    unit_run = _run_pytest(
        ["tests", "-m", "not connected"], cwd=mixed_suite, force_probe=True
    )
    assert unit_run.returncode == pytest.ExitCode.OK, (
        unit_run.stdout + unit_run.stderr
    )
    assert "2 passed" in unit_run.stdout, unit_run.stdout
    assert "1 deselected" in unit_run.stdout, unit_run.stdout


def test_unavailable_sqlcipher_skips_only_connected_tests(mixed_suite):
    """With the probe forced False, only the connected test skips.

    Before the fix this run reported ``3 skipped``: the outside tests
    inherited the connected suite's skip and pytest still exited 0.
    """
    run = _run_pytest(["tests"], cwd=mixed_suite, force_probe=False)
    assert run.returncode == pytest.ExitCode.OK, run.stdout + run.stderr
    assert "2 passed" in run.stdout, run.stdout
    assert "1 skipped" in run.stdout, run.stdout