diff --git a/dep_freshness/_tables.py b/dep_freshness/_tables.py index 3aa7390..fe0f57c 100644 --- a/dep_freshness/_tables.py +++ b/dep_freshness/_tables.py @@ -91,6 +91,8 @@ NPM_ACCEPT: Final = "application/vnd.npm.install-v1+json" # --- Cache ------------------------------------------------------------------ CACHE_PATH_ENV: Final = "DEP_FRESHNESS_CACHE" +# Seconds; overrides every ecosystem TTL (a repo that wants a once-a-day gate). +TTL_ENV: Final = "DEP_FRESHNESS_TTL_SECONDS" DEFAULT_CACHE_DIR: Final = "~/.cache/dep-freshness" CACHE_FILE: Final = "registry.json" TTL_SECONDS: Final = 6 * 3600 diff --git a/dep_freshness/cache.py b/dep_freshness/cache.py index 27020ff..be005ba 100644 --- a/dep_freshness/cache.py +++ b/dep_freshness/cache.py @@ -21,6 +21,7 @@ from dep_freshness._tables import ( DEFAULT_CACHE_DIR, GITCOMMIT, GITTAG, + TTL_ENV, TTL_GITTAG_SECONDS, TTL_SECONDS, ) @@ -33,7 +34,14 @@ def cache_dir() -> Path: def ttl_for(ecosystem: str) -> int: - """Git refs move rarely and cost a subprocess, so they cache longer.""" + """Git refs move rarely and cost a subprocess, so they cache longer. + + `DEP_FRESHNESS_TTL_SECONDS` overrides both; garbage is ignored, because a + typo in an env var must not break a commit hook. + """ + override = os.environ.get(TTL_ENV, "") + if override.isdigit() and int(override) > 0: + return int(override) return TTL_GITTAG_SECONDS if ecosystem in (GITTAG, GITCOMMIT) else TTL_SECONDS diff --git a/dep_freshness/tests/test_resolve.py b/dep_freshness/tests/test_resolve.py index 556626e..0b19e7e 100644 --- a/dep_freshness/tests/test_resolve.py +++ b/dep_freshness/tests/test_resolve.py @@ -12,6 +12,7 @@ from pathlib import Path import pytest +from dep_freshness._tables import TTL_SECONDS from dep_freshness.cache import Cache, Entry, ttl_for from dep_freshness.models import Dep from dep_freshness.registries import http @@ -139,3 +140,16 @@ def _offline(*_args): def _explode(*_args): raise RuntimeError("boom") + + +def test_ttl_env_overrides_the_registry_ttl(monkeypatch) -> None: + """A repo can stretch the refresh window (once-a-day pre-push gates).""" + monkeypatch.setenv("DEP_FRESHNESS_TTL_SECONDS", "86400") + assert ttl_for("npm") == 86400 + assert ttl_for("gittag") == 86400 + + +def test_ttl_env_garbage_falls_back_to_the_default(monkeypatch) -> None: + """A malformed value must never break a commit hook.""" + monkeypatch.setenv("DEP_FRESHNESS_TTL_SECONDS", "soon") + assert ttl_for("npm") == TTL_SECONDS