fix: guard target lists with board images

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 21:46:27 +01:00
1 parent 2b5955e8a5
commit 7222207f3b
3 files changed
+44 -1

No files matched your search

+2 -1
View File
@@ -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
+12
View File
@@ -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."""
+30
View File
@@ -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()