From: Simon Brandhof Date: Mon, 9 Oct 2017 20:43:33 +0000 (+0200) Subject: SONAR-9930 Noisy ERROR logs when failing to delete a temp directory that does not... X-Git-Tag: 6.7-RC1~307 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=baf3fdf7fecd2ae48731a603fb261d12a84d1c05;p=sonarqube.git SONAR-9930 Noisy ERROR logs when failing to delete a temp directory that does not exist --- diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java index a921452d83c..2ceed0b82e7 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java @@ -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; } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/DefaultTempFolderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/DefaultTempFolderTest.java index 7ec43bda906..62ec60f1d49 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/DefaultTempFolderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/DefaultTempFolderTest.java @@ -19,12 +19,14 @@ */ 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(); + } }