utils/dep_freshness/tests/test_http.py
Krzysztof kuhy Rudnicki a4ea305164 dep_freshness: Gradle ecosystem -- version catalogs, wrapper, Maven registries
The gate had no way to read a Gradle build, which left the two Kotlin
forks (tachiyomisy, syncyomi-src) outside the fleet-wide rule.

- parsers/gradle.py reads gradle/libs.versions.toml ([libraries] and
  [plugins] in every shape: inline string, module + version.ref,
  group/name/version, rich {strictly|require|prefer} tables) into
  `maven:<group>:<artifact>` deps, pointing the fix at the [versions]
  line when a ref is used; BOM-managed entries without a version are
  skipped. gradle-wrapper.properties becomes `toolchain:gradle`.
- registries/maven.py asks Google Maven, Central and the plugin portal
  in that order, reading the full <versions> list -- AndroidX writes
  alphas into <latest>. Plugins resolve through their marker artifact.
- registries/toolchain.gradle_latest reads services.gradle.org.
- http.get_text shares the transport with get_json (XML bodies).
- Templates: the pre-commit `files:` and the CI cache key know the
  catalog. 100% branch on every new file; fixtures only.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Yc4TiPjhbQLfH35QBPfrbP
2026-09-12 19:10:42 +02:00

132 lines
4.0 KiB
Python

"""The network layer: one probe per host, and failures that never raise out.
The gate is called from a pre-commit hook the user may not bypass, so every
transport error has to arrive as `Offline` -- something the caller can answer
with a cached value -- rather than as a traceback.
"""
from __future__ import annotations
import io
import socket
from urllib.error import HTTPError, URLError
import pytest
from dep_freshness.registries import http
@pytest.fixture(autouse=True)
def real_probe(monkeypatch):
"""Undo conftest's blanket `host_reachable` stub for this module."""
monkeypatch.undo()
http.reset_probes()
http.force_offline(False)
yield
http.reset_probes()
http.force_offline(False)
def test_the_probe_is_made_once_per_host(monkeypatch):
calls = []
def fake_connect(address, timeout=None):
calls.append(address)
return io.BytesIO()
monkeypatch.setattr(socket, "create_connection", fake_connect)
assert http.host_reachable("https://pub.dev/a")
assert http.host_reachable("https://pub.dev/b")
assert len(calls) == 1
assert calls[0] == ("pub.dev", 443)
def test_an_unreachable_host_is_remembered_as_unreachable(monkeypatch):
monkeypatch.setattr(socket, "create_connection", _refuse)
assert not http.host_reachable("https://nowhere.invalid/x")
def test_force_offline_short_circuits_even_a_known_good_host(monkeypatch):
monkeypatch.setattr(socket, "create_connection", lambda *a, **k: io.BytesIO())
assert http.host_reachable("https://pub.dev/a")
http.force_offline(True)
assert not http.host_reachable("https://pub.dev/a")
def test_get_json_raises_offline_when_the_host_is_down(monkeypatch):
monkeypatch.setattr(socket, "create_connection", _refuse)
with pytest.raises(http.Offline):
http.get_json("https://nowhere.invalid/x")
def test_a_404_means_the_package_does_not_exist(monkeypatch):
_reachable(monkeypatch)
monkeypatch.setattr(http, "urlopen", _raiser(
HTTPError("u", 404, "Not Found", {}, None)))
assert http.get_json("https://pub.dev/api/packages/nope") is None
def test_a_500_degrades_to_offline_rather_than_a_missing_package(monkeypatch):
_reachable(monkeypatch)
monkeypatch.setattr(http, "urlopen", _raiser(
HTTPError("u", 500, "Boom", {}, None)))
with pytest.raises(http.Offline):
http.get_json("https://pub.dev/api/packages/x")
def test_a_transport_error_degrades_to_offline(monkeypatch):
_reachable(monkeypatch)
monkeypatch.setattr(http, "urlopen", _raiser(URLError("reset")))
with pytest.raises(http.Offline):
http.get_json("https://pub.dev/api/packages/x")
def test_a_successful_body_is_decoded(monkeypatch):
_reachable(monkeypatch)
monkeypatch.setattr(http, "urlopen", lambda *a, **k: _Response(b'{"a": 1}'))
assert http.get_json("https://pub.dev/x", accept="application/json") == {"a": 1}
def test_undecodable_json_degrades_to_offline(monkeypatch):
_reachable(monkeypatch)
monkeypatch.setattr(http, "urlopen", lambda *a, **k: _Response(b"not json"))
with pytest.raises(http.Offline):
http.get_json("https://pub.dev/x")
def _refuse(*_args, **_kwargs):
raise OSError("connection refused")
def _reachable(monkeypatch):
monkeypatch.setattr(http, "host_reachable", lambda _url: True)
def _raiser(error):
def raise_it(*_args, **_kwargs):
raise error
return raise_it
class _Response:
def __init__(self, body: bytes) -> None:
self._body = body
def read(self) -> bytes:
return self._body
def __enter__(self):
return self
def __exit__(self, *_exc):
return False
def test_get_text_returns_the_raw_body_and_none_on_404(monkeypatch):
_reachable(monkeypatch)
monkeypatch.setattr(http, "urlopen", lambda *a, **k: _Response(b"<x/>"))
assert http.get_text("https://repo1.maven.org/x") == "<x/>"
monkeypatch.setattr(http, "urlopen", _raiser(
HTTPError("u", 404, "Not Found", {}, None)))
assert http.get_text("https://repo1.maven.org/x") is None