@@ -66,16 +66,13 @@ class SarifReport(
66
66
*/
67
67
private val relatedLocationId = 1 // for attaching link to generated test in related locations
68
68
69
- private fun shouldProcessUncheckedException (result : UtExecutionResult ) = (result is UtImplicitlyThrownException )
70
- || ((result is UtOverflowFailure ) && UtSettings .treatOverflowAsError)
71
-
72
69
private fun constructSarif (): Sarif {
73
70
val sarifResults = mutableListOf<SarifResult >()
74
71
val sarifRules = mutableSetOf<SarifRule >()
75
72
76
73
for (testCase in testCases) {
77
74
for (execution in testCase.executions) {
78
- if (shouldProcessUncheckedException (execution.result)) {
75
+ if (shouldProcessExecutionResult (execution.result)) {
79
76
val (sarifResult, sarifRule) = processUncheckedException(
80
77
method = testCase.method,
81
78
utExecution = execution,
@@ -144,7 +141,7 @@ class SarifReport(
144
141
if (classFqn == null )
145
142
return listOf ()
146
143
val sourceRelativePath = sourceFinding.getSourceRelativePath(classFqn)
147
- val startLine = extractLineNumber (utExecution) ? : defaultLineNumber
144
+ val startLine = getLastLineNumber (utExecution) ? : defaultLineNumber
148
145
val sourceCode = sourceFinding.getSourceFile(classFqn)?.readText() ? : " "
149
146
val sourceRegion = SarifRegion .withStartLine(sourceCode, startLine)
150
147
return listOf (
@@ -301,10 +298,24 @@ class SarifReport(
301
298
return " ..."
302
299
}
303
300
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 {
306
306
utExecution.path.lastOrNull()?.stmt?.javaSourceStartLineNumber
307
307
} catch (t: Throwable ) {
308
308
null
309
309
}
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
+ }
310
321
}
0 commit comments