diff --git a/tests/test_rpf9_names.py b/tests/test_rpf9_names.py index c8402aa5e1..c4f6381f8d 100644 --- a/tests/test_rpf9_names.py +++ b/tests/test_rpf9_names.py @@ -426,6 +426,22 @@ def test_publish_leaves_existing_tmp_sibling_untouched(self): with open(sibling, "rb") as f: self.assertEqual(f.read(), b"pre-existing sibling") + def test_stage_cleanup_restores_write_mode_on_replace_failure(self): + with tempfile.TemporaryDirectory() as tmp: + out = os.path.join(tmp, "out.json") + chmod_calls = [] + real_chmod = os.chmod + + def track_chmod(p, mode, **kwargs): + chmod_calls.append((p, mode)) + return real_chmod(p, mode, **kwargs) + + with mock.patch("rpf9_names.os.replace", side_effect=OSError("replace failed")), \ + mock.patch("rpf9_names.os.chmod", side_effect=track_chmod): + with self.assertRaises(OSError): + rpf9_names.publish_report(out, [], {}) + self.assertTrue(any(mode == 0o600 for p, mode in chmod_calls)) + if __name__ == "__main__": unittest.main() diff --git a/tools/rpf9_names.py b/tools/rpf9_names.py index 126ffad29f..596f988a9e 100644 --- a/tools/rpf9_names.py +++ b/tools/rpf9_names.py @@ -211,7 +211,11 @@ def publish_report(path, rows, summary): json.dump({"summary": summary, "files": rows}, f, indent=1) f.write("\n") os.chmod(temp, published_mode(path)) - os.replace(temp, path) + try: + os.replace(temp, path) + except BaseException: + os.chmod(temp, 0o600) + raise def _int_arg(text):