Skip to content
Open
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
40 changes: 40 additions & 0 deletions tests/test_bvh_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,46 @@ 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.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(
any(mode == 0o640 for _, mode in chmod_calls),
f"expected published_mode 0o640 to be applied, got {chmod_calls!r}",
)


if __name__ == "__main__":
unittest.main()
9 changes: 6 additions & 3 deletions tools/bvh_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down