@@ -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