Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add another way to find the last line (#460)
  • Loading branch information
mmvpm committed Jul 13, 2022
commit 82a4b261a44afb1bdc55bf6793dc1014f3ac0479
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ data class SarifRegion(
*/
fun withStartLine(text: String, startLine: Int): SarifRegion {
val neededLine = text.split('\n').getOrNull(startLine - 1) // to zero-based
val startColumn = neededLine?.let {
neededLine.takeWhile { it.toString().isBlank() }.length + 1 // to one-based
val startColumn = neededLine?.run {
takeWhile { it.toString().isBlank() }.length + 1 // to one-based
}
return SarifRegion(startLine = startLine, startColumn = startColumn)
}
Expand Down
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
}
}