utils/dep_freshness/tests/conftest.py
Krzysztof kuhy Rudnicki 1439c0f5c0 Make the allowlist inheritable, and cache the quarantine lookups
Two things that were fine in utils and would not have been in the next forty
repos.

**The allowlist is now fleet-wide plus per-repo.** `typescript-eslint` blocks
TS 7 in EVERY repo that pins typescript -- eight of them and counting -- and a
repo-only allowlist meant hand-copying one entry into eight files that then rot
independently. That is the four-way fork of the 250-line cap, reproduced inside
the mechanism built to avoid it. `~/utils/dependency-freshness.allowlist.yaml`
is inherited by every repo; a repo entry for the same `ecosystem:package`
overrides it.

Inherited entries are exempt from the no-longer-needed check everywhere except
the repo that owns the file. Without that carve-out a fleet-wide hold on
typescript exits 2 in every Flutter app -- it excuses nothing there, which is
the normal case, not rot -- and those repos become uncommittable. Confirmed
against punchme, which now exits 0 while utils still reports the entry as in
use.

**`quarantine.py` bypassed the cache entirely.** It ran for every npm finding
and fetched the FULL packument -- 7MB for react, 16MB for typescript -- with no
TTL and no reuse, inside a pre-commit hook. One finding in utils, but the JS
repos ahead have 16 to 37 each. Now cached under the normal 6h TTL, keyed
`<name>@<dist-tag>`: the tag has to be part of the key rather than clamped
afterwards, because clamping returns the ceiling and the ceiling is frequently
the very release the quarantine is excluding.

**Both cache and shared allowlist are now redirected by AUTOUSE fixtures.** A
quarantine test had already seeded a fake answer for package "x" into the real
~/.cache/dep-freshness/registry.json, and the next run read its own pollution
back and passed for the wrong reason. Removed the entry; the fixture makes it
unrepeatable.

292 tests, 100% branch coverage on every module.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CRykabMwPgDzFHcARoqpaK
2026-08-28 13:58:44 +02:00

88 lines
3.1 KiB
Python

"""Shared fixtures. Nothing here may touch the network.
Every registry answer is a fixture: the gate's worst failure mode is reporting
"up to date" against a pre-release, and a test that hits the real registry
cannot pin that behaviour down.
"""
from __future__ import annotations
from pathlib import Path
import subprocess
import sys
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
@pytest.fixture
def repo(tmp_path: Path) -> Path:
"""A throwaway git repo, so git-ignore lookups behave as in real use."""
subprocess.run(["git", "init", "-q", str(tmp_path)], check=True)
return tmp_path
@pytest.fixture(autouse=True)
def no_network(monkeypatch):
"""Fail loudly if a test reaches for a socket instead of a fixture.
Imported inside the fixture, not at module top: the sys.path insert above
is what makes the package importable at all, and a top-level import after
it is a lint violation this repo does not suppress.
"""
from dep_freshness.registries import http
def forbidden(*_args, **_kwargs):
raise AssertionError("test tried to open a real connection")
# Every host is reachable unless a test asked for offline mode, so
# `--offline` behaves in tests exactly as it does in a real run.
monkeypatch.setattr(
http, "host_reachable", lambda _url: not http._forced_offline
)
monkeypatch.setattr(http, "urlopen", forbidden)
http.reset_probes()
http.force_offline(False)
@pytest.fixture(autouse=True)
def no_shared_allowlist(tmp_path, monkeypatch) -> Path:
"""Point the fleet-wide allowlist at a path that does not exist.
AUTOUSE for the same reason as `cache_dir`: repos inherit the real
~/utils/dependency-freshness.allowlist.yaml, so without this every test
silently reads whatever is currently excused fleet-wide and starts
depending on it.
"""
# In its OWN directory, not tmp_path: the `repo` fixture is tmp_path, and
# the gate exempts inherited entries from the rot check everywhere except
# the repo that owns the shared file. Putting both in one directory makes
# every test look like it is running inside utils.
target = tmp_path / "shared" / "shared-allowlist.yaml"
target.parent.mkdir(parents=True, exist_ok=True)
monkeypatch.setenv("DEP_FRESHNESS_SHARED_ALLOWLIST", str(target))
return target
@pytest.fixture(autouse=True)
def cache_dir(tmp_path, monkeypatch) -> Path:
"""Point the on-disk registry snapshot at a scratch directory.
AUTOUSE, and not negotiable: any code path that constructs a bare `Cache()`
writes to the real ~/.cache/dep-freshness/registry.json otherwise. That
already happened once -- a quarantine test seeded a fake answer for package
"x" into the live cache, and the next run of the same test read its own
pollution back and passed for the wrong reason.
"""
target = tmp_path / "cache"
monkeypatch.setenv("DEP_FRESHNESS_CACHE", str(target))
return target
def write(root: Path, relative: str, body: str) -> Path:
path = root / relative
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(body, encoding="utf-8")
return path