Add sarif results minimization #490 by mmvpm · Pull Request #498 · UnitTestBot/UTBotJava · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 11 additions & 1 deletion utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt
36 changes: 35 additions & 1 deletion utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,45 @@ class SarifReport(
return Sarif.fromRun(
SarifRun(
SarifTool.fromRules(sarifRules.toList()),
sarifResults
minimizeResults(sarifResults)
)
)
}

/**
* Minimizes detected errors and removes duplicates.
*
* Between two [SarifResult]s with the same `ruleId` and `locations`
* it chooses the one with the shorter length of the execution trace.
*
* __Example:__
*
* The SARIF report for the code below contains only one unchecked exception in `methodB`.
* But without minimization, the report will contain two results: for `methodA` and for `methodB`.
*
* ```
* class Example {
* int methodA(int a) {
* return methodB(a);
* }
* int methodB(int b) {
* return 1 / b;
* }
* }
* ```
*/
private fun minimizeResults(sarifResults: List<SarifResult>): List<SarifResult> {
val groupedResults = sarifResults.groupBy { sarifResult ->
Pair(sarifResult.ruleId, sarifResult.locations)
Comment thread
sergeypospelov marked this conversation as resolved.
}
val minimizedResults = groupedResults.map { (_, sarifResultsGroup) ->
sarifResultsGroup.minByOrNull { sarifResult ->
sarifResult.totalCodeFlowLocations()
}!!
}
return minimizedResults
}

private fun processUncheckedException(
method: UtMethod<*>,
utExecution: UtExecution,
Expand Down
110 changes: 110 additions & 0 deletions utbot-framework/src/test/kotlin/org/utbot/sarif/SarifReportTest.kt