upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMoonEyes <ducoinbenjamin@yahoo.fr>2026-11-28 12:03:17 +0100
committerGitHub <noreply@github.com>2026-11-28 12:03:17 +0100
commit2edfdf6094cd7cb39feb3817cde8e2ff4c3d8f8a (patch)
tree97921cf13de294e25486d9b3f5135ccc72c4719e
parent4195fdb96d3faf79cdb5285bcea6198f494f4447 (diff)
parent55c0dca33956397c28fd2415b3e89429fb6239e0 (diff)
Merge pull request #775 from trigger337/fix/anim-skeleton-stage-cleanup
anim_skeleton_match: restore write mode before unlink on replace failure and publish mode on Windows (ANIM-SKELETON-STAGE-CLEANUP)
-rw-r--r--tests/test_anim_skeleton_match.py44
-rw-r--r--tools/anim_skeleton_match.py10
2 files changed, 51 insertions, 3 deletions
diff --git a/tests/test_anim_skeleton_match.py b/tests/test_anim_skeleton_match.py
index 3b39c76e6..e8f2fde36 100644
--- a/tests/test_anim_skeleton_match.py
+++ b/tests/test_anim_skeleton_match.py
@@ -74,6 +74,50 @@ class SkeletonMatchTests(unittest.TestCase):
74 with open(path, encoding="utf-8") as f: 74 with open(path, encoding="utf-8") as f:
75 self.assertEqual(f.read(), previous) 75 self.assertEqual(f.read(), previous)
76 76
77 def test_publish_cleans_up_staged_file_when_replace_fails_even_if_readonly(self):
78 with tempfile.TemporaryDirectory() as tmp:
79 path = os.path.join(tmp, "report.json")
80 real_unlink = os.unlink
81 unlink_calls = []
82
83 def windows_strict_unlink(p):
84 mode = os.stat(p).st_mode
85 if not (mode & 0o200):
86 raise PermissionError(13, f"Access is denied: {p}")
87 unlink_calls.append(p)
88 return real_unlink(p)
89
90 replace_calls = []
91
92 def fake_replace(src, dst):
93 replace_calls.append((src, dst))
94 raise OSError(13, "Permission denied")
95
96 with mock.patch.object(asm, "published_mode", return_value=0o444), \
97 mock.patch.object(asm, "replace_retry", side_effect=fake_replace), \
98 mock.patch("os.unlink", side_effect=windows_strict_unlink):
99 with self.assertRaises(OSError):
100 asm._write_json_report(path, '{"failed": true}')
101
102 self.assertEqual(len(replace_calls), 1)
103 self.assertEqual(len(unlink_calls), 1)
104 leftover = [f for f in os.listdir(tmp) if f.startswith(".anim_skeleton_match-")]
105 self.assertEqual(leftover, [])
106
107 def test_publish_applies_published_mode_on_windows(self):
108 with tempfile.TemporaryDirectory() as tmp:
109 path = os.path.join(tmp, "report_win.json")
110 chmod_calls = []
111 real_chmod = os.chmod
112
113 def track_chmod(p, mode):
114 chmod_calls.append((p, mode))
115 real_chmod(p, mode)
116
117 with mock.patch("os.name", "nt"), mock.patch("os.chmod", side_effect=track_chmod):
118 asm._write_json_report(path, '{"win": true}')
119 self.assertTrue(any(p.endswith(".tmp") and mode == asm.published_mode(path) for p, mode in chmod_calls), chmod_calls)
120
77 121
78if __name__ == "__main__": 122if __name__ == "__main__":
79 unittest.main() 123 unittest.main()
diff --git a/tools/anim_skeleton_match.py b/tools/anim_skeleton_match.py
index 2cab41339..a774bd054 100644
--- a/tools/anim_skeleton_match.py
+++ b/tools/anim_skeleton_match.py
@@ -141,14 +141,18 @@ def _write_json_report(path: str, text: str) -> None:
141 try: 141 try:
142 with staged: 142 with staged:
143 staged.write(text if text.endswith("\n") else text + "\n") 143 staged.write(text if text.endswith("\n") else text + "\n")
144 if os.name != "nt": 144 os.chmod(staged.name, published_mode(destination))
145 os.chmod(staged.name, published_mode(destination))
146 replace_retry(staged.name, destination) 145 replace_retry(staged.name, destination)
147 finally: 146 except BaseException:
147 try:
148 os.chmod(staged.name, 0o600)
149 except OSError:
150 pass
148 try: 151 try:
149 os.unlink(staged.name) 152 os.unlink(staged.name)
150 except FileNotFoundError: 153 except FileNotFoundError:
151 pass 154 pass
155 raise
152 156
153 157
154def main(argv=None): 158def main(argv=None):