diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-10-09 22:43:33 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-10-11 13:34:27 +0200 |
commit | baf3fdf7fecd2ae48731a603fb261d12a84d1c05 (patch) | |
tree | 690488274560cebcd0be0b8032c37c64a4760231 /sonar-plugin-api/src | |
parent | a4bb18bf2e7dee17af8ae2974f47a9f8909a638b (diff) | |
download | sonarqube-baf3fdf7fecd2ae48731a603fb261d12a84d1c05.tar.gz sonarqube-baf3fdf7fecd2ae48731a603fb261d12a84d1c05.zip |
SONAR-9930 Noisy ERROR logs when failing to delete a temp directory that does not exist
Diffstat (limited to 'sonar-plugin-api/src')
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java | 8 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/DefaultTempFolderTest.java | 34 |
2 files changed, 37 insertions, 5 deletions
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(); + } } |