diff --git a/tests/offline/test_acr_sec2_lsf_recover_and_unpack.py b/tests/offline/test_acr_sec2_lsf_recover_and_unpack.py index 13e9ca78..e8df5fd3 100644 --- a/tests/offline/test_acr_sec2_lsf_recover_and_unpack.py +++ b/tests/offline/test_acr_sec2_lsf_recover_and_unpack.py @@ -51,6 +51,11 @@ class TestAcrSec2LsfRecoverAndUnpack(unittest.TestCase): self.assertEqual(p.read_text(), '{"a": 2}\n') harness.write_or_check_json(p, {"a": 1}, False, "test report") harness.write_or_check_json(p, {"a": 1}, True, "test report") + # Distinct JSON types (false vs 0) must not compare equal. + p.write_text('{"physical_gpu_access": false, "b": 1}\n') + harness.write_or_check_json(p, {"b": 1, "physical_gpu_access": False}, True, "test report") + with self.assertRaises(SystemExit): + harness.write_or_check_json(p, {"b": 1, "physical_gpu_access": 0}, True, "test report") def test_resolve_repo_path_handles_relative_and_absolute(self): harness = _load("_acr_elf_harness_paths", "tools/_acr_elf_harness.py") diff --git a/tools/_acr_elf_harness.py b/tools/_acr_elf_harness.py index 19948aa7..ecc30e64 100644 --- a/tools/_acr_elf_harness.py +++ b/tools/_acr_elf_harness.py @@ -55,11 +55,19 @@ def resolve_repo_path(path_str: str) -> Path: return p if p.is_absolute() else (ROOT / p) +def _canonical_json(obj) -> str: + return json.dumps(obj, sort_keys=True, separators=(",", ":")) + + def write_or_check_json(path: Path, report: dict, check: bool, label: str) -> None: if check: if not path.is_file(): raise SystemExit(f"Missing {label}: {path}") - if json.loads(path.read_text()) != report: + try: + existing = json.loads(path.read_text()) + except json.JSONDecodeError as exc: + raise SystemExit(f"Invalid JSON in {label}: {path}: {exc}") from exc + if _canonical_json(existing) != _canonical_json(report): raise SystemExit(f"{label} differs: {path}") else: path.write_text(json.dumps(report, indent=2) + "\n")