Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tests/test_rpf9_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
6 changes: 5 additions & 1 deletion tools/rpf9_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading