diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-10-29 09:54:36 +0100 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-11-03 10:14:38 +0100 |
commit | d5d4548ee4f0c13360ca4f28ff751f927daef663 (patch) | |
tree | 136b63be0a3f2cc6af1982750b349d0e83ada9a1 /sonar-batch | |
parent | 21659de7af40d0537e97df0bb8f7a4cb4ec390e6 (diff) | |
download | sonarqube-d5d4548ee4f0c13360ca4f28ff751f927daef663.tar.gz sonarqube-d5d4548ee4f0c13360ca4f28ff751f927daef663.zip |
SONAR-6108 Keep batch report if debug mode is enabled
Diffstat (limited to 'sonar-batch')
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java | 7 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java | 49 |
2 files changed, 49 insertions, 7 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java index cf5030fac07..ef1790ce8ad 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java +++ b/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java @@ -22,6 +22,7 @@ package org.sonar.batch.report; import com.github.kevinsawicki.http.HttpRequest; import com.google.common.annotations.VisibleForTesting; import com.google.gson.Gson; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -29,8 +30,10 @@ import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; import java.util.Date; + import javax.annotation.CheckForNull; import javax.annotation.Nullable; + import org.apache.commons.io.FileUtils; import org.picocontainer.Startable; import org.slf4j.Logger; @@ -47,7 +50,6 @@ import org.sonar.batch.bootstrap.ServerClient; import org.sonar.batch.protocol.output.BatchReportWriter; import org.sonar.batch.scan.ImmutableProjectReactor; import org.sonar.batch.util.BatchUtils; - import static java.lang.String.format; @BatchSide @@ -55,6 +57,7 @@ public class ReportPublisher implements Startable { private static final Logger LOG = LoggerFactory.getLogger(ReportPublisher.class); public static final String KEEP_REPORT_PROP_KEY = "sonar.batch.keepReport"; + public static final String VERBOSE_KEY = "sonar.verbose"; public static final String DUMP_REPORT_PROP_KEY = "sonar.batch.dumpReportDir"; private final ServerClient serverClient; @@ -91,7 +94,7 @@ public class ReportPublisher implements Startable { @Override public void stop() { - if (!settings.getBoolean(KEEP_REPORT_PROP_KEY)) { + if (!settings.getBoolean(KEEP_REPORT_PROP_KEY) && !settings.getBoolean(VERBOSE_KEY)) { FileUtils.deleteQuietly(reportDir); } else { LOG.info("Batch report generated in " + reportDir); diff --git a/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java b/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java index c12e616e863..93582aa6d7d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java @@ -19,6 +19,15 @@ */ package org.sonar.batch.report; +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; @@ -30,7 +39,6 @@ import org.sonar.api.utils.TempFolder; import org.sonar.batch.analysis.DefaultAnalysisMode; import org.sonar.batch.bootstrap.ServerClient; import org.sonar.batch.scan.ImmutableProjectReactor; - import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; @@ -42,16 +50,24 @@ public class ReportPublisherTest { private ImmutableProjectReactor reactor; + private Settings settings; + + private ProjectDefinition root; + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + @Before public void setUp() { + settings = new Settings(); mode = mock(DefaultAnalysisMode.class); reactor = mock(ImmutableProjectReactor.class); - when(reactor.getRoot()).thenReturn(ProjectDefinition.create().setKey("struts")); + root = ProjectDefinition.create().setKey("struts").setWorkDir(temp.getRoot()); + when(reactor.getRoot()).thenReturn(root); } @Test public void should_log_successful_analysis() { - Settings settings = new Settings(); settings.setProperty(CoreProperties.SERVER_BASE_URL, "http://myserver/"); ReportPublisher job = new ReportPublisher(settings, mock(ServerClient.class), mock(Server.class), mock(AnalysisContextReportPublisher.class), reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]); @@ -66,7 +82,6 @@ public class ReportPublisherTest { @Test public void should_log_successful_analysis_with_ce_task() { - Settings settings = new Settings(); settings.setProperty(CoreProperties.SERVER_BASE_URL, "http://myserver/"); ReportPublisher job = new ReportPublisher(settings, mock(ServerClient.class), mock(Server.class), mock(AnalysisContextReportPublisher.class), reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]); @@ -82,7 +97,6 @@ public class ReportPublisherTest { @Test public void should_log_successful_issues_analysis() { - Settings settings = new Settings(); when(mode.isIssues()).thenReturn(true); ReportPublisher job = new ReportPublisher(settings, mock(ServerClient.class), mock(Server.class), mock(AnalysisContextReportPublisher.class), reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]); @@ -94,4 +108,29 @@ public class ReportPublisherTest { verifyNoMoreInteractions(logger); } + @Test + public void should_not_delete_report() throws IOException { + settings.setProperty("sonar.verbose", true); + Path reportDir = temp.getRoot().toPath().resolve("batch-report"); + Files.createDirectory(reportDir); + ReportPublisher job = new ReportPublisher(settings, mock(ServerClient.class), mock(Server.class), mock(AnalysisContextReportPublisher.class), reactor, mode, + mock(TempFolder.class), new ReportPublisherStep[0]); + + job.start(); + job.stop(); + assertThat(reportDir).isDirectory(); + } + + @Test + public void should_delete_report() throws IOException { + Path reportDir = temp.getRoot().toPath().resolve("batch-report"); + Files.createDirectory(reportDir); + ReportPublisher job = new ReportPublisher(settings, mock(ServerClient.class), mock(Server.class), mock(AnalysisContextReportPublisher.class), reactor, mode, + mock(TempFolder.class), new ReportPublisherStep[0]); + + job.start(); + job.stop(); + assertThat(reportDir).doesNotExist(); + } + } |