Copilot commented on code in PR #3655:
URL: https://github.com/apache/maven-surefire/pull/3655#discussion_r4763192556


##########
surefire-api/src/main/java/org/apache/maven/surefire/api/runorder/RunEntryStatisticsMap.java:
##########
@@ -120,11 +124,23 @@ public static RunEntryStatisticsMap 
fromStream(InputStream fileReader) {
     }
 
     public void serialize(File statsFile) throws IOException {
-        if (statsFile.isFile()) {
-            //noinspection ResultOfMethodCallIgnored
-            statsFile.delete();
+        // write a temp file next to the stats file and move it in place, so a 
killed build can't leave half a file
+        Path target = statsFile.getAbsoluteFile().toPath();
+        Path tmp = Files.createTempFile(target.getParent(), 
statsFile.getName(), ".tmp");
+        try {
+            write(tmp);
+            try {
+                Files.move(tmp, target, REPLACE_EXISTING, ATOMIC_MOVE);
+            } catch (AtomicMoveNotSupportedException e) {
+                Files.move(tmp, target, REPLACE_EXISTING);
+            }
+        } finally {
+            Files.deleteIfExists(tmp);

Review Comment:
   The serialization tests only cover successful writes, and the new reporter 
failure test fails before a temporary file is created. The atomic-move fallback 
and cleanup after a write/move failure therefore remain untested. Please add 
regression tests verifying fallback replacement, preservation of existing 
statistics when writing fails, and temporary-file cleanup.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to