tools: preserve JSON types in ACR --check report comparison

This commit is contained in:
morebrackets
2026-12-02 05:57:59 -08:00
parent ee2bb4613c
commit db9784136e
2 changed files with 14 additions and 1 deletions

View File

@ -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")

View File

@ -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")