mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:08:38 +00:00
Merge pull request #2004 from Ch1mpleo/fix/f16-widen-probe-atomic-json
tools/f16_widen_probe: write --json report atomically (F16-WIDEN-PROBE-ATOMIC-JSON)
This commit is contained in:
@ -199,7 +199,9 @@ class Contracts(unittest.TestCase):
|
||||
hw_runner, "open_oracle", return_value=FakeOracle()
|
||||
),
|
||||
unittest.mock.patch.object(hw_asm, "family", return_value="gfx10"),
|
||||
unittest.mock.patch("builtins.open", side_effect=OSError("disk full")),
|
||||
unittest.mock.patch.object(
|
||||
probe.tempfile, "NamedTemporaryFile", side_effect=OSError("disk full")
|
||||
),
|
||||
redirect_stdout(out),
|
||||
redirect_stderr(err),
|
||||
):
|
||||
@ -263,6 +265,23 @@ class Contracts(unittest.TestCase):
|
||||
)
|
||||
self.assertIn("v_mov_b32 v4, 0x7ddb", t.lines)
|
||||
|
||||
def test_atomic_json_write_and_replace_failure_preserves_existing(self):
|
||||
import tempfile
|
||||
from unittest import mock
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
out = os.path.join(tmp, "report.json")
|
||||
self.assertIsNone(probe.write_json_report(out, {"ok": True}))
|
||||
self.assertEqual(os.listdir(tmp), ["report.json"])
|
||||
with open(out, "w", encoding="utf-8", newline="\n") as f:
|
||||
f.write('{"keep": true}\n')
|
||||
with mock.patch.object(probe.os, "replace", side_effect=OSError("replace denied")):
|
||||
err = probe.write_json_report(out, {"ok": False})
|
||||
self.assertIsNotNone(err)
|
||||
self.assertEqual(os.listdir(tmp), ["report.json"])
|
||||
with open(out, encoding="utf-8") as f:
|
||||
self.assertEqual(f.read(), '{"keep": true}\n')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -25,6 +25,7 @@ import json
|
||||
import os
|
||||
import struct
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.dont_write_bytecode = True
|
||||
@ -281,11 +282,24 @@ def write_json_report(path, data):
|
||||
os.makedirs(parent, exist_ok=True)
|
||||
except OSError as e:
|
||||
return f"cannot create directory for --json {path}: {e.strerror or e}"
|
||||
tmp_path = None
|
||||
try:
|
||||
with open(path, "w", encoding="utf-8", newline="\n") as f:
|
||||
with tempfile.NamedTemporaryFile(
|
||||
"w", encoding="utf-8", newline="\n",
|
||||
dir=parent or ".", prefix=".f16_widen_probe-", suffix=".tmp", delete=False,
|
||||
) as f:
|
||||
tmp_path = f.name
|
||||
json.dump(data, f, indent=2)
|
||||
os.replace(tmp_path, path)
|
||||
tmp_path = None
|
||||
except OSError as e:
|
||||
return f"cannot write --json {path}: {e.strerror or e}"
|
||||
finally:
|
||||
if tmp_path is not None:
|
||||
try:
|
||||
os.unlink(tmp_path)
|
||||
except OSError:
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user