"""Native exporters for list formats: Recalbox es_bios.xml, the RomM fixture, System.dat, BizHawk, RetroBat and the MiSTer database. """ from __future__ import annotations import tests import difflib import io import json import tempfile import unittest import xml.etree.ElementTree as ET import zipfile from collections import OrderedDict from pathlib import Path from model.platforms import load_platform_config, native_layer from exporter import discover_exporters from exporter.landed import landed_changes from resolve.reconcile import build_native_model from tests.exporterssupport import REPO_ROOT, SCRAPED, TRUTH, UPSTREAM_CACHE, model class RecalboxExport(unittest.TestCase): def _render(self): systems, report = model() exporter = discover_exporters()["recalbox"]() return exporter, systems, exporter.render(systems, report, {}, None) def test_output_is_well_formed_xml_in_recalbox_shape(self): _, _, produced = self._render() root = ET.fromstring(produced["es_bios.xml"]) self.assertEqual(root.tag, "biosList") self.assertTrue(list(root.iter("system"))) def test_every_element_carries_what_the_schema_requires(self): """es_bios.xsd makes path, md5 and core required on every bios.""" _, _, produced = self._render() root = ET.fromstring(produced["es_bios.xml"]) for element in root.iter("bios"): for attribute in ("path", "md5", "core"): self.assertTrue(element.get(attribute), attribute) def test_mandatory_is_only_stated_when_it_is_false(self): _, _, produced = self._render() root = ET.fromstring(produced["es_bios.xml"]) by_path = {e.get("path"): e for e in root.iter("bios")} self.assertIsNone(by_path["psx/scph5501.bin"].get("mandatory")) self.assertEqual(by_path["psx/scph7001.bin"].get("mandatory"), "false") def test_hash_match_mandatory_does_not_follow_mandatory(self): """They are separate axes: Recalbox uses all four combinations.""" _, _, produced = self._render() root = ET.fromstring(produced["es_bios.xml"]) optional = next( e for e in root.iter("bios") if e.get("mandatory") == "false" ) self.assertIsNone(optional.get("hashMatchMandatory")) def test_the_export_passes_its_own_validator(self): exporter, systems, produced = self._render() self.assertEqual(exporter.validate(systems, produced), []) def test_an_addition_goes_where_the_core_reads_it(self): """DuckStation reads scph5000.bin at the BIOS root, but the export wrote psx/scph5000.bin, a path no core opens.""" import copy truth = copy.deepcopy(TRUTH) truth["systems"]["sony-playstation"]["files"].append({ "name": "scph5000.bin", "md5": "57a06303dfa9cf9351222dfcbb4a29d9", "size": 524288, "required": False, "_cores": ["duckstation"], }) systems, report = build_native_model(truth, SCRAPED) produced = discover_exporters()["recalbox"]().render(systems, report, {}, None) paths = {e.get("path") for e in ET.fromstring(produced["es_bios.xml"]).iter("bios")} self.assertIn("scph5000.bin", paths) self.assertNotIn("psx/scph5000.bin", paths) def test_two_blocks_of_one_platform_stay_two(self): """Recalbox files Pentagon and Scorpion under zxspectrum, apart. Merged, the Scorpion ROMs sat under the Pentagon name and the BIOS manager lost a section, and the line the maintainer commented out went with the rewrite. """ old, new = "1" * 32, "2" * 32 def declared(name, label, md5): return { "name": name, "destination": f"fuse/{name}", "native_system": "zxspectrum", "native_name": label, "native_path": f"fuse/{name}", "md5": md5, "core": "libretro/fuse", "required": False, } pentagon = "Pentagon 128/512/1024 (ZX Spectrum clone)" scorpion = "Scorpion 256K (ZX Spectrum clone)" scraped = {"systems": {"sinclair-zxspectrum": { "native_id": "zxspectrum", "files": [ declared("128p-0.rom", pentagon, "3" * 32), declared("256s-0.rom", scorpion, old), ], }}} truth = {"systems": {"sinclair-zxspectrum": {"files": [{ "name": "256s-0.rom", "path": "fuse/256s-0.rom", "md5": new, "required": False, "_cores": ["fuse"], }]}}} original = ( '\n\n' f' \n' f' \n' ' \n' " \n" f' \n' f' \n' " \n\n" ) systems, report = build_native_model(truth, scraped) produced = discover_exporters()["recalbox"]().render( systems, report, {"es_bios.xml": original}, None )["es_bios.xml"] self.assertIn('