Fix incomplete results in the SARIF report by mmvpm · Pull Request #471 · UnitTestBot/UTBotJava · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,13 @@ class SarifReport(
*/
private val relatedLocationId = 1 // for attaching link to generated test in related locations

private fun shouldProcessUncheckedException(result: UtExecutionResult) = (result is UtImplicitlyThrownException)
|| ((result is UtOverflowFailure) && UtSettings.treatOverflowAsError)

private fun constructSarif(): Sarif {
val sarifResults = mutableListOf<SarifResult>()
val sarifRules = mutableSetOf<SarifRule>()

for (testCase in testCases) {
for (execution in testCase.executions) {
if (shouldProcessUncheckedException(execution.result)) {
if (shouldProcessExecutionResult(execution.result)) {
val (sarifResult, sarifRule) = processUncheckedException(
method = testCase.method,
utExecution = execution,
Expand Down Expand Up @@ -144,7 +141,7 @@ class SarifReport(
if (classFqn == null)
return listOf()
val sourceRelativePath = sourceFinding.getSourceRelativePath(classFqn)
val startLine = extractLineNumber(utExecution) ?: defaultLineNumber
val startLine = getLastLineNumber(utExecution) ?: defaultLineNumber
val sourceCode = sourceFinding.getSourceFile(classFqn)?.readText() ?: ""
val sourceRegion = SarifRegion.withStartLine(sourceCode, startLine)
return listOf(
Expand Down Expand Up @@ -301,10 +298,24 @@ class SarifReport(
return "..."
}

private fun extractLineNumber(utExecution: UtExecution): Int? =
try {
/**
* Returns the number of the last line in the execution path.
*/
private fun getLastLineNumber(utExecution: UtExecution): Int? {
Comment thread
mmvpm marked this conversation as resolved.
val lastPathLine = try {
utExecution.path.lastOrNull()?.stmt?.javaSourceStartLineNumber
} catch (t: Throwable) {
null
}
// if for some reason we can't extract the last line from the path
val lastCoveredInstruction =
utExecution.coverage?.coveredInstructions?.lastOrNull()?.lineNumber
return lastPathLine ?: lastCoveredInstruction
}

private fun shouldProcessExecutionResult(result: UtExecutionResult): Boolean {
val implicitlyThrown = result is UtImplicitlyThrownException
val overflowFailure = result is UtOverflowFailure && UtSettings.treatOverflowAsError
return implicitlyThrown || overflowFailure
}
}