diff options
author | Wojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com> | 2023-06-15 17:04:02 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-06-15 20:03:02 +0000 |
commit | 27fb5c0e4fad811bae8028a4e13267fdc083f8b3 (patch) | |
tree | ffb36de9543756e2af3a31af02ec8f312d3cac26 /sonar-scanner-engine | |
parent | 8bc53b74c73dd5dc777187ce6842719f87730213 (diff) | |
download | sonarqube-27fb5c0e4fad811bae8028a4e13267fdc083f8b3.tar.gz sonarqube-27fb5c0e4fad811bae8028a4e13267fdc083f8b3.zip |
SONAR-19522 Fail-fast if SARIF file doesn't exist. (#8587)
Diffstat (limited to 'sonar-scanner-engine')
2 files changed, 41 insertions, 13 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/SarifIssuesImportSensor.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/SarifIssuesImportSensor.java index 346cb727f7d..53a2a651dde 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/SarifIssuesImportSensor.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/SarifIssuesImportSensor.java @@ -19,6 +19,7 @@ */ package org.sonar.scanner.externalissue.sarif; +import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.util.Arrays; import java.util.Collections; @@ -37,9 +38,12 @@ import org.sonar.api.config.PropertyDefinition; import org.sonar.api.resources.Qualifiers; import org.sonar.api.scanner.ScannerSide; import org.sonar.api.scanner.sensor.ProjectSensor; +import org.sonar.api.utils.MessageException; import org.sonar.core.sarif.Sarif210; import org.sonar.core.sarif.SarifSerializer; +import static java.lang.String.format; + @ScannerSide public class SarifIssuesImportSensor implements ProjectSensor { @@ -81,6 +85,8 @@ public class SarifIssuesImportSensor implements ProjectSensor { try { SarifImportResults sarifImportResults = processReport(context, reportPath); filePathToImportResults.put(reportPath, sarifImportResults); + } catch (NoSuchFileException e) { + throw MessageException.of(format("SARIF report file not found: %s", e.getFile())); } catch (Exception exception) { LOG.warn("Failed to process SARIF report from file '{}', error: '{}'", reportPath, exception.getMessage()); } @@ -92,7 +98,7 @@ public class SarifIssuesImportSensor implements ProjectSensor { return Arrays.stream(config.getStringArray(SARIF_REPORT_PATHS_PROPERTY_KEY)).collect(Collectors.toSet()); } - private SarifImportResults processReport(SensorContext context, String reportPath) { + private SarifImportResults processReport(SensorContext context, String reportPath) throws NoSuchFileException { LOG.debug("Importing SARIF issues from '{}'", reportPath); Path reportFilePath = context.fileSystem().resolvePath(reportPath).toPath(); Sarif210 sarifReport = sarifSerializer.deserialize(reportFilePath); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/SarifIssuesImportSensorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/SarifIssuesImportSensorTest.java index 6a85b604a86..f3181825034 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/SarifIssuesImportSensorTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/SarifIssuesImportSensorTest.java @@ -20,6 +20,7 @@ package org.sonar.scanner.externalissue.sarif; import com.google.common.collect.MoreCollectors; +import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.util.Optional; import org.junit.Before; @@ -33,11 +34,13 @@ import org.sonar.api.batch.sensor.internal.SensorContextTester; import org.sonar.api.config.internal.MapSettings; import org.sonar.api.testfixtures.log.LogAndArguments; import org.sonar.api.testfixtures.log.LogTester; +import org.sonar.api.utils.MessageException; import org.sonar.api.utils.log.LoggerLevel; import org.sonar.core.sarif.Sarif210; import org.sonar.core.sarif.SarifSerializer; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -68,7 +71,7 @@ public class SarifIssuesImportSensorTest { private final SensorContextTester sensorContext = SensorContextTester.create(Path.of(".")); @Test - public void execute_whenSingleFileIsSpecified_shouldImportResults() { + public void execute_whenSingleFileIsSpecified_shouldImportResults() throws NoSuchFileException { sensorSettings.setProperty("sonar.sarifReportPaths", FILE_1); ReportAndResults reportAndResults = mockSuccessfulReportAndResults(FILE_1); @@ -83,7 +86,7 @@ public class SarifIssuesImportSensorTest { } @Test - public void execute_whenMultipleFilesAreSpecified_shouldImportResults() { + public void execute_whenMultipleFilesAreSpecified_shouldImportResults() throws NoSuchFileException { sensorSettings.setProperty("sonar.sarifReportPaths", SARIF_REPORT_PATHS_PARAM); ReportAndResults reportAndResults1 = mockSuccessfulReportAndResults(FILE_1); ReportAndResults reportAndResults2 = mockSuccessfulReportAndResults(FILE_2); @@ -99,7 +102,7 @@ public class SarifIssuesImportSensorTest { } @Test - public void execute_whenFileContainsOnlySuccessfulRuns_shouldLogCorrectMessage() { + public void execute_whenFileContainsOnlySuccessfulRuns_shouldLogCorrectMessage() throws NoSuchFileException { sensorSettings.setProperty("sonar.sarifReportPaths", FILE_1); ReportAndResults reportAndResults = mockSuccessfulReportAndResults(FILE_1); @@ -110,7 +113,7 @@ public class SarifIssuesImportSensorTest { } @Test - public void execute_whenFileContainsOnlyFailedRuns_shouldLogCorrectMessage() { + public void execute_whenFileContainsOnlyFailedRuns_shouldLogCorrectMessage() throws NoSuchFileException { sensorSettings.setProperty("sonar.sarifReportPaths", FILE_1); ReportAndResults reportAndResults = mockFailedReportAndResults(FILE_1); @@ -122,7 +125,7 @@ public class SarifIssuesImportSensorTest { } @Test - public void execute_whenFileContainsFailedAndSuccessfulRuns_shouldLogCorrectMessage() { + public void execute_whenFileContainsFailedAndSuccessfulRuns_shouldLogCorrectMessage() throws NoSuchFileException { sensorSettings.setProperty("sonar.sarifReportPaths", FILE_1); @@ -137,7 +140,7 @@ public class SarifIssuesImportSensorTest { } @Test - public void execute_whenImportFails_shouldSkipReport() { + public void execute_whenImportFails_shouldSkipReport() throws NoSuchFileException { sensorSettings.setProperty("sonar.sarifReportPaths", SARIF_REPORT_PATHS_PARAM); ReportAndResults reportAndResults1 = mockFailedReportAndResults(FILE_1); @@ -154,7 +157,7 @@ public class SarifIssuesImportSensorTest { } @Test - public void execute_whenDeserializationFails_shouldSkipReport() { + public void execute_whenDeserializationFails_shouldSkipReport() throws NoSuchFileException { sensorSettings.setProperty("sonar.sarifReportPaths", SARIF_REPORT_PATHS_PARAM); failDeserializingReport(FILE_1); @@ -168,12 +171,31 @@ public class SarifIssuesImportSensorTest { assertSummaryIsCorrectlyDisplayedForSuccessfulFile(FILE_2, reportAndResults2.getSarifImportResults()); } - private void failDeserializingReport(String path) { + @Test + public void execute_whenDeserializationThrowsMessageException_shouldRethrow() throws NoSuchFileException { + sensorSettings.setProperty("sonar.sarifReportPaths", FILE_1); + + NoSuchFileException e = new NoSuchFileException("non-existent"); + failDeserializingReportWithException(FILE_1, e); + + SarifIssuesImportSensor sensor = new SarifIssuesImportSensor(sarifSerializer, sarifImporter, sensorSettings.asConfig()); + assertThatThrownBy(() -> sensor.execute(sensorContext)) + .isInstanceOf(MessageException.class) + .hasMessage("SARIF report file not found: non-existent"); + + } + + private void failDeserializingReport(String path) throws NoSuchFileException { Path reportFilePath = sensorContext.fileSystem().resolvePath(path).toPath(); when(sarifSerializer.deserialize(reportFilePath)).thenThrow(new NullPointerException("deserialization failed")); } - private ReportAndResults mockSuccessfulReportAndResults(String path) { + private void failDeserializingReportWithException(String path, Exception exception) throws NoSuchFileException { + Path reportFilePath = sensorContext.fileSystem().resolvePath(path).toPath(); + when(sarifSerializer.deserialize(reportFilePath)).thenThrow(exception); + } + + private ReportAndResults mockSuccessfulReportAndResults(String path) throws NoSuchFileException { Sarif210 report = mockSarifReport(path); SarifImportResults sarifImportResults = mock(SarifImportResults.class); @@ -185,14 +207,14 @@ public class SarifIssuesImportSensorTest { return new ReportAndResults(report, sarifImportResults); } - private Sarif210 mockSarifReport(String path) { + private Sarif210 mockSarifReport(String path) throws NoSuchFileException { Sarif210 report = mock(Sarif210.class); Path reportFilePath = sensorContext.fileSystem().resolvePath(path).toPath(); when(sarifSerializer.deserialize(reportFilePath)).thenReturn(report); return report; } - private ReportAndResults mockFailedReportAndResults(String path) { + private ReportAndResults mockFailedReportAndResults(String path) throws NoSuchFileException { Sarif210 report = mockSarifReport(path); SarifImportResults sarifImportResults = mock(SarifImportResults.class); @@ -203,7 +225,7 @@ public class SarifIssuesImportSensorTest { return new ReportAndResults(report, sarifImportResults); } - private ReportAndResults mockMixedReportAndResults(String path) { + private ReportAndResults mockMixedReportAndResults(String path) throws NoSuchFileException { Sarif210 report = mockSarifReport(path); SarifImportResults sarifImportResults = mock(SarifImportResults.class); |