Files
libretro/tests/test_truth_data_dir_readers.py
T

139 lines
5.7 KiB
Python

"""A core that reads a file through its data directory is one of its readers.
LRPS2 reads pcsx2/resources/GameIndex.yaml from the tree its data directory
reference names and declares no file entry for it. The truth entry other
cores build for that path named armsx2 and yaps2 alone, so the export paired
RetroArch's System.dat line, which describes LRPS2's copy, with armsx2's
revision, and hash_conflict did not see LRPS2's value.
"""
from __future__ import annotations
import tests
import hashlib
import os
import tempfile
import unittest
import unittest.mock
from pathlib import Path
from resolve.nativemodel import checked_hashes
from resolve.truth import generate_platform_truth
REPO_ROOT = Path(__file__).resolve().parent.parent
GAME_INDEX = "pcsx2/resources/GameIndex.yaml"
DECLARED = b"the revision armsx2 declares"
TREE_COPY = b"the revision the tree holds"
def _profile(name: str, **fields) -> dict:
return {"emulator": name, "type": "libretro", "systems": ["sony-playstation-2"], **fields}
class DataDirectoryReaders(unittest.TestCase):
def setUp(self):
self._tmp = tempfile.TemporaryDirectory()
self.addCleanup(self._tmp.cleanup)
root = Path(self._tmp.name)
for tree, content in (("lrps2", TREE_COPY), ("resources", TREE_COPY)):
relative = GAME_INDEX if tree == "lrps2" else "GameIndex.yaml"
copy = root / tree / relative
copy.parent.mkdir(parents=True)
copy.write_bytes(content)
self.registry = {
"lrps2": {"local_cache": str(root / "lrps2")},
"resources": {"local_cache": str(root / "resources")},
}
self.profiles = {
"armsx2": _profile("armsx2", files=[{
"name": "GameIndex.yaml", "path": GAME_INDEX, "system": "sony-playstation-2",
"sha1": hashlib.sha1(DECLARED).hexdigest(),
}]),
"lrps2": _profile("lrps2", data_directories=[
{"ref": "lrps2", "destination": ""},
], files=[{"name": "<bios>.bin", "path": "pcsx2/bios/<bios>.bin"}]),
# Names what it reads in the tree, patches.zip alone: the tree
# adds no reader of GameIndex.yaml (pcee2 compiles it in).
"listed": _profile("listed", data_directories=[
{"ref": "resources", "destination": "pcsx2/resources"},
], files=[{"name": "patches.zip", "path": "pcsx2/resources/patches.zip"}]),
}
self.config = {"cores": ["armsx2", "lrps2", "listed"],
"systems": {"sony-playstation-2": {"files": []}}}
def _entry(self, registry: dict) -> dict:
model = generate_platform_truth(
"plat", self.config, {}, self.profiles, data_registry=registry,
)
return next(
f for f in model["systems"]["sony-playstation-2"]["files"]
if f["name"] == "GameIndex.yaml"
)
def test_the_reader_counts_and_its_copy_conflicts(self):
entry = self._entry(self.registry)
self.assertEqual(entry["_cores"], ["armsx2", "lrps2"])
self.assertIn("sha1", entry["hash_conflict"])
# The kept value describes one revision among those the cores read:
# it pairs a line, it never corrects one.
self.assertNotIn("sha1", checked_hashes(entry))
def test_without_the_cache_nothing_is_folded(self):
entry = self._entry({})
self.assertEqual(entry["_cores"], ["armsx2"])
self.assertNotIn("hash_conflict", entry)
def test_the_same_copy_adds_a_reader_and_no_conflict(self):
tree_copy = Path(self.registry["lrps2"]["local_cache"]) / GAME_INDEX
tree_copy.write_bytes(DECLARED)
entry = self._entry(self.registry)
self.assertEqual(entry["_cores"], ["armsx2", "lrps2"])
self.assertNotIn("hash_conflict", entry)
def test_a_bundled_tree_brings_the_file_as_a_bundled_file_does(self):
"""The tree read installed_on alone: a tree the emulator ships
(bundled: true) counted its reader among those the platform must
supply, so the entry asked for a file the emulator installs."""
self.profiles["armsx2"]["files"][0]["bundled"] = True
self.profiles["lrps2"]["data_directories"][0]["bundled"] = True
entry = self._entry(self.registry)
self.assertEqual(entry["_cores"], ["armsx2", "lrps2"])
self.assertEqual(entry["_brought_by"], [])
self.assertTrue(entry.get("bundled"))
self.profiles["lrps2"]["data_directories"][0]["not_bundled_on"] = ["plat"]
self.assertEqual(self._entry(self.registry)["_brought_by"], ["lrps2:libretro"])
def test_the_gaps_page_hands_truth_its_own_registry(self):
"""The divergence table folded the checked-out repository's
_data_dirs.yml under generate_site --platforms-dir."""
from sitegen import gaps
coverages = {"plat": {"platform": "Plat", "config": self.config}}
rows = gaps._divergence_rows(
coverages, gaps.PlatformRegistries({}, self.registry), self.profiles, {},
)
self.assertEqual(len(rows), 1)
seen: list[dict] = []
def truth(*args, **kwargs):
seen.append(kwargs["data_registry"])
return generate_platform_truth(*args, **kwargs)
with unittest.mock.patch("resolve.truth.generate_platform_truth", truth):
gaps._divergence_rows(
coverages, gaps.PlatformRegistries({}, self.registry), self.profiles, {},
)
self.assertEqual(seen, [self.registry])
def test_truth_reads_no_registry_of_its_own(self):
with self.assertRaises(TypeError):
generate_platform_truth("plat", self.config, {}, self.profiles) # type: ignore[call-arg]
if __name__ == "__main__":
os.environ.setdefault("RETROBIOS_OFFLINE", "1")
unittest.main()