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_story_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,22 @@ def test_output_cannot_overwrite_carved_input(self):
self.assertEqual(p.returncode, 2)
self.assertIn("output would overwrite an input file", p.stderr)

def test_stage_cleanup_restores_write_mode_on_replace_failure(self):
with tempfile.TemporaryDirectory() as tmp:
dest = 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("story_index.os.replace", side_effect=OSError("replace failed")), \
mock.patch("story_index.os.chmod", side_effect=track_chmod):
with self.assertRaises(OSError):
story_index.write_output(dest, [], {})
self.assertTrue(any(mode == 0o600 for p, mode in chmod_calls))


if __name__ == "__main__":
unittest.main()
6 changes: 5 additions & 1 deletion tools/story_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,11 @@ def write_output(path, rows, summary):
# os.replace hands the destination the staged file's bits; give them the destination's mode
# (an existing report's, or what open() would have made for a new file) so perms are stable.
os.chmod(staged, published_mode(dest))
os.replace(staged, dest)
try:
os.replace(staged, dest)
except BaseException:
os.chmod(staged, 0o600)
raise


def byte_count(text):
Expand Down
Loading