From 6451ea13b490ea72e36370d5484f92bbec83c130 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sun, 22 Nov 2026 03:24:04 +0100 Subject: [PATCH] fix: hold the data trees inside build_export --- scripts/export_native.py | 26 +++++------- scripts/exporter/native.py | 18 ++++++--- tests/test_audit_regressions.py | 4 +- tests/test_export_report.py | 71 +++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 24 deletions(-) diff --git a/scripts/export_native.py b/scripts/export_native.py index 4d443df5f..3aef42496 100644 --- a/scripts/export_native.py +++ b/scripts/export_native.py @@ -16,7 +16,6 @@ Usage: from __future__ import annotations import argparse -import contextlib import sys from pathlib import Path @@ -27,8 +26,6 @@ from core.paths import TRUTH_DIR from exporter.originals import DEFAULT_CACHE from exporter.runner import run from model.cliargs import platform_arg, selected_platforms -from model.registry import load_data_dir_registry -from packs.datadirs import hold_data_dirs def main() -> None: @@ -69,20 +66,15 @@ def main() -> None: platforms = selected_platforms(args) - with contextlib.ExitStack() as held: - if not args.refresh_cache: - # Removal candidates are proved against data/: a refresh swapping - # a tree mid-walk listed entries only that tree holds as removable. - hold_data_dirs(held, load_data_dir_registry(args.platforms_dir)) - code = run( - platforms, - args.truth_dir, - args.output_dir, - args.platforms_dir, - args.upstream_dir, - args.fetch, - args.refresh_cache, - ) + code = run( + platforms, + args.truth_dir, + args.output_dir, + args.platforms_dir, + args.upstream_dir, + args.fetch, + args.refresh_cache, + ) sys.exit(code) diff --git a/scripts/exporter/native.py b/scripts/exporter/native.py index 21aa33165..1dbee0769 100644 --- a/scripts/exporter/native.py +++ b/scripts/exporter/native.py @@ -5,6 +5,7 @@ file from the platform's original, and a report lists every change.""" from __future__ import annotations +import contextlib import functools import json from dataclasses import dataclass, field @@ -26,6 +27,7 @@ from model.platforms import load_platform_config, native_layer from model.profiles import load_emulator_profiles from model.registry import load_data_dir_registry, load_platform_registry from model.safeparse import yaml_load +from packs.datadirs import hold_data_dirs from resolve.categories import removal_candidates from resolve.reconcile import Report, build_native_model from resolve.truth import generate_platform_truth @@ -256,12 +258,16 @@ def build_export( summary = _landed_summary(counts) withheld = exporter.withheld() unwritten = _unwritten(refused, withheld, report) - # Reported, never removed: the platform's file keeps every entry. - candidates = removal_candidates( - systems, scraped, profiles, load_data_dir_registry(platforms_dir), - ) - # Reported, never rewritten: both declarations stay as the platform wrote them. - doubled = doubled_in(scraped, platforms_dir) + data_registry = load_data_dir_registry(platforms_dir) + with contextlib.ExitStack() as held: + # Both are proved against data/: a refresh swapping a tree mid-walk + # listed entries only that tree holds as removable. Held here, every + # caller gets the guarantee, the freshness check included. + hold_data_dirs(held, data_registry) + # Reported, never removed: the platform's file keeps every entry. + candidates = removal_candidates(systems, scraped, profiles, data_registry) + # Reported, never rewritten: both declarations stay as the platform wrote them. + doubled = doubled_in(scraped, platforms_dir) messages = _export_messages(summary, unwritten, candidates, doubled, withheld, changes) return ExportResult(platform, True, messages, produced, exporter.file_modes(), { diff --git a/tests/test_audit_regressions.py b/tests/test_audit_regressions.py index 56ca741aa..fd2509869 100644 --- a/tests/test_audit_regressions.py +++ b/tests/test_audit_regressions.py @@ -6785,8 +6785,10 @@ class ReportsHoldTheDataTrees(unittest.TestCase): def test_each_entry_point_holds_them(self): import ast + # export_native holds them in build_export, which the freshness + # check calls too (TheExportHoldsTheDataTrees in test_export_report). for script in ("generate_readme.py", "generate_site.py", "slots.py", - "export_native.py", "cross_reference.py"): + "cross_reference.py"): tree = ast.parse((ROOT / "scripts" / script).read_text(encoding="utf-8")) main = next( node for node in tree.body diff --git a/tests/test_export_report.py b/tests/test_export_report.py index 53ac773a5..a3c6bfe75 100644 --- a/tests/test_export_report.py +++ b/tests/test_export_report.py @@ -728,5 +728,76 @@ class ADestinationDeclaredTwiceIsNamed(unittest.TestCase): "platform runs is known to read this file", page) +class TheExportHoldsTheDataTrees(unittest.TestCase): + """Removal candidates and doubled destinations are proved against data/. + Only the export command took the shared hold, so the freshness check, + which builds the same export, read the trees while a refresh swapped + them and called a committed report stale.""" + + def test_both_are_computed_under_the_hold(self): + import contextlib + import json + from unittest import mock + + from exporter.base_exporter import BaseExporter + + class Plain(BaseExporter): + @staticmethod + def platform_name() -> str: + return "plat" + + @staticmethod + def native_filename() -> str: + return "list.json" + + def render(self, systems, report, originals, scraped=None): + return {"list.json": json.dumps(sorted(systems))} + + def validate(self, systems, produced): + return [] + + held: list[str] = [] + + @contextlib.contextmanager + def tree_lock(): + held.append("held") + try: + yield + finally: + held.remove("held") + + def hold(stack, registry): + self.assertEqual(registry, {"tree": {"local_cache": "data/tree"}}) + stack.enter_context(tree_lock()) + + def under_hold(name): + def check(*_args, **_kwargs): + self.assertEqual(held, ["held"], name) + return [] + return check + + truth = {"systems": {"psx": {"files": [{"name": "a.bin", "md5": "b" * 32}]}}} + scraped = {"systems": {"psx": {"native_id": "psx", "files": [ + {"name": "a.bin", "destination": "a.bin", "native_system": "psx", + "md5": "a" * 32}, + ]}}} + with tempfile.TemporaryDirectory() as scratch, \ + mock.patch.object(native, "load_inputs", return_value=(truth, scraped)), \ + mock.patch.object(native, "unpinned", return_value=[]), \ + mock.patch.object(native, "load_data_dir_registry", + return_value={"tree": {"local_cache": "data/tree"}}), \ + mock.patch.object(native, "hold_data_dirs", side_effect=hold) as holding, \ + mock.patch.object(native, "removal_candidates", + side_effect=under_hold("removal_candidates")), \ + mock.patch.object(native, "doubled_in", side_effect=under_hold("doubled_in")): + result = build_export( + "plat", Plain, Path(scratch), str(ROOT / "platforms"), + Path(scratch) / "cache", False, + ) + self.assertTrue(result.ok, result.messages) + holding.assert_called_once() + self.assertEqual(held, []) + + if __name__ == "__main__": unittest.main()