Skip to content

Commit

Permalink
fix: Ignore coverage for files with no covered lines
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed May 27, 2024
1 parent a2983d0 commit 776c766
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<coverage version="1">
<file path="function ut3_demo.betwnstr">
<lineToCover lineNumber="1" covered="false"/>
<lineToCover lineNumber="2" covered="false"/>
<lineToCover lineNumber="3" covered="false"/>
<lineToCover lineNumber="4" covered="false"/>
</file>
</coverage>

0 comments on commit 776c766

Please sign in to comment.