From 7222207f3b9a8476eec2af1aa2179c5e532ee010 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 21:46:27 +0100 Subject: [PATCH] fix: guard target lists with board images --- scripts/packs/outputdir.py | 3 ++- scripts/packs/targets.py | 12 ++++++++++++ tests/test_target_names.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/scripts/packs/outputdir.py b/scripts/packs/outputdir.py index 20fff0a15..64b40f5a2 100644 --- a/scripts/packs/outputdir.py +++ b/scripts/packs/outputdir.py @@ -9,6 +9,7 @@ from collections.abc import Iterator from pathlib import Path from core.artifacts import artifact_lock, repository_output_lock +from packs.targets import is_target_list def json_artifact_kind(path: Path) -> str | None: @@ -23,7 +24,7 @@ def json_artifact_kind(path: Path) -> str | None: return None if "manifest_version" in data: return "manifest" - if all(v is None or isinstance(v, list) for v in data.values()): + if is_target_list(data): return "targets" return None diff --git a/scripts/packs/targets.py b/scripts/packs/targets.py index 123f746c1..9cb706b72 100644 --- a/scripts/packs/targets.py +++ b/scripts/packs/targets.py @@ -29,6 +29,18 @@ def generate_target_manifests(targets_dir: str, output_dir: str) -> list[tuple[s return written +def is_target_list(data: object) -> bool: + """Whether a decoded JSON document has the shape target_list writes: + each target a core list, None, or its cores beside its board images.""" + return isinstance(data, dict) and bool(data) and all( + held is None or isinstance(held, list) or ( + isinstance(held, dict) and set(held) == {"cores", "images"} + and isinstance(held["cores"], list) and isinstance(held["images"], list) + ) + for held in data.values() + ) + + def target_list(yml_file: Path) -> dict[str, list[str] | dict | None]: """A platform's cores per hardware target, as install/targets/ publishes them and a pack carries them: aliases included, overrides applied.""" diff --git a/tests/test_target_names.py b/tests/test_target_names.py index 3740ff1ff..ef6f8207d 100644 --- a/tests/test_target_names.py +++ b/tests/test_target_names.py @@ -266,5 +266,35 @@ class AnEmptyOverrideIsNoOverride(unittest.TestCase): self.assertEqual(canonical_target_name(["p", "q"], "board", tmp), "board") +class TheOutputGuardKnowsEveryTargetShape(unittest.TestCase): + """A board with images is written {"cores", "images"}: the guard read + only lists, and --manifest replaced install/targets/batocera.json.""" + + def test_a_list_with_board_images_is_refused_to_a_manifest_run(self): + import contextlib + import io + import json + + from packs.outputdir import holds_other_kind, json_artifact_kind + from packs.targets import generate_target_manifests + + with tempfile.TemporaryDirectory(dir=REPO_ROOT / "tmp") as tmp: + targets = Path(tmp) / "targets" + targets.mkdir() + (targets / "p.yml").write_text( + "targets:\n board:\n cores: [a]\n images: [board-x]\n", + encoding="utf-8", + ) + out = Path(tmp) / "out" + generate_target_manifests(str(targets), str(out)) + written = out / "p.json" + self.assertEqual(json.loads(written.read_text(encoding="utf-8")), + {"board": {"cores": ["a"], "images": ["board-x"]}}) + self.assertEqual(json_artifact_kind(written), "targets") + with contextlib.redirect_stderr(io.StringIO()) as err: + self.assertTrue(holds_other_kind(str(out), "manifest")) + self.assertIn("p.json", err.getvalue()) + + if __name__ == "__main__": unittest.main()