refactor: share the hash scrapers' sparse clone

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 06:01:53 +01:00
1 parent 91fe93c5a2
commit 9f8ac141c1
5 files changed
+145 -209

No files matched your search

+24 -15
View File
@@ -433,17 +433,19 @@ class TheScraperClonesTheReleaseItLabels(unittest.TestCase):
from scripts.scraper import mame_hash_scraper as scraper
from scripts.scraper import sparse_clone
calls: list[list[str]] = []
def git(args: list[str], cwd: object = None) -> str:
calls.append(args)
return "c" * 40 + "\n" if args[0] == "rev-parse" else ""
with mock.patch.object(scraper, "fetch_github_latest_version", return_value="mame0289"), \
mock.patch.object(scraper, "_run_git",
side_effect=lambda args, cwd=None: calls.append(args)), \
mock.patch.object(sparse_clone, "_git", side_effect=git), \
mock.patch.object(scraper, "parse_mame_source_tree",
return_value={"neogeo": {"roms": []}}), \
mock.patch.object(scraper, "_get_commit", return_value="c" * 40), \
mock.patch.object(scraper, "_write_cache"), \
mock.patch.object(scraper, "_cleanup"), \
mock.patch.object(scraper, "_load_cache", return_value={}), \
mock.patch.object(scraper, "_is_stale", return_value=True):
mock.patch.object(scraper, "_write_cache"):
data = scraper._fetch_hashes(force=True)
clone = next(args for args in calls if args[0] == "clone")
self.assertEqual(clone[clone.index("--branch") + 1], "mame0289")
@@ -806,22 +808,29 @@ class TestDerivativeVersion(unittest.TestCase):
class AnUnreadableCloneStopsTheScrape(unittest.TestCase):
"""_get_commit swallowed a failed rev-parse and returned "": cached for a
day, the merge then advanced core_version and the refs while
"""A failed rev-parse was swallowed and returned "": cached for a day,
the merge then advanced core_version and the refs while
upstream_commit stayed on the previous release."""
def test_no_empty_commit_reaches_the_cache(self):
import subprocess
from unittest import mock
from scripts.scraper import mame_hash_scraper as scraper
from scripts.scraper import sparse_clone
dest = Path(tempfile.gettempdir()) / "retrobios-no-clone"
failing = subprocess.CalledProcessError(128, ["git", "rev-parse", "HEAD"])
with mock.patch.object(scraper, "_run_git", side_effect=failing), self.assertRaises(RuntimeError):
scraper._get_commit()
empty = subprocess.CompletedProcess(["git"], 0, stdout="\n", stderr="")
with mock.patch.object(scraper, "_run_git", return_value=empty), self.assertRaises(RuntimeError):
scraper._get_commit()
def git_failing(args: list[str], cwd: object = None) -> str:
if args[0] == "rev-parse":
raise failing
return ""
for git in (git_failing, lambda args, cwd=None: "\n" if args[0] == "rev-parse" else ""):
with mock.patch.object(sparse_clone, "_git", side_effect=git), \
mock.patch.object(sparse_clone, "remove_clone"), \
self.assertRaises(RuntimeError):
sparse_clone.sparse_clone("https://x/y.git", dest, ["src"])
+17 -19
View File
@@ -1137,18 +1137,11 @@ class HashScraperRefusesAnEmptyRead(unittest.TestCase):
cache = Path(tmp) / "mame-hashes.json"
with mock.patch.object(m, "_CACHE_PATH", cache), \
mock.patch.object(m, "fetch_github_latest_version", return_value="mame0289"), \
mock.patch.object(m, "_sparse_clone"), \
mock.patch.object(m, "_cleanup"), \
mock.patch.object(m, "_get_commit", return_value="abc"), \
mock.patch.object(m, "_sparse_clone", return_value="abc"), \
mock.patch.object(m, "parse_mame_source_tree", return_value={}), self.assertRaises(RuntimeError):
m._fetch_hashes(force=True)
self.assertFalse(cache.exists())
def test_mame_does_not_serve_an_empty_cache(self):
from scraper import mame_hash_scraper as m
self.assertTrue(m._is_stale(self._fresh({})))
self.assertFalse(m._is_stale(self._fresh({"neogeo": {}})))
def test_fbneo_raises_and_writes_no_cache(self):
from scraper import fbneo_hash_scraper as f
@@ -1156,23 +1149,28 @@ class HashScraperRefusesAnEmptyRead(unittest.TestCase):
with tempfile.TemporaryDirectory() as tmp:
cache = Path(tmp) / "fbneo-hashes.json"
with mock.patch.object(f, "CACHE_PATH", cache), \
mock.patch.object(f, "_sparse_clone"), \
mock.patch.object(f, "_cleanup"), \
mock.patch.object(f, "_extract_version", return_value=("v1.0.0.03", "abc")), \
mock.patch.object(f, "sparse_clone", return_value="abc"), \
mock.patch.object(f, "_extract_version", return_value="v1.0.0.03"), \
mock.patch.object(f, "parse_fbneo_source_tree", return_value={}), self.assertRaises(RuntimeError):
f.fetch_and_cache(force=True)
self.assertFalse(cache.exists())
def test_fbneo_does_not_serve_an_empty_cache(self):
from scraper import fbneo_hash_scraper as f
def test_an_empty_or_unreadable_cache_is_not_served(self):
from scraper.sparse_clone import fresh_cache
with tempfile.TemporaryDirectory() as tmp:
cache = Path(tmp) / "fbneo-hashes.json"
with mock.patch.object(f, "CACHE_PATH", cache):
cache.write_text(json.dumps(self._fresh({})))
self.assertFalse(f._is_cache_fresh())
cache.write_text(json.dumps(self._fresh({"neogeo": {}})))
self.assertTrue(f._is_cache_fresh())
cache = Path(tmp) / "hashes.json"
self.assertIsNone(fresh_cache(cache))
cache.write_text(json.dumps(self._fresh({})))
self.assertIsNone(fresh_cache(cache))
cache.write_text(json.dumps(self._fresh({"neogeo": {}})))
self.assertEqual(fresh_cache(cache)["commit"], "abc")
# A timestamp without a time zone crashed the FBNeo reader.
cache.write_text(json.dumps({"fetched_at": "2026-11-20T00:00:00",
"bios_sets": {"neogeo": {}}}))
self.assertIsNone(fresh_cache(cache))
cache.write_text("{")
self.assertIsNone(fresh_cache(cache))