diff --git a/tests/test_ds_permute_probe.py b/tests/test_ds_permute_probe.py index 7843c5cc6..a31d0d35d 100644 --- a/tests/test_ds_permute_probe.py +++ b/tests/test_ds_permute_probe.py @@ -110,6 +110,49 @@ class DsPermuteProbeTests(unittest.TestCase): self.assertNotIn(b"\r\n", raw) self.assertEqual(json.loads(raw.decode("utf-8")), payload) + @staticmethod + def _held_open(winerror): + # PermissionError as Windows raises it while another handle holds the file without share-delete. + e = PermissionError(13, "Access is denied") + e.winerror = winerror + return e + + def test_write_json_report_rides_out_a_short_windows_hold(self): + import replace_retry + with tempfile.TemporaryDirectory() as tmp: + out_path = os.path.join(tmp, "out.json") + with open(out_path, "w", encoding="utf-8") as f: + f.write("{}") + real_replace = os.replace + calls = [] + + def held_once(src, dst): + calls.append(dst) + if len(calls) == 1: + raise self._held_open(32) + return real_replace(src, dst) + + with mock.patch.object(replace_retry.os, "replace", side_effect=held_once): + err = ds_permute_probe.write_json_report(out_path, {"ok": True}) + self.assertIsNone(err) + self.assertEqual(len(calls), 2) + with open(out_path, encoding="utf-8") as f: + self.assertEqual(json.load(f), {"ok": True}) + self.assertEqual(os.listdir(tmp), ["out.json"]) + + def test_write_json_report_names_a_report_held_open(self): + with tempfile.TemporaryDirectory() as tmp: + out_path = os.path.join(tmp, "out.json") + with open(out_path, "w", encoding="utf-8") as f: + f.write('{"old": true}') + with mock.patch.object(ds_permute_probe, "replace_retry", side_effect=self._held_open(5)): + err = ds_permute_probe.write_json_report(out_path, {"ok": True}) + self.assertIn("cannot write --json", err) + self.assertIn("open in another program", err) + with open(out_path, encoding="utf-8") as f: + self.assertEqual(json.load(f), {"old": True}) + self.assertEqual(os.listdir(tmp), ["out.json"]) + def test_write_json_report_and_main_catch_oserror_cleanly(self): with tempfile.TemporaryDirectory() as tmp: out_path = os.path.join(tmp, "out.json") diff --git a/tools/ds_permute_probe.py b/tools/ds_permute_probe.py index 871a9b230..8d3c54907 100644 --- a/tools/ds_permute_probe.py +++ b/tools/ds_permute_probe.py @@ -23,6 +23,8 @@ sys.dont_write_bytecode = True if HERE not in sys.path: sys.path.insert(0, HERE) +from replace_retry import replace as replace_retry # noqa: E402 + SENTINEL = 0xCAFE0000 PAYLOAD_BASE = 0x1000 @@ -209,8 +211,13 @@ def write_json_report(path, data): ) as f: tmp_path = f.name json.dump(data, f, indent=2) - os.replace(tmp_path, path) + # Windows refuses the rename while another program holds the report open without share-delete (a viewer, + # an editor, a virus scanner); replace_retry rides out a short hold, and a long one is reported as such. + replace_retry(tmp_path, path) tmp_path = None + except PermissionError as e: + hint = " (is the report open in another program? close it and run again)" if getattr(e, "winerror", None) else "" + return f"cannot write --json {path}: {e.strerror or e}{hint}" except OSError as e: return f"cannot write --json {path}: {e.strerror or e}" finally: