Skip to content

Commit cc43bc3

Browse files
committed
Add another way to find the last line (#460)
1 parent e150db0 commit cc43bc3

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ data class SarifRegion(
156156
*/
157157
fun withStartLine(text: String, startLine: Int): SarifRegion {
158158
val neededLine = text.split('\n').getOrNull(startLine - 1) // to zero-based
159-
val startColumn = neededLine?.let {
160-
neededLine.takeWhile { it.toString().isBlank() }.length + 1 // to one-based
159+
val startColumn = neededLine?.run {
160+
takeWhile { it.toString().isBlank() }.length + 1 // to one-based
161161
}
162162
return SarifRegion(startLine = startLine, startColumn = startColumn)
163163
}

utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,13 @@ class SarifReport(
6666
*/
6767
private val relatedLocationId = 1 // for attaching link to generated test in related locations
6868

69-
private fun shouldProcessUncheckedException(result: UtExecutionResult) = (result is UtImplicitlyThrownException)
70-
|| ((result is UtOverflowFailure) && UtSettings.treatOverflowAsError)
71-
7269
private fun constructSarif(): Sarif {
7370
val sarifResults = mutableListOf<SarifResult>()
7471
val sarifRules = mutableSetOf<SarifRule>()
7572

7673
for (testCase in testCases) {
7774
for (execution in testCase.executions) {
78-
if (shouldProcessUncheckedException(execution.result)) {
75+
if (shouldProcessExecutionResult(execution.result)) {
7976
val (sarifResult, sarifRule) = processUncheckedException(
8077
method = testCase.method,
8178
utExecution = execution,
@@ -144,7 +141,7 @@ class SarifReport(
144141
if (classFqn == null)
145142
return listOf()
146143
val sourceRelativePath = sourceFinding.getSourceRelativePath(classFqn)
147-
val startLine = extractLineNumber(utExecution) ?: defaultLineNumber
144+
val startLine = getLastLineNumber(utExecution) ?: defaultLineNumber
148145
val sourceCode = sourceFinding.getSourceFile(classFqn)?.readText() ?: ""
149146
val sourceRegion = SarifRegion.withStartLine(sourceCode, startLine)
150147
return listOf(
@@ -301,10 +298,24 @@ class SarifReport(
301298
return "..."
302299
}
303300

304-
private fun extractLineNumber(utExecution: UtExecution): Int? =
305-
try {
301+
/**
302+
* Returns the number of the last line in the execution path.
303+
*/
304+
private fun getLastLineNumber(utExecution: UtExecution): Int? {
305+
val lastPathLine = try {
306306
utExecution.path.lastOrNull()?.stmt?.javaSourceStartLineNumber
307307
} catch (t: Throwable) {
308308
null
309309
}
310+
// if for some reason we can't extract the last line from the path
311+
val lastCoveredInstruction =
312+
utExecution.coverage?.coveredInstructions?.lastOrNull()?.lineNumber
313+
return lastPathLine ?: lastCoveredInstruction
314+
}
315+
316+
private fun shouldProcessExecutionResult(result: UtExecutionResult): Boolean {
317+
val implicitlyThrown = result is UtImplicitlyThrownException
318+
val overflowFailure = result is UtOverflowFailure && UtSettings.treatOverflowAsError
319+
return implicitlyThrown || overflowFailure
320+
}
310321
}

0 commit comments

Comments
 (0)