Files
libretro/tests/test_shared_groups.py
T

97 lines
4.3 KiB
Python

"""A shared group adds files, never a second name for a native one.
The quasi88 group declared N88SUB.ROM beside the disk.rom System.dat
already names for nec-pc-88, same bytes: the RetroArch pack carried the
2 KB ROM twice under quasi88/, and the group read as twelve required files
where four did the work. Alternative names are aliases in the profile.
"""
from __future__ import annotations
import sys
import unittest
from pathlib import Path
import yaml
REPO_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(REPO_ROOT / "scripts"))
from common import list_registered_platforms, load_platform_config # noqa: E402
PLATFORMS = str(REPO_ROOT / "platforms")
def _md5s(entry: dict) -> set[str]:
raw = entry.get("md5") or ""
values = raw if isinstance(raw, list) else str(raw).split(",")
return {v.strip().lower() for v in values if v and v.strip()}
class SharedGroupsAddOnly(unittest.TestCase):
def test_no_shared_file_repeats_a_native_file_of_the_same_directory(self):
shared = yaml.safe_load((REPO_ROOT / "platforms" / "_shared.yml").read_text())
groups = shared.get("shared_groups") or {}
repeated = []
for platform in list_registered_platforms(PLATFORMS, include_archived=True):
config = load_platform_config(platform, PLATFORMS)
for system in config.get("systems", {}).values():
included = [
fe for group in system.get("includes", []) for fe in groups.get(group, [])
]
if not included:
continue
group_keys = {
(fe.get("name"), fe.get("destination", fe.get("name"))) for fe in included
}
native: dict[tuple[str, str], str] = {}
for fe in system.get("files", []):
if (fe.get("name"), fe.get("destination", fe.get("name"))) in group_keys:
continue
dest = fe.get("destination") or fe.get("name", "")
directory = dest.rsplit("/", 1)[0] if "/" in dest else ""
for md5 in _md5s(fe):
native.setdefault((directory, md5), fe.get("name", ""))
for fe in included:
dest = fe.get("destination") or fe.get("name", "")
directory = dest.rsplit("/", 1)[0] if "/" in dest else ""
for md5 in _md5s(fe):
other = native.get((directory, md5))
if other and other.lower() != fe.get("name", "").lower():
repeated.append(f"{platform}:{fe['name']} = {other}")
self.assertEqual(repeated, [])
class ADeclaredAliasSettlesTheRequirement(unittest.TestCase):
"""quasi88 reads n88sub.rom or disk.rom; System.dat names disk.rom. The
gap pass compared the entry's name alone and the pack carried the same
2 KB ROM a second time as quasi88/n88sub.rom."""
def test_a_platform_naming_an_alias_has_met_the_entry(self):
import os # noqa: PLC0415
import tempfile # noqa: PLC0415
from common import _emulator_profiles_cache # noqa: PLC0415
from verify import find_undeclared_files # noqa: PLC0415
with tempfile.TemporaryDirectory() as tmp:
Path(tmp, "emulators").mkdir()
Path(tmp, "emulators", "quasi88.yml").write_text(
"emulator: QUASI88\ntype: libretro\nsystems: [nec-pc-88]\ncores: [quasi88]\n"
"files:\n - name: n88sub.rom\n aliases: [N88SUB.ROM, disk.rom]\n"
" required: true\n"
)
_emulator_profiles_cache.clear()
self.addCleanup(_emulator_profiles_cache.clear)
config = {"platform": "P", "verification_mode": "existence", "cores": "all_libretro",
"systems": {"nec-pc-88": {"files": [{"name": "disk.rom", "destination": "quasi88/disk.rom"}]}}}
db = {"files": {}, "indexes": {"by_name": {}, "by_md5": {}, "by_crc32": {}, "by_path_suffix": {}}}
previous = os.getcwd()
os.chdir(tmp)
self.addCleanup(os.chdir, previous)
undeclared = find_undeclared_files(config, "emulators", db, data_names=set())
self.assertEqual([u["name"] for u in undeclared], [])
if __name__ == "__main__":
unittest.main()