Skip to content

Commit 97ec5a3

Browse files
committed
Support AssertionError in SARIF report
1 parent 560ac03 commit 97ec5a3

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

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

+14-21
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,7 @@ import com.fasterxml.jackson.module.kotlin.readValue
66
import org.utbot.common.PathUtil.fileExtension
77
import org.utbot.common.PathUtil.toPath
88
import org.utbot.framework.UtSettings
9-
import org.utbot.framework.plugin.api.ExecutableId
10-
import org.utbot.framework.plugin.api.UtExecution
11-
import org.utbot.framework.plugin.api.UtExecutionFailure
12-
import org.utbot.framework.plugin.api.UtExecutionResult
13-
import org.utbot.framework.plugin.api.UtImplicitlyThrownException
14-
import org.utbot.framework.plugin.api.UtMethodTestSet
15-
import org.utbot.framework.plugin.api.UtModel
16-
import org.utbot.framework.plugin.api.UtOverflowFailure
17-
import org.utbot.framework.plugin.api.UtSymbolicExecution
9+
import org.utbot.framework.plugin.api.*
1810

1911
/**
2012
* Used for the SARIF report creation by given test cases and generated tests code.
@@ -74,10 +66,10 @@ class SarifReport(
7466
for (testSet in testSets) {
7567
for (execution in testSet.executions) {
7668
if (shouldProcessExecutionResult(execution.result)) {
77-
val (sarifResult, sarifRule) = processUncheckedException(
69+
val (sarifResult, sarifRule) = processExecutionFailure(
7870
method = testSet.method,
7971
utExecution = execution,
80-
uncheckedException = execution.result as UtExecutionFailure
72+
executionFailure = execution.result as UtExecutionFailure
8173
)
8274
sarifResults += sarifResult
8375
sarifRules += sarifRule
@@ -127,14 +119,14 @@ class SarifReport(
127119
return minimizedResults
128120
}
129121

130-
private fun processUncheckedException(
122+
private fun processExecutionFailure(
131123
method: ExecutableId,
132124
utExecution: UtExecution,
133-
uncheckedException: UtExecutionFailure
125+
executionFailure: UtExecutionFailure
134126
): Pair<SarifResult, SarifRule> {
135127

136-
val exceptionName = uncheckedException.exception::class.java.simpleName
137-
val ruleId = "utbot.unchecked.$exceptionName"
128+
val exceptionName = executionFailure.exception::class.java.simpleName
129+
val ruleId = "utbot.exception.$exceptionName"
138130

139131
val methodName = method.name
140132
val classFqn = method.classId.name
@@ -146,20 +138,20 @@ class SarifReport(
146138
Level.Error,
147139
Message(
148140
text = """
149-
Unchecked exception: ${uncheckedException.exception}.
141+
Unexpected exception: ${executionFailure.exception}.
150142
Test case: `$methodName($methodArguments)`
151143
[Generated test for this case]($relatedLocationId)
152144
""".trimIndent()
153145
),
154146
getLocations(utExecution, classFqn),
155147
getRelatedLocations(utExecution),
156-
getCodeFlows(method, utExecution, uncheckedException)
148+
getCodeFlows(method, utExecution, executionFailure)
157149
)
158150
val sarifRule = SarifRule(
159151
ruleId,
160152
exceptionName,
161153
SarifRule.Description(
162-
text = "Unchecked $exceptionName detected."
154+
text = "Unexpected $exceptionName detected."
163155
),
164156
SarifRule.Description(
165157
text = "Seems like an exception $exceptionName might be thrown."
@@ -205,7 +197,7 @@ class SarifReport(
205197
private fun getCodeFlows(
206198
method: ExecutableId,
207199
utExecution: UtExecution,
208-
uncheckedException: UtExecutionFailure
200+
executionFailure: UtExecutionFailure
209201
): List<SarifCodeFlow> {
210202
/* Example of a typical stack trace:
211203
- java.lang.Math.multiplyExact(Math.java:867)
@@ -215,7 +207,7 @@ class SarifReport(
215207
- sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
216208
- ...
217209
*/
218-
val stackTrace = uncheckedException.exception.stackTrace
210+
val stackTrace = executionFailure.exception.stackTrace
219211

220212
val lastMethodCallIndex = stackTrace.indexOfLast {
221213
it.className == method.classId.name && it.methodName == method.name
@@ -362,6 +354,7 @@ class SarifReport(
362354
private fun shouldProcessExecutionResult(result: UtExecutionResult): Boolean {
363355
val implicitlyThrown = result is UtImplicitlyThrownException
364356
val overflowFailure = result is UtOverflowFailure && UtSettings.treatOverflowAsError
365-
return implicitlyThrown || overflowFailure
357+
val assertionError = result is UtExplicitlyThrownException && result.exception is AssertionError
358+
return implicitlyThrown || overflowFailure || assertionError
366359
}
367360
}

0 commit comments

Comments
 (0)