Files
libretro/tests/test_retrodeck_scraper_errors.py
T
Abdessamad Derraz f61c2cb7d4 refactor: one network layer for the scrapers
Each network job had several copies. One is kept per job and
every caller uses it.

- Source revision: model.sourcerevision (source_revision, required_revision,
  platform_revision) replaces BaseTargetScraper's pattern list, the
  exporter's _REVISION, the four launcher_revision copies and RetroBat's
  own YAML reader. It reads the union of the shapes: GitHub raw, blob and
  tree with or without a path after the revision, GitLab raw, blob, tree
  and archive, codeload tar.gz and zip. The target scrapers now answer
  for RetroDECK, NextUI, MinUI and MyMinUI, whose tree URLs end at the
  commit, and the exporter reads GitLab blob and tree and codeload zip.
  EmuDeck and RetroBat still require a 40-hex commit, and each launcher
  reader requires its own repository in the URL.
- Latest release: fetch_github_latest_version, type-checked, is the only
  lookup. mame_hash_scraper, romset_dat_importer and the Lakka target
  scraper call it. romset_dat_importer's NoReleaseError is gone: a
  release without a tag raises RuntimeError.
- Tree listing: github_tree and tree_entries refuse a truncated or
  unreadable listing. The libretro and core-info scrapers share
  core_info_files and fetch_info_file, with libretro's refusal of an empty
  listing. The core-info listing and the RetroDECK, ROCKNIX launcher and
  libretro listings were not checked for truncation before.
- GET: fetch_bytes is the one request path. fetch_text, fetch_present
  (None on 404 only), github_json and download are built on it, so every
  read retries 429 and 5xx, caps the body and raises FetchError, a
  ConnectionError carrying the HTTP status. The target scrapers' own
  fetch_text raised RuntimeError: they now raise FetchError, and an
  optional Batocera board include is skipped on 404 only, not on any
  failure. The exporter's originals, MiSTer, RetroPie, redump and the
  buildbot index go through it too.
- Headers: core.upstream.forge_headers is the one builder. The token goes
  to api.github.com and raw.githubusercontent.com, the host compared
  whole, as Bearer. A URL that only names api.github.com in its query
  got the scrapers' token before, and profile_sync sent its token to
  github.com, whose release downloads redirect to asset storage with the
  header. Scraper reads of raw files now carry the token.
- Submodule commit: submodule_commit checks that the path is a
  submodule. Batocera's target scraper accepted any contents answer with
  a sha, a directory's tree sha included.
- Download: download streams through a scratch file renamed into place,
  retries like fetch_bytes, can keep an existing file and returns the
  Content-Disposition. It replaces the _download of romset_dat_importer
  and game_dat_importer.
2026-11-20 20:02:15 +01:00

87 lines
2.7 KiB
Python

"""A component manifest that could not be read is not a missing one.
The RetroDECK scraper printed "skip" on any HTTP or network error and wrote
retrodeck.yml without that component: a 503 on pcsx2's manifest removed the
PS2 BIOS and the pcsx2 standalone core, exit code 0.
"""
from __future__ import annotations
import tests
import io
import json
import unittest
import urllib.error
from unittest import mock
from scripts.scraper.retrodeck_scraper import Scraper
TREE = {"tree": [{"path": "pcsx2", "type": "tree"}, {"path": "ppsspp", "type": "tree"}]}
class _Response(io.BytesIO):
def __enter__(self):
return self
def __exit__(self, *exc):
return False
def _opener(failure):
def urlopen(req, timeout=0):
url = req.full_url
if "/commits/" in url:
return _Response(json.dumps({"sha": "a" * 40}).encode())
if "api.github.com" in url:
return _Response(json.dumps(TREE).encode())
if "pcsx2" in url and failure is not None:
raise failure
return _Response(json.dumps({"ppsspp": {"bios": []}}).encode())
return urlopen
class ManifestErrors(unittest.TestCase):
def fetch(self, failure):
# A 503 is retried after a wait: the waits are skipped, not the retries.
with mock.patch("urllib.request.urlopen", _opener(failure)), \
mock.patch("time.sleep"):
return Scraper()._fetch_remote_manifests()
def test_a_server_error_stops_the_scrape(self):
error = urllib.error.HTTPError("u", 503, "unavailable", {}, None)
with self.assertRaises(ConnectionError):
self.fetch(error)
def test_a_dropped_connection_stops_the_scrape(self):
with self.assertRaises(ConnectionError):
self.fetch(urllib.error.URLError("reset"))
def test_a_404_is_a_component_without_a_manifest(self):
error = urllib.error.HTTPError("u", 404, "not found", {}, None)
self.assertEqual([name for name, _ in self.fetch(error)], ["ppsspp"])
def test_the_tree_and_files_are_read_at_one_commit(self):
seen = []
opener = _opener(None)
def recording(req, timeout=0):
seen.append(req.full_url)
return opener(req, timeout)
with mock.patch("urllib.request.urlopen", recording):
Scraper()._fetch_remote_manifests()
reads = [url for url in seen if "/commits/" not in url]
self.assertTrue(reads)
self.assertTrue(all("a" * 40 in url for url in reads), reads)
def test_a_truncated_tree_stops_the_scrape(self):
TREE["truncated"] = True
self.addCleanup(TREE.pop, "truncated")
with self.assertRaises(RuntimeError):
self.fetch(None)
if __name__ == "__main__":
unittest.main()