diff --git a/sonar-zpa-plugin/src/main/kotlin/org/sonar/plsqlopen/utplsql/CoverageResultImporter.kt b/sonar-zpa-plugin/src/main/kotlin/org/sonar/plsqlopen/utplsql/CoverageResultImporter.kt index 585afaf4..7cf0a1da 100644 --- a/sonar-zpa-plugin/src/main/kotlin/org/sonar/plsqlopen/utplsql/CoverageResultImporter.kt +++ b/sonar-zpa-plugin/src/main/kotlin/org/sonar/plsqlopen/utplsql/CoverageResultImporter.kt @@ -20,6 +20,7 @@ package org.sonar.plsqlopen.utplsql import org.simpleframework.xml.core.Persister +import org.sonar.api.batch.fs.InputFile import org.sonar.api.batch.sensor.SensorContext import org.sonar.api.notifications.AnalysisWarnings import org.sonar.plsqlopen.symbols.ObjectLocator @@ -66,28 +67,46 @@ class CoverageResultImporter(private val objectLocator: ObjectLocator, if (inputFile != null) { logger.debug("The path ${file.path} was mapped to ${inputFile}") - val newCoverage = context.newCoverage().onFile(inputFile) + saveCoverage(context, inputFile, file, lineOffset, filePath) + } else { + logger.warn("The path ${file.path} was not found in the project") + } + } + } + + private fun saveCoverage( + context: SensorContext, + inputFile: InputFile, + file: CoveredFile, + lineOffset: Int, + filePath: String + ) { + val linesToCover = file.linesToCover + if (linesToCover != null) { + if (linesToCover.all { !it.covered }) { + // No need to save coverage for files with no covered lines + return + } - file.linesToCover?.forEach { line -> - val lineNumber = line.lineNumber + lineOffset - newCoverage.lineHits(lineNumber, if (line.covered) 1 else 0) + val newCoverage = context.newCoverage().onFile(inputFile) - val branchesToCover = line.branchesToCover - val coveredBranches = line.coveredBranches ?: 0 - if (branchesToCover != null) { - check(coveredBranches <= branchesToCover) { - "\"coveredBranches\" should not be greater than \"branchesToCover\" on line " + - "${line.lineNumber} for file \"$filePath\"" - } + file.linesToCover?.forEach { line -> + val lineNumber = line.lineNumber + lineOffset + newCoverage.lineHits(lineNumber, if (line.covered) 1 else 0) - newCoverage.conditions(lineNumber, branchesToCover, coveredBranches) + val branchesToCover = line.branchesToCover + val coveredBranches = line.coveredBranches ?: 0 + if (branchesToCover != null) { + check(coveredBranches <= branchesToCover) { + "\"coveredBranches\" should not be greater than \"branchesToCover\" on line " + + "${line.lineNumber} for file \"$filePath\"" } - } - newCoverage.save() - } else { - logger.warn("The path ${file.path} was not found in the project") + newCoverage.conditions(lineNumber, branchesToCover, coveredBranches) + } } + + newCoverage.save() } } diff --git a/sonar-zpa-plugin/src/test/kotlin/org/sonar/plsqlopen/utplsql/UtPlSqlSensorTest.kt b/sonar-zpa-plugin/src/test/kotlin/org/sonar/plsqlopen/utplsql/UtPlSqlSensorTest.kt index 0fdc69ed..236bd130 100644 --- a/sonar-zpa-plugin/src/test/kotlin/org/sonar/plsqlopen/utplsql/UtPlSqlSensorTest.kt +++ b/sonar-zpa-plugin/src/test/kotlin/org/sonar/plsqlopen/utplsql/UtPlSqlSensorTest.kt @@ -172,4 +172,35 @@ class UtPlSqlSensorTest { sensor.execute(context) verify(analysisWarnings).addUnique("No utPLSQL coverage report was found for sonar.zpa.coverage.reportPaths using pattern doesnotexists.xml") } + + @Test + fun shouldNotImportCoverageIfFileDoesNotContainCoveredLined() { + val relativePath = "betwnstr.sql" + val mainFile = TestInputFileBuilder("moduleKey", relativePath) + .setType(InputFile.Type.MAIN) + .setCharset(StandardCharsets.UTF_8) + .build() + context.fileSystem().add(mainFile) + + whenever(objectLocator.findMainObject(any(), any())).thenReturn( + MappedObject( + identifier = "", + objectType = PlSqlGrammar.CREATE_FUNCTION, + fileType = PlSqlFile.Type.MAIN, + path = mainFile.path(), + inputFile = mainFile, + firstLine = 3, + lastLine = 10 + ) + ) + + context.settings().setProperty(UtPlSqlSensor.COVERAGE_REPORT_PATH_KEY, "coverage-report-all-uncovered.xml") + sensor.execute(context) + + val key = mainFile.key() + assertThat(context.lineHits(key, 1)).isNull() + assertThat(context.lineHits(key, 2)).isNull() + assertThat(context.lineHits(key, 3)).isNull() + assertThat(context.lineHits(key, 4)).isNull() + } } diff --git a/sonar-zpa-plugin/src/test/resources/org/sonar/plsqlopen/utplsql/coverage-report-all-uncovered.xml b/sonar-zpa-plugin/src/test/resources/org/sonar/plsqlopen/utplsql/coverage-report-all-uncovered.xml new file mode 100644 index 00000000..1ff065e3 --- /dev/null +++ b/sonar-zpa-plugin/src/test/resources/org/sonar/plsqlopen/utplsql/coverage-report-all-uncovered.xml @@ -0,0 +1,9 @@ + + + + + + + + +