]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9930 Noisy ERROR logs when failing to delete a temp directory that does not...
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 9 Oct 2017 20:43:33 +0000 (22:43 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 11 Oct 2017 11:34:27 +0000 (13:34 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java
sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/DefaultTempFolderTest.java

index a921452d83c4207c440c39bb9c80bb059d3fc999..2ceed0b82e749f978de7099b5ca16ddaaabdb254 100644 (file)
@@ -93,7 +93,9 @@ public class DefaultTempFolder implements TempFolder {
 
   public void clean() {
     try {
-      Files.walkFileTree(tempDir.toPath(), DeleteRecursivelyFileVisitor.INSTANCE);
+      if (tempDir.exists()) {
+        Files.walkFileTree(tempDir.toPath(), DeleteRecursivelyFileVisitor.INSTANCE);
+      }
     } catch (IOException e) {
       LOG.error("Failed to delete temp folder", e);
     }
@@ -110,13 +112,13 @@ public class DefaultTempFolder implements TempFolder {
 
     @Override
     public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
-      Files.delete(file);
+      Files.deleteIfExists(file);
       return FileVisitResult.CONTINUE;
     }
 
     @Override
     public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
-      Files.delete(dir);
+      Files.deleteIfExists(dir);
       return FileVisitResult.CONTINUE;
     }
   }
index 7ec43bda9069933b1884fe10ae8bc0ea78195cb9..62ec60f1d49c257b97559e1916e329a8d78c765e 100644 (file)
  */
 package org.sonar.api.utils.internal;
 
+import java.io.File;
+import org.apache.commons.io.FileUtils;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.junit.rules.TemporaryFolder;
-
-import java.io.File;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
 
 import static org.assertj.core.api.Assertions.assertThat;
 
@@ -36,6 +38,9 @@ public class DefaultTempFolderTest {
   @Rule
   public TemporaryFolder temp = new TemporaryFolder();
 
+  @Rule
+  public LogTester logTester = new LogTester();
+
   @Test
   public void createTempFolderAndFile() throws Exception {
     File rootTempFolder = temp.newFolder();
@@ -86,4 +91,29 @@ public class DefaultTempFolderTest {
     throwable.expectMessage("Failed to create temp file");
     tempFolder.newFile(tooLong, ".txt");
   }
+
+  @Test
+  public void clean_deletes_non_empty_directory() throws Exception {
+    File dir = temp.newFolder();
+    FileUtils.touch(new File(dir, "foo.txt"));
+
+    DefaultTempFolder underTest = new DefaultTempFolder(dir);
+    underTest.clean();
+
+    assertThat(dir).doesNotExist();
+  }
+
+  @Test
+  public void clean_does_not_fail_if_directory_has_already_been_deleted() throws Exception {
+    File dir = temp.newFolder();
+
+    DefaultTempFolder underTest = new DefaultTempFolder(dir);
+    underTest.clean();
+    assertThat(dir).doesNotExist();
+
+    // second call does not fail, nor log ERROR logs
+    underTest.clean();
+
+    assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty();
+  }
 }