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


##########
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:
   Thanks, I added `testSerializeKeepsWhatIsThereAndCleansUpWhenTheMoveFails` 
in e98267b3a. It points the stats file at a directory that isn't empty, so the 
move fails after the temp file has been written. The test checks that 
`serialize` throws, that the old content is still there, and that no `.tmp` 
file is left behind. If the `deleteIfExists` in the `finally` is removed, the 
test fails. I didn't add a test for the `AtomicMoveNotSupportedException` 
fallback: a local file system always supports an atomic move within one 
directory, so testing it would need a mock of `Files` or a hook added only for 
the test, which isn't worth it for those three lines.
   



-- 
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