mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-22 15:32:29 +01:00
dep_freshness: add pacman-repo lock adapter
Some checks are pending
file length / file-length (push) Waiting to run
kuhy_credit drift / drift (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
palette drift / palette (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
shared gate tests / gate unit tests (push) Waiting to run
Some checks are pending
file length / file-length (push) Waiting to run
kuhy_credit drift / drift (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
palette drift / palette (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
shared gate tests / gate unit tests (push) Waiting to run
A key=value *.lock file (name/repo/version/sha256) pins one package out of an Arch-style repo .db. The adapter reads the db (gzip or zstd tar) via a new http.get_bytes, matches %NAME% in each desc, and the judge compares with a port of pacman's vercmp (epoch:pkgver-pkgrel), not PEP 440. A right version with a wrong sha256 is a lock mismatch. Locks are recognised by content (a repo= line ending in .db in the first 4 KB of a *.lock), so explicit paths from pre-commit and --all both work. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
7917d70b97
commit
14f0f47c56
@ -23,6 +23,10 @@ MAVEN: Final = "maven"
|
||||
# wait for, so the only reading of "current" it admits is "at HEAD".
|
||||
GITCOMMIT: Final = "gitcommit"
|
||||
TOOLCHAIN: Final = "toolchain"
|
||||
# One package out of an Arch-style repo db (`<repo>.db`), pinned by a small
|
||||
# `key=value` lock file. Versions are `epoch:pkgver-pkgrel` and compare with
|
||||
# pacman's vercmp, never PEP 440 (see registries/pacman_repo.py).
|
||||
PACMAN_REPO: Final = "pacman-repo"
|
||||
# The constraint a workflow parser writes for a version MATRIX. Standing
|
||||
# decision (kuhy, 2026-08-28): one toolchain version per repo, always newest,
|
||||
# so a matrix is a finding to delete rather than a range to satisfy.
|
||||
@ -38,6 +42,7 @@ ECOSYSTEMS: Final = (
|
||||
MAVEN,
|
||||
GITCOMMIT,
|
||||
TOOLCHAIN,
|
||||
PACMAN_REPO,
|
||||
)
|
||||
|
||||
# --- Registry endpoints -----------------------------------------------------
|
||||
@ -134,6 +139,12 @@ MANIFEST_GLOBS: Final = (
|
||||
"gradle-wrapper.properties",
|
||||
)
|
||||
REQUIREMENTS_PATTERN: Final = r"^requirements.*\.txt$"
|
||||
# A pacman-repo lock has no fixed filename (`proton-native.lock` is one), so
|
||||
# it is recognised by content instead of a glob: a `*.lock` file whose first
|
||||
# few KB hold a `repo=` line ending in `.db`. The read is bounded because
|
||||
# `--all` sniffs every Cargo.lock and pubspec.lock too.
|
||||
PACMAN_LOCK_SUFFIX: Final = ".lock"
|
||||
PACMAN_LOCK_SNIFF_BYTES: Final = 4096
|
||||
|
||||
# `.utils` is where every CI workflow checks the shared gate out, INSIDE the
|
||||
# repo being checked. Without it `--all` walks utils' own manifests and fails
|
||||
|
||||
@ -19,6 +19,7 @@ from dep_freshness.parsers import (
|
||||
golang,
|
||||
gradle,
|
||||
javascript,
|
||||
pacman_lock,
|
||||
pubspec,
|
||||
python,
|
||||
rust,
|
||||
@ -46,6 +47,7 @@ def is_manifest(path: Path) -> bool:
|
||||
or bool(_REQUIREMENTS.match(path.name))
|
||||
or workflow.is_workflow(path)
|
||||
or gradle.is_catalog(path)
|
||||
or pacman_lock.is_lock(path)
|
||||
)
|
||||
|
||||
|
||||
@ -60,6 +62,8 @@ def parse_manifest(path: Path) -> list[Dep]:
|
||||
return workflow.parse(path)
|
||||
if gradle.is_catalog(path):
|
||||
return gradle.parse_catalog(path)
|
||||
if pacman_lock.is_lock(path):
|
||||
return pacman_lock.parse(path)
|
||||
return []
|
||||
|
||||
|
||||
|
||||
@ -15,9 +15,10 @@ purpose:
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dep_freshness._tables import GITCOMMIT, MATRIX, TOOLCHAIN
|
||||
from dep_freshness._tables import GITCOMMIT, MATRIX, PACMAN_REPO, TOOLCHAIN
|
||||
from dep_freshness.constraints import lower_bound, satisfies
|
||||
from dep_freshness.models import Dep, Finding, Severity
|
||||
from dep_freshness.registries.pacman_repo import vercmp
|
||||
from dep_freshness.resolve import Answer
|
||||
from dep_freshness.versions import behind, google_api_key
|
||||
|
||||
@ -46,6 +47,29 @@ def _commit(dep: Dep, heads: str) -> Finding | None:
|
||||
)
|
||||
|
||||
|
||||
def _pacman(dep: Dep, answer: str) -> Finding | None:
|
||||
"""A repo db lists ONE version per package; any other pin is undownloadable.
|
||||
|
||||
`answer` is `"<version> <sha256>"`. Compared with pacman's vercmp, so an
|
||||
epoch outranks a bigger pkgver. The checksum goes into the detail so the
|
||||
bump is a paste, and a right version with a wrong checksum is caught.
|
||||
"""
|
||||
version, _, sha = answer.partition(" ")
|
||||
fix = f"version={version}" + (f" sha256={sha}" if sha else "")
|
||||
if not dep.pinned:
|
||||
return Finding(dep, Severity.UNPINNED, version, detail=f"pin {fix}")
|
||||
if vercmp(dep.pinned, version) != 0:
|
||||
return Finding(dep, Severity.STALE, version, detail=f"the repo serves {fix}")
|
||||
if sha and dep.locked and dep.locked != sha.lower():
|
||||
return Finding(
|
||||
dep,
|
||||
Severity.LOCK_MISMATCH,
|
||||
version,
|
||||
detail=f"lock sha256 {dep.locked} != the repo's {sha}",
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
def _generation(dep: Dep, pin, latest: str) -> Finding | None:
|
||||
"""A Google API client is judged inside its own generation only."""
|
||||
gen, rank = pin
|
||||
@ -130,6 +154,8 @@ def judge(dep: Dep, answer: Answer) -> Finding | None:
|
||||
return _toolchain(dep, latest)
|
||||
if dep.ecosystem == GITCOMMIT:
|
||||
return _commit(dep, latest)
|
||||
if dep.ecosystem == PACMAN_REPO:
|
||||
return _pacman(dep, latest)
|
||||
pin = google_api_key(dep.pinned or "")
|
||||
if pin is not None:
|
||||
return _generation(dep, pin, latest)
|
||||
|
||||
81
dep_freshness/parsers/pacman_lock.py
Normal file
81
dep_freshness/parsers/pacman_lock.py
Normal file
@ -0,0 +1,81 @@
|
||||
"""A pacman-repo lock -> one package pinned out of an Arch-style repo db.
|
||||
|
||||
The format is one `key=value` per line, `#` comments allowed:
|
||||
|
||||
name=proton-cachyos-native
|
||||
repo=https://mirror.cachyos.org/repo/x86_64/cachyos/cachyos.db
|
||||
version=1:11.0.20261015-1
|
||||
sha256=<hex>
|
||||
|
||||
There is no fixed filename, so `is_lock` recognises one by content: a
|
||||
`*.lock` file whose first few KB hold a `repo=` line ending in `.db`. That
|
||||
keeps Cargo.lock, pubspec.lock and friends out without a name list, and lets
|
||||
pre-commit pass the file by name. `version` is kept verbatim: the epoch makes
|
||||
it unreadable to `exact_pin`, and the judge compares it with pacman's vercmp.
|
||||
`sha256` rides in `Dep.locked`, the field for "what the lock resolved to".
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from dep_freshness._tables import (
|
||||
PACMAN_LOCK_SNIFF_BYTES,
|
||||
PACMAN_LOCK_SUFFIX,
|
||||
PACMAN_REPO,
|
||||
)
|
||||
from dep_freshness.models import Dep
|
||||
from dep_freshness.registries.pacman_repo import qualified_name
|
||||
|
||||
_REPO_DB = re.compile(r"^\s*repo\s*=\s*\S+\.db\s*$", re.MULTILINE)
|
||||
|
||||
|
||||
def is_lock(path: Path) -> bool:
|
||||
"""True for a `*.lock` whose head names a repo `.db`."""
|
||||
if not path.name.endswith(PACMAN_LOCK_SUFFIX):
|
||||
return False
|
||||
try:
|
||||
with path.open("rb") as handle:
|
||||
head = handle.read(PACMAN_LOCK_SNIFF_BYTES)
|
||||
except OSError:
|
||||
return False
|
||||
return bool(_REPO_DB.search(head.decode("utf-8", errors="replace")))
|
||||
|
||||
|
||||
def _fields(text: str) -> dict[str, tuple[str, int]]:
|
||||
"""`key -> (value, 1-based line)`; the first occurrence of a key wins."""
|
||||
fields: dict[str, tuple[str, int]] = {}
|
||||
for number, line in enumerate(text.splitlines(), start=1):
|
||||
stripped = line.strip()
|
||||
if not stripped or stripped.startswith("#") or "=" not in stripped:
|
||||
continue
|
||||
key, _, value = stripped.partition("=")
|
||||
fields.setdefault(key.strip(), (value.strip(), number))
|
||||
return fields
|
||||
|
||||
|
||||
def parse(path: Path) -> list[Dep]:
|
||||
if not is_lock(path):
|
||||
return []
|
||||
try:
|
||||
fields = _fields(path.read_text(encoding="utf-8", errors="replace"))
|
||||
except OSError:
|
||||
return []
|
||||
name, name_line = fields.get("name", ("", 0))
|
||||
repo, _ = fields.get("repo", ("", 0))
|
||||
if not name or not repo:
|
||||
return []
|
||||
version, version_line = fields.get("version", ("", 0))
|
||||
sha, _ = fields.get("sha256", ("", 0))
|
||||
return [
|
||||
Dep(
|
||||
ecosystem=PACMAN_REPO,
|
||||
name=qualified_name(repo, name),
|
||||
constraint=version,
|
||||
path=path,
|
||||
line=version_line or name_line,
|
||||
pinned=version or None,
|
||||
locked=sha.lower() or None,
|
||||
)
|
||||
]
|
||||
@ -112,6 +112,20 @@ def get_text(url: str, accept: str | None = None) -> str | None:
|
||||
|
||||
Same contract as `get_json`; Maven metadata is XML, not JSON.
|
||||
"""
|
||||
body = get_bytes(url, accept)
|
||||
if body is None:
|
||||
return None
|
||||
try:
|
||||
return body.decode("utf-8")
|
||||
except UnicodeDecodeError as exc:
|
||||
raise Offline(f"{url}: {exc}") from exc
|
||||
|
||||
|
||||
def get_bytes(url: str, accept: str | None = None) -> bytes | None:
|
||||
"""Fetch a raw body, or raise `Offline` / return None on 404.
|
||||
|
||||
Same contract as `get_json`; a pacman repo `.db` is a compressed tar.
|
||||
"""
|
||||
if not host_reachable(url):
|
||||
raise Offline(url)
|
||||
headers = {"User-Agent": USER_AGENT}
|
||||
@ -125,7 +139,7 @@ def get_text(url: str, accept: str | None = None) -> str | None:
|
||||
attempt += 1
|
||||
try:
|
||||
with urlopen(request, timeout=HTTP_TIMEOUT * attempt) as response:
|
||||
return response.read().decode("utf-8")
|
||||
return response.read()
|
||||
except HTTPError as exc:
|
||||
if exc.code == 404:
|
||||
return None
|
||||
|
||||
179
dep_freshness/registries/pacman_repo.py
Normal file
179
dep_freshness/registries/pacman_repo.py
Normal file
@ -0,0 +1,179 @@
|
||||
"""Arch-style package repos: the one version a repo `.db` lists for a package.
|
||||
|
||||
A repo db (`cachyos.db`, `extra.db`) is a tar, gzip- or zstd-compressed, with
|
||||
one `<name>-<version>/desc` entry per package. The desc file holds `%NAME%`,
|
||||
`%VERSION%` and `%SHA256SUM%` blocks. A repo keeps exactly ONE version of each
|
||||
package, so "newest stable" is simply whatever the db lists.
|
||||
|
||||
The repo URL travels inside the dep name as `<repo-url>#<package>`, because
|
||||
the resolver and the cache only ever see the name (the gittag lesson). Arch
|
||||
package names cannot contain `#`, so `rpartition` splits it unambiguously.
|
||||
That string is also the cache key and the allowlist label.
|
||||
|
||||
The answer is `"<version> <sha256>"`, space-joined like gitcommit's heads:
|
||||
the judge needs the version to compare and the checksum to check the lock's.
|
||||
|
||||
Versions compare with pacman's `vercmp` (a port of libalpm's version.c),
|
||||
never PEP 440: `1:11.0.20261015-1` is not a version `packaging` can read.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import tarfile
|
||||
|
||||
from dep_freshness.registries.http import Offline, get_bytes
|
||||
|
||||
try: # stdlib since Python 3.14; tarfile's "r:*" reads zstd through it
|
||||
import compression.zstd # noqa: F401
|
||||
|
||||
_HAS_ZSTD = True
|
||||
except ImportError: # pragma: no cover - this machine runs 3.15
|
||||
_HAS_ZSTD = False
|
||||
|
||||
_ZSTD_MAGIC = b"\x28\xb5\x2f\xfd"
|
||||
_SEPARATOR = "#"
|
||||
|
||||
|
||||
def qualified_name(repo: str, package: str) -> str:
|
||||
"""The dep name for `package` out of the db at `repo`."""
|
||||
return f"{repo.strip()}{_SEPARATOR}{package.strip()}"
|
||||
|
||||
|
||||
def split_name(name: str) -> tuple[str, str]:
|
||||
"""`(repo_url, package)` back out of a `qualified_name`."""
|
||||
repo, _, package = name.rpartition(_SEPARATOR)
|
||||
return repo, package
|
||||
|
||||
|
||||
def _fields(desc: str) -> dict[str, str]:
|
||||
"""`{"NAME": ..., "VERSION": ...}`: the first line under each `%KEY%`."""
|
||||
fields: dict[str, str] = {}
|
||||
lines = desc.splitlines()
|
||||
for number, line in enumerate(lines[:-1]):
|
||||
if len(line) > 2 and line.startswith("%") and line.endswith("%"):
|
||||
fields.setdefault(line.strip("%"), lines[number + 1].strip())
|
||||
return fields
|
||||
|
||||
|
||||
def _open(url: str, body: bytes) -> tarfile.TarFile:
|
||||
if body.startswith(_ZSTD_MAGIC) and not _HAS_ZSTD:
|
||||
# `Offline`, not a bare error: `Resolver.latest` catches only that, so
|
||||
# anything else would crash the gate instead of reporting UNKNOWN
|
||||
# (exit 3 under --strict) with this message in the degraded banner.
|
||||
raise Offline(
|
||||
f"{url} is zstd-compressed; reading it needs Python 3.14+ "
|
||||
"(compression.zstd)"
|
||||
)
|
||||
try:
|
||||
return tarfile.open(fileobj=io.BytesIO(body), mode="r:*")
|
||||
except tarfile.TarError as exc:
|
||||
# A captive portal's HTML is a broken network, not an answer.
|
||||
raise Offline(f"{url}: not a repo db ({exc})") from exc
|
||||
|
||||
|
||||
def latest(name: str) -> str | None:
|
||||
"""`"<version> <sha256>"` the repo lists for the package, or None.
|
||||
|
||||
Matched on `%NAME%`, not the directory name: `foo-` also prefixes
|
||||
`foo-git-1.0-1/`. The tar is untrusted, so it is read, never extracted.
|
||||
"""
|
||||
repo, package = split_name(name)
|
||||
body = get_bytes(repo)
|
||||
if body is None:
|
||||
return None
|
||||
with _open(repo, body) as tar:
|
||||
for member in tar:
|
||||
if not (
|
||||
member.isfile()
|
||||
and member.name.startswith(f"{package}-")
|
||||
and member.name.endswith("/desc")
|
||||
):
|
||||
continue
|
||||
handle = tar.extractfile(member)
|
||||
if handle is None:
|
||||
continue
|
||||
fields = _fields(handle.read().decode("utf-8", errors="replace"))
|
||||
if fields.get("NAME") == package and fields.get("VERSION"):
|
||||
sha = fields.get("SHA256SUM", "")
|
||||
return f"{fields['VERSION']} {sha}".strip()
|
||||
return None
|
||||
|
||||
|
||||
# --- vercmp -----------------------------------------------------------------
|
||||
|
||||
|
||||
def _alnum(char: str) -> bool:
|
||||
return char.isascii() and char.isalnum()
|
||||
|
||||
|
||||
def _rpmvercmp(a: str, b: str) -> int:
|
||||
"""libalpm's `rpmvercmp`: segment-wise compare of one EVR component."""
|
||||
if a == b:
|
||||
return 0
|
||||
i = j = 0
|
||||
while i < len(a) and j < len(b):
|
||||
sep_a, sep_b = i, j
|
||||
while i < len(a) and not _alnum(a[i]):
|
||||
i += 1
|
||||
while j < len(b) and not _alnum(b[j]):
|
||||
j += 1
|
||||
if i >= len(a) or j >= len(b):
|
||||
break
|
||||
# A longer separator run sorts higher (`1.0..1` > `1.0.1`).
|
||||
if i - sep_a != j - sep_b:
|
||||
return -1 if i - sep_a < j - sep_b else 1
|
||||
start_a, start_b = i, j
|
||||
numeric = a[i].isdigit()
|
||||
same_kind = str.isdigit if numeric else (lambda c: c.isascii() and c.isalpha())
|
||||
while i < len(a) and same_kind(a[i]):
|
||||
i += 1
|
||||
while j < len(b) and same_kind(b[j]):
|
||||
j += 1
|
||||
seg_a, seg_b = a[start_a:i], b[start_b:j]
|
||||
if not seg_b:
|
||||
# Different segment kinds: a number beats letters.
|
||||
return 1 if numeric else -1
|
||||
if numeric:
|
||||
seg_a, seg_b = seg_a.lstrip("0"), seg_b.lstrip("0")
|
||||
if len(seg_a) != len(seg_b):
|
||||
return 1 if len(seg_a) > len(seg_b) else -1
|
||||
if seg_a != seg_b:
|
||||
return 1 if seg_a > seg_b else -1
|
||||
if i >= len(a) and j >= len(b):
|
||||
return 0
|
||||
# One side has more left: `1.0.a` > `1.0`, but `1.0a` < `1.0`.
|
||||
if (i >= len(a) and not b[j].isalpha()) or (i < len(a) and a[i].isalpha()):
|
||||
return -1
|
||||
return 1
|
||||
|
||||
|
||||
def _evr(text: str) -> tuple[str, str, str | None]:
|
||||
"""`(epoch, pkgver, pkgrel)`; epoch defaults to "0", pkgrel may be None."""
|
||||
digits = 0
|
||||
while digits < len(text) and text[digits].isdigit():
|
||||
digits += 1
|
||||
if digits < len(text) and text[digits] == ":":
|
||||
epoch, rest = text[:digits] or "0", text[digits + 1 :]
|
||||
else:
|
||||
epoch, rest = "0", text
|
||||
version, dash, release = rest.rpartition("-")
|
||||
if not dash:
|
||||
return epoch, rest, None
|
||||
return epoch, version, release
|
||||
|
||||
|
||||
def vercmp(left: str, right: str) -> int:
|
||||
"""-1, 0 or 1, exactly as pacman's `vercmp left right` prints.
|
||||
|
||||
A missing pkgrel on either side compares equal on pkgrel, so `1.0`
|
||||
matches `1.0-7`.
|
||||
"""
|
||||
if left == right:
|
||||
return 0
|
||||
epoch_l, ver_l, rel_l = _evr(left)
|
||||
epoch_r, ver_r, rel_r = _evr(right)
|
||||
result = _rpmvercmp(epoch_l, epoch_r) or _rpmvercmp(ver_l, ver_r)
|
||||
if result == 0 and rel_l is not None and rel_r is not None:
|
||||
result = _rpmvercmp(rel_l, rel_r)
|
||||
return result
|
||||
@ -18,6 +18,7 @@ from dep_freshness._tables import (
|
||||
MAVEN,
|
||||
MAX_WORKERS,
|
||||
NPM,
|
||||
PACMAN_REPO,
|
||||
PUB,
|
||||
PYPI,
|
||||
TOOLCHAIN,
|
||||
@ -31,6 +32,7 @@ from dep_freshness.registries import (
|
||||
gomod,
|
||||
maven,
|
||||
npmjs,
|
||||
pacman_repo,
|
||||
pub,
|
||||
pypi,
|
||||
)
|
||||
@ -46,6 +48,7 @@ _LOOKUP = {
|
||||
GITTAG: gittag.latest,
|
||||
MAVEN: maven.latest,
|
||||
GITCOMMIT: gitcommit.latest,
|
||||
PACMAN_REPO: pacman_repo.latest,
|
||||
}
|
||||
|
||||
|
||||
|
||||
135
dep_freshness/tests/test_pacman_lock.py
Normal file
135
dep_freshness/tests/test_pacman_lock.py
Normal file
@ -0,0 +1,135 @@
|
||||
"""A pacman-repo lock: parsed, discovered, and judged end to end.
|
||||
|
||||
The consumer is steam-game-installer's `steam_installer/data/proton-native.lock`,
|
||||
which pins one package out of the CachyOS repo. Pre-commit hands the gate the
|
||||
file by name, so `is_manifest` must accept it on content, not on a glob.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from dep_freshness import check, discover
|
||||
from dep_freshness.models import Severity
|
||||
from dep_freshness.parsers import pacman_lock
|
||||
from dep_freshness.registries import pacman_repo
|
||||
from dep_freshness.tests.conftest import write
|
||||
from dep_freshness.tests.test_pacman_repo import SHA, build_db
|
||||
|
||||
REPO = "https://mirror.cachyos.org/repo/x86_64/cachyos/cachyos.db"
|
||||
LOCK = f"""\
|
||||
# pacman-repo lock: newest stable package from an Arch-style repo db
|
||||
name=proton-cachyos-native
|
||||
repo={REPO}
|
||||
version=1:11.0.20261015-1
|
||||
sha256={SHA}
|
||||
"""
|
||||
LOCK_PATH = "steam_installer/data/proton-native.lock"
|
||||
|
||||
|
||||
def test_the_lock_parses_to_one_pinned_dep(repo):
|
||||
path = write(repo, LOCK_PATH, LOCK)
|
||||
[dep] = pacman_lock.parse(path)
|
||||
assert dep.ecosystem == "pacman-repo"
|
||||
assert pacman_repo.split_name(dep.name) == (REPO, "proton-cachyos-native")
|
||||
assert dep.pinned == "1:11.0.20261015-1"
|
||||
assert dep.locked == SHA
|
||||
assert dep.line == 4 # the version= line is what a bump edits
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("name", "body"),
|
||||
[
|
||||
("Cargo.lock", '[[package]]\nname = "serde"\nversion = "1.0.0"\n'),
|
||||
("other.lock", "name=x\nrepo=https://example.org/not-a-db.json\n"),
|
||||
("proton-native.txt", LOCK),
|
||||
],
|
||||
)
|
||||
def test_other_files_are_not_pacman_locks(repo, name, body):
|
||||
path = write(repo, name, body)
|
||||
assert not discover.is_manifest(path)
|
||||
assert discover.parse_manifest(path) == []
|
||||
|
||||
|
||||
def test_the_lock_is_discovered_by_content(repo):
|
||||
path = write(repo, LOCK_PATH, LOCK)
|
||||
assert discover.is_manifest(path)
|
||||
assert path in discover.find_manifests(repo)
|
||||
|
||||
|
||||
def test_a_lock_without_a_version_is_unpinned(repo):
|
||||
path = write(repo, "x.lock", LOCK.replace("version=1:11.0.20261015-1\n", ""))
|
||||
[dep] = pacman_lock.parse(path)
|
||||
assert dep.pinned is None
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def gate(repo, monkeypatch):
|
||||
"""Run the CLI on the lock, with the repo db answering `served`."""
|
||||
def invoke(lock_body, served, *flags):
|
||||
name, version, sha = served
|
||||
body = build_db({name: version}, sha=sha)
|
||||
monkeypatch.setattr(pacman_repo, "get_bytes", lambda _url: body)
|
||||
path = write(repo, LOCK_PATH, lock_body)
|
||||
monkeypatch.chdir(repo)
|
||||
return check.main([*flags, str(path)])
|
||||
return invoke
|
||||
|
||||
|
||||
CURRENT = ("proton-cachyos-native", "1:11.0.20261015-1", SHA)
|
||||
|
||||
|
||||
def test_a_current_lock_passes(gate):
|
||||
assert gate(LOCK, CURRENT, "--strict") == 0
|
||||
|
||||
|
||||
def test_a_lock_behind_the_repo_fails(gate, capsys):
|
||||
old = LOCK.replace("1:11.0.20261015-1", "1:10.0.20250101-1")
|
||||
assert gate(old, CURRENT) == 1
|
||||
out = capsys.readouterr()
|
||||
text = out.out + out.err
|
||||
assert "proton-cachyos-native" in text
|
||||
assert "1:11.0.20261015-1" in text
|
||||
assert SHA in text # the bump is copy-pasteable
|
||||
|
||||
|
||||
def test_an_epoch_beats_a_bigger_pkgver(gate):
|
||||
# PEP 440 would read `11.0` > `1:2.0` (or nothing at all); pacman says the
|
||||
# epoch wins, so a lock on 11.0 is behind a repo on 1:2.0.
|
||||
old = LOCK.replace("1:11.0.20261015-1", "11.0-1")
|
||||
assert gate(old, ("proton-cachyos-native", "1:2.0-1", SHA)) == 1
|
||||
|
||||
|
||||
def test_a_right_version_with_a_wrong_sha_is_a_mismatch(gate, capsys):
|
||||
assert gate(LOCK, (*CURRENT[:2], "cd" * 32), "--json") == 1
|
||||
payload = json.loads(capsys.readouterr().out)
|
||||
assert Severity.LOCK_MISMATCH.value in json.dumps(payload)
|
||||
|
||||
|
||||
def test_a_package_the_repo_dropped_is_undeterminable(gate):
|
||||
assert gate(LOCK, ("something-else", "1.0-1", SHA), "--strict") == 3
|
||||
|
||||
|
||||
def test_offline_with_a_cold_cache_is_undeterminable(repo, monkeypatch):
|
||||
from dep_freshness.registries import http
|
||||
|
||||
http.force_offline(True)
|
||||
path = write(repo, LOCK_PATH, LOCK)
|
||||
monkeypatch.chdir(repo)
|
||||
assert check.main(["--strict", str(path)]) == 3
|
||||
assert check.main([str(path)]) == 0 # pre-commit degrades, never corners
|
||||
|
||||
|
||||
def test_zstd_on_an_old_python_is_undeterminable_not_a_crash(
|
||||
repo, monkeypatch, capsys
|
||||
):
|
||||
body = build_db({"proton-cachyos-native": "1:11.0.20261015-1"}, "zst")
|
||||
monkeypatch.setattr(pacman_repo, "get_bytes", lambda _url: body)
|
||||
monkeypatch.setattr(pacman_repo, "_HAS_ZSTD", False)
|
||||
path = write(repo, LOCK_PATH, LOCK)
|
||||
monkeypatch.chdir(repo)
|
||||
assert check.main(["--strict", str(path)]) == 3
|
||||
out = capsys.readouterr()
|
||||
assert "compression.zstd" in out.out + out.err
|
||||
146
dep_freshness/tests/test_pacman_repo.py
Normal file
146
dep_freshness/tests/test_pacman_repo.py
Normal file
@ -0,0 +1,146 @@
|
||||
"""The pacman-repo adapter: pacman `vercmp` order and reading a repo `.db`.
|
||||
|
||||
Versions here are `epoch:pkgver-pkgrel`, which PEP 440 cannot read at all:
|
||||
`versions.behind` answers False for every pair, so a stale lock would read
|
||||
as current. The ordering tests below come from the vercmp(8) man page, and
|
||||
the differential test checks them against the real `/usr/bin/vercmp`.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import itertools
|
||||
import shutil
|
||||
import subprocess
|
||||
import tarfile
|
||||
|
||||
import pytest
|
||||
|
||||
from dep_freshness.registries import pacman_repo
|
||||
from dep_freshness.registries.http import Offline
|
||||
|
||||
REPO = "https://mirror.example.org/repo/x86_64/extra/extra.db"
|
||||
SHA = "ab" * 32
|
||||
|
||||
# vercmp(8): "1.0a < 1.0b < 1.0beta < 1.0p < 1.0pre < 1.0rc < 1.0 < 1.0.a < 1.0.1"
|
||||
MAN_ORDER = ["1.0a", "1.0b", "1.0beta", "1.0p", "1.0pre", "1.0rc", "1.0", "1.0.a",
|
||||
"1.0.1"]
|
||||
PAIRS = [
|
||||
("1:1.0", "2.0", 1),
|
||||
("0:2.0", "2.0", 0),
|
||||
("1:11.0.20261015-1", "1:10.0.20250101-1", 1),
|
||||
("1:11.0.20261015-1", "1:11.0.20261015-1", 0),
|
||||
("1.0-1", "1.0-2", -1),
|
||||
("1.0", "1.0-7", 0), # a missing pkgrel compares equal
|
||||
("1.0.0", "1.0", 1),
|
||||
("1.0_1", "1.0.1", 0), # any non-alphanumeric run is one separator
|
||||
("1.0", "1.0.0.0", -1),
|
||||
("2.0", "10.0", -1),
|
||||
("1.001", "1.1", 0),
|
||||
("r123", "r45", 1),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("left", "right"), list(itertools.pairwise(MAN_ORDER)))
|
||||
def test_vercmp_follows_the_man_page_order(left, right):
|
||||
assert pacman_repo.vercmp(left, right) == -1
|
||||
assert pacman_repo.vercmp(right, left) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("left", "right", "expected"), PAIRS)
|
||||
def test_vercmp_epoch_and_pkgrel(left, right, expected):
|
||||
assert pacman_repo.vercmp(left, right) == expected
|
||||
|
||||
|
||||
@pytest.mark.skipif(not shutil.which("vercmp"), reason="pacman's vercmp absent")
|
||||
@pytest.mark.parametrize(
|
||||
("left", "right"),
|
||||
[*itertools.pairwise(MAN_ORDER), *((a, b) for a, b, _ in PAIRS)],
|
||||
)
|
||||
def test_vercmp_agrees_with_pacman(left, right):
|
||||
real = subprocess.run(
|
||||
["vercmp", left, right], capture_output=True, text=True, check=True
|
||||
).stdout.strip()
|
||||
assert pacman_repo.vercmp(left, right) == int(real)
|
||||
|
||||
|
||||
def _desc(name: str, version: str, sha: str = SHA) -> bytes:
|
||||
return (
|
||||
f"%FILENAME%\n{name}-{version}-x86_64.pkg.tar.zst\n\n%NAME%\n{name}\n\n"
|
||||
f"%VERSION%\n{version}\n\n%SHA256SUM%\n{sha}\n\n"
|
||||
).encode()
|
||||
|
||||
|
||||
def build_db(
|
||||
packages: dict[str, str], compression: str = "gz", sha: str = SHA
|
||||
) -> bytes:
|
||||
"""A repo db the way repo-add writes it: one `<name>-<ver>/desc` per package."""
|
||||
raw = io.BytesIO()
|
||||
with tarfile.open(fileobj=raw, mode=f"w:{compression}") as tar:
|
||||
for name, version in packages.items():
|
||||
body = _desc(name, version, sha)
|
||||
info = tarfile.TarInfo(f"{name}-{version}/desc")
|
||||
info.size = len(body)
|
||||
tar.addfile(info, io.BytesIO(body))
|
||||
return raw.getvalue()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def serve(monkeypatch):
|
||||
"""Answer the next `get_bytes` with `body`, recording the URL asked for."""
|
||||
asked: list[str] = []
|
||||
|
||||
def install(body):
|
||||
def fake(url):
|
||||
asked.append(url)
|
||||
return body
|
||||
monkeypatch.setattr(pacman_repo, "get_bytes", fake)
|
||||
return asked
|
||||
return install
|
||||
|
||||
|
||||
@pytest.mark.parametrize("compression", ["gz", "zst"])
|
||||
def test_latest_reads_version_and_sha_from_the_db(serve, compression):
|
||||
asked = serve(build_db({"proton-native": "1:11.0-1", "zlib": "1.3-1"},
|
||||
compression))
|
||||
name = pacman_repo.qualified_name(REPO, "proton-native")
|
||||
assert pacman_repo.latest(name) == f"1:11.0-1 {SHA}"
|
||||
assert asked == [REPO]
|
||||
|
||||
|
||||
def test_a_sibling_sharing_the_name_prefix_is_not_the_package(serve):
|
||||
serve(build_db({"proton-native-v3": "9:9.0-1", "proton-native": "1:2.0-1"}))
|
||||
name = pacman_repo.qualified_name(REPO, "proton-native")
|
||||
assert pacman_repo.latest(name).split()[0] == "1:2.0-1"
|
||||
|
||||
|
||||
def test_a_package_the_db_does_not_list_is_none(serve):
|
||||
serve(build_db({"zlib": "1.3-1"}))
|
||||
assert pacman_repo.latest(pacman_repo.qualified_name(REPO, "nope")) is None
|
||||
|
||||
|
||||
def test_a_missing_db_is_none(serve):
|
||||
serve(None)
|
||||
assert pacman_repo.latest(pacman_repo.qualified_name(REPO, "x")) is None
|
||||
|
||||
|
||||
def test_a_db_that_is_not_a_tar_is_offline(serve):
|
||||
serve(b"<html>captive portal</html>")
|
||||
with pytest.raises(Offline):
|
||||
pacman_repo.latest(pacman_repo.qualified_name(REPO, "x"))
|
||||
|
||||
|
||||
def test_zstd_without_compression_zstd_names_the_missing_module(
|
||||
serve, monkeypatch
|
||||
):
|
||||
serve(build_db({"x": "1.0-1"}, "zst"))
|
||||
monkeypatch.setattr(pacman_repo, "_HAS_ZSTD", False)
|
||||
with pytest.raises(Offline, match="compression.zstd"):
|
||||
pacman_repo.latest(pacman_repo.qualified_name(REPO, "x"))
|
||||
|
||||
|
||||
def test_qualified_name_round_trips():
|
||||
assert pacman_repo.split_name(pacman_repo.qualified_name(REPO, "x")) == (
|
||||
REPO,
|
||||
"x",
|
||||
)
|
||||
Loading…
Reference in New Issue
Block a user