mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-10 20:27:15 +01:00
"Are we on the newest version?" was not mechanically answerable across these repos: constraint styles had drifted per-ecosystem and a caret range is "newest-compatible" by definition, which gives a checker nothing to compare. This gate makes it answerable and then enforces the answer. Checks two things at once -- that every dependency carries an exact pin, and that the pin equals its registry's latest STABLE release -- across pub.dev, PyPI, npm, crates.io, the Go proxy, kuhyx/utils git tags, and the Flutter / Node / Python toolchains. Three deliberate design points: * Pre-releases are rejected per-ecosystem, because every ecosystem leaks them differently and each leak fails GREEN. npm lets a maintainer point `dist-tags.latest` at an rc; crates.io's `max_version` includes them; PyPI's legacy `info.version` includes yanked releases. Unit-tested with fixtures, since an end-to-end run cannot surface a wrong "up to date". * Offline, the gate serves an expired cache entry and PASSES with a DEGRADED banner (exit 3 only under --strict, which is CI). `--no-verify` is banned here, so a hook that hard-failed offline would leave no way to commit at all. * The allowlist has two entry classes. `blocked_by: transitive:<pkg>@<ver>` is a predicate that clears itself when upstream moves and must NOT carry a date; everything else is discretionary and requires `expires`, capped at 90 days. Exact pins plus latest-everywhere is provably unsatisfiable when a dependency internally constrains a shared transitive, and expiring the predicate class on a calendar would turn the gate red for a reason no commit could fix. An entry with nothing left to excuse is itself an error. One hook id, one shim, one shared implementation -- always delegating, so it cannot fork the way the 250-line cap forked into four ids, two of which were independent reimplementations. 256 tests, 100% branch coverage on every module of the gate. Also adds the workflow that runs them, which incidentally gives md_naming's 71 tests their first CI run. Not yet installed in any repo: a repo that gets the gate before its deps are bumped is instantly and permanently red, so the gate and the upgrade land together, per repo. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CRykabMwPgDzFHcARoqpaK
148 lines
4.9 KiB
Python
148 lines
4.9 KiB
Python
"""Cache-then-network-then-stale-cache, and the toolchain routing.
|
|
|
|
The third step is the one that matters: serving an *expired* entry when the
|
|
network is gone is what keeps the pre-commit hook from cornering a user who is
|
|
forbidden from passing `--no-verify`.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from dep_freshness.cache import Cache, Entry, ttl_for
|
|
from dep_freshness.models import Dep
|
|
from dep_freshness.registries import http
|
|
from dep_freshness.registries import toolchain as tc
|
|
from dep_freshness.resolve import Resolver, _toolchain_latest
|
|
|
|
|
|
def dep(ecosystem="pub", name="http") -> Dep:
|
|
return Dep(ecosystem=ecosystem, name=name, constraint="1.0.0",
|
|
path=Path("pubspec.yaml"), line=1, pinned="1.0.0")
|
|
|
|
|
|
@pytest.fixture
|
|
def resolver(cache_dir):
|
|
return Resolver(cache=Cache(cache_dir / "registry.json"))
|
|
|
|
|
|
def test_a_fresh_cache_entry_short_circuits_the_network(resolver, monkeypatch):
|
|
resolver.cache.put("pub", "http", "1.6.0")
|
|
monkeypatch.setattr("dep_freshness.resolve._fetch", _explode)
|
|
assert resolver.latest(dep()).version == "1.6.0"
|
|
|
|
|
|
def test_a_cold_cache_fetches_and_stores(resolver, monkeypatch):
|
|
monkeypatch.setattr("dep_freshness.resolve._fetch", lambda *_a: "1.6.0")
|
|
assert resolver.latest(dep()).version == "1.6.0"
|
|
assert resolver.cache.get("pub", "http").version == "1.6.0"
|
|
|
|
|
|
def test_an_expired_entry_is_served_when_the_network_is_gone(resolver, monkeypatch):
|
|
resolver.cache.put("pub", "http", "1.6.0")
|
|
stale = time.time() - ttl_for("pub") - 60
|
|
resolver.cache._data["pub/http"]["fetched_at"] = stale
|
|
monkeypatch.setattr("dep_freshness.resolve._fetch", _offline)
|
|
answer = resolver.latest(dep())
|
|
assert answer.version == "1.6.0"
|
|
assert answer.stale_cache_days is not None
|
|
assert not answer.unavailable
|
|
|
|
|
|
def test_no_cache_and_no_network_is_unavailable(resolver, monkeypatch):
|
|
monkeypatch.setattr("dep_freshness.resolve._fetch", _offline)
|
|
assert resolver.latest(dep()).unavailable
|
|
|
|
|
|
def test_prefetch_records_why_a_lookup_degraded(resolver, monkeypatch):
|
|
monkeypatch.setattr("dep_freshness.resolve._fetch", _offline)
|
|
resolver.prefetch([dep()])
|
|
assert resolver.degraded
|
|
|
|
|
|
def test_prefetch_survives_an_unexpected_error(resolver, monkeypatch):
|
|
monkeypatch.setattr("dep_freshness.resolve._fetch", _explode)
|
|
resolver.prefetch([dep()])
|
|
assert any("boom" in reason for reason in resolver.degraded)
|
|
|
|
|
|
def test_prefetch_skips_packages_already_fresh(resolver, monkeypatch):
|
|
resolver.cache.put("pub", "http", "1.6.0")
|
|
monkeypatch.setattr("dep_freshness.resolve._fetch", _explode)
|
|
resolver.prefetch([dep()])
|
|
assert resolver.degraded == []
|
|
|
|
|
|
def test_refresh_discards_the_existing_cache(cache_dir, monkeypatch):
|
|
seeded = Cache(cache_dir / "registry.json")
|
|
seeded.put("pub", "http", "1.5.0")
|
|
seeded.save()
|
|
monkeypatch.setattr("dep_freshness.resolve._fetch", lambda *_a: "1.6.0")
|
|
resolver = Resolver(cache=Cache(cache_dir / "registry.json"), refresh=True)
|
|
assert resolver.latest(dep()).version == "1.6.0"
|
|
|
|
|
|
def test_git_tags_get_a_longer_ttl_than_registries():
|
|
assert ttl_for("gittag") > ttl_for("pub")
|
|
|
|
|
|
def test_a_corrupt_cache_file_reads_as_a_cold_cache(cache_dir):
|
|
path = cache_dir / "registry.json"
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text("{not json", encoding="utf-8")
|
|
assert Cache(path).get("pub", "http") is None
|
|
|
|
|
|
def test_a_malformed_cache_entry_is_ignored(cache_dir):
|
|
cache = Cache(cache_dir / "registry.json")
|
|
cache._data["pub/http"] = {"version": "1.6.0"} # no fetched_at
|
|
assert cache.get("pub", "http") is None
|
|
|
|
|
|
def test_saving_is_a_no_op_when_nothing_changed(cache_dir):
|
|
cache = Cache(cache_dir / "registry.json")
|
|
cache.save()
|
|
assert not (cache_dir / "registry.json").exists()
|
|
|
|
|
|
def test_an_unwritable_cache_slows_the_next_run_but_does_not_fail_it(tmp_path):
|
|
blocked = tmp_path / "afile"
|
|
blocked.write_text("x", encoding="utf-8")
|
|
cache = Cache(blocked / "registry.json")
|
|
cache.put("pub", "http", "1.6.0")
|
|
cache.save() # must not raise
|
|
|
|
|
|
def test_entry_age_is_measured_from_fetch_time():
|
|
assert Entry("1.0.0", time.time() - 120).age() >= 120
|
|
|
|
|
|
@pytest.mark.parametrize("name,expected", [
|
|
("flutter", "3.47.2"), ("dart", "3.13.2"), ("node", "24.20.0"),
|
|
])
|
|
def test_toolchain_routing(monkeypatch, name, expected):
|
|
monkeypatch.setattr(tc, "flutter_latest", lambda: ("3.47.2", "3.13.2"))
|
|
monkeypatch.setattr(tc, "node_latest", lambda: "24.20.0")
|
|
assert _toolchain_latest(name) == expected
|
|
|
|
|
|
def test_python_targets_the_installed_interpreter():
|
|
"""Never python.org: on Arch the interpreter is pacman-managed."""
|
|
import platform
|
|
assert _toolchain_latest("python") == platform.python_version()
|
|
|
|
|
|
def test_an_unknown_toolchain_has_no_target():
|
|
assert _toolchain_latest("go") is None
|
|
|
|
|
|
def _offline(*_args):
|
|
raise http.Offline("no network")
|
|
|
|
|
|
def _explode(*_args):
|
|
raise RuntimeError("boom")
|