From 28cb3569e0b08a98d227a121524c94b57e09d025 Mon Sep 17 00:00:00 2001 From: Andrey Date: Tue, 24 Nov 2026 16:29:51 +0300 Subject: [PATCH 1/2] bvh_groups: restore write mode before unlink on replace failure and publish mode on Windows (BVH-GROUPS-STAGE-CLEANUP) --- tests/test_bvh_groups.py | 35 +++++++++++++++++++++++++++++++++++ tools/bvh_groups.py | 9 ++++++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/tests/test_bvh_groups.py b/tests/test_bvh_groups.py index 7993580970..56909cc531 100644 --- a/tests/test_bvh_groups.py +++ b/tests/test_bvh_groups.py @@ -341,6 +341,41 @@ def test_main_json_oserror_returns_exit_2(self): self.assertTrue(lines[0].startswith("bvh_groups:"), lines[0]) self.assertNotIn("Traceback", se.getvalue()) + def test_publish_cleans_up_staged_file_when_replace_fails_even_if_readonly(self): + path = os.path.join(self.tmp.name, "report.json") + with open(path, "w", encoding="utf-8") as f: + f.write('{"initial": 1}\n') + os.chmod(path, 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(bvh_groups, "replace_retry", side_effect=OSError(13, "Permission denied")), \ + mock.patch("os.unlink", side_effect=windows_strict_unlink): + with self.assertRaises(OSError): + bvh_groups.write_json_report(path, {"source": "x", "summary": {}, "rows": []}) + + leftover = [f for f in os.listdir(self.tmp.name) if f.startswith(".bvh_groups-")] + self.assertEqual(leftover, []) + + def test_publish_applies_published_mode_on_windows(self): + path = os.path.join(self.tmp.name, "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): + bvh_groups.write_json_report(path, {"source": "x", "summary": {}, "rows": []}) + self.assertTrue(len(chmod_calls) > 0) + if __name__ == "__main__": unittest.main() diff --git a/tools/bvh_groups.py b/tools/bvh_groups.py index d249ad35da..49497242bd 100644 --- a/tools/bvh_groups.py +++ b/tools/bvh_groups.py @@ -319,13 +319,16 @@ def write_json_report(path, report): with staged: json.dump(report, staged, indent=1, allow_nan=False) staged.write("\n") - if os.name != "nt": - os.chmod(staged.name, published_mode(destination)) + os.chmod(staged.name, published_mode(destination)) replace_retry(staged.name, destination) finally: + try: + os.chmod(staged.name, 0o600) + except OSError: + pass try: os.unlink(staged.name) - except FileNotFoundError: + except OSError: pass From 99ef7a875eb47cd2c8c9178a5352f0fb5f90a277 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 2 Dec 2026 00:41:58 +0300 Subject: [PATCH 2/2] tests: assert published_mode (0o640) is applied on Windows in test_bvh_groups --- tests/test_bvh_groups.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/test_bvh_groups.py b/tests/test_bvh_groups.py index 56909cc531..6f7696b10d 100644 --- a/tests/test_bvh_groups.py +++ b/tests/test_bvh_groups.py @@ -372,9 +372,14 @@ 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): + with mock.patch("os.name", "nt"), \ + mock.patch.object(bvh_groups, "published_mode", return_value=0o640), \ + mock.patch("os.chmod", side_effect=track_chmod): bvh_groups.write_json_report(path, {"source": "x", "summary": {}, "rows": []}) - self.assertTrue(len(chmod_calls) > 0) + self.assertTrue( + any(mode == 0o640 for _, mode in chmod_calls), + f"expected published_mode 0o640 to be applied, got {chmod_calls!r}", + ) if __name__ == "__main__":