From 9e7e655776966f0e2e40fa96dfc73aca5c2c865e Mon Sep 17 00:00:00 2001 From: Andrey Date: Tue, 24 Nov 2026 16:55:54 +0300 Subject: [PATCH] image_query_probe: publish mode on Windows (IMAGE-QUERY-PROBE-STAGE-CLEANUP) agent: yes AI-assisted: yes --- tests/test_image_query_probe.py | 37 +++++++++++++++++++++++++++++++++ tools/image_query_probe.py | 3 +-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/tests/test_image_query_probe.py b/tests/test_image_query_probe.py index c0edbd889..2a1f303fb 100644 --- a/tests/test_image_query_probe.py +++ b/tests/test_image_query_probe.py @@ -226,6 +226,43 @@ class Comparison(unittest.TestCase): finally: os.chmod(report, 0o600) + def test_publish_cleans_up_staged_file_when_replace_fails_even_if_readonly(self): + with tempfile.TemporaryDirectory() as tmp: + report = os.path.join(tmp, "report.json") + with open(report, "w", encoding="utf-8") as f: + f.write('{"initial": 1}\n') + os.chmod(report, 0o444) + + real_unlink = os.unlink + + def windows_strict_unlink(p): + mode = os.stat(p).st_mode + if not (mode & 0o200): + raise PermissionError(13, f"Access is denied: {p}") + return real_unlink(p) + + with mock.patch.object(pr, "replace_retry", side_effect=OSError(13, "Permission denied")), \ + mock.patch("os.unlink", side_effect=windows_strict_unlink): + with self.assertRaises(OSError): + pr.write_report(report, {"new": True}) + + leftover = [f for f in os.listdir(tmp) if f.startswith(".image_query_probe-")] + self.assertEqual(leftover, []) + + def test_publish_applies_published_mode_on_windows(self): + with tempfile.TemporaryDirectory() as tmp: + report = os.path.join(tmp, "report_win.json") + chmod_calls = [] + real_chmod = os.chmod + + def track_chmod(p, mode): + chmod_calls.append((p, mode)) + real_chmod(p, mode) + + with mock.patch("os.name", "nt"), mock.patch("os.chmod", side_effect=track_chmod): + pr.write_report(report, {"win": True}) + self.assertTrue(len(chmod_calls) > 0) + class Hardware(unittest.TestCase): def test_on_the_gpu(self): diff --git a/tools/image_query_probe.py b/tools/image_query_probe.py index 29828f293..d5b2a2799 100644 --- a/tools/image_query_probe.py +++ b/tools/image_query_probe.py @@ -308,8 +308,7 @@ def write_report(path, report): ) as f: tmp = f.name json.dump(report, f, indent=1) - if os.name != "nt": - os.chmod(tmp, published_mode(destination)) + os.chmod(tmp, published_mode(destination)) replace_retry(tmp, destination) except BaseException: if tmp is not None: