Skip to content

Built-in SARIF reports visualizer #1023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ target/
.idea/
.gradle/
*.log
.rdgen
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class GenerateTestsCommand :
else -> {
val sourceFinding =
SourceFindingStrategyDefault(classFqn, sourceCodeFile, testsFilePath, projectRootPath)
val report = SarifReport(testSets, testClassBody, sourceFinding).createReport()
val report = SarifReport(testSets, testClassBody, sourceFinding).createReport().toJson()
saveToFile(report, sarifReport)
println("The report was saved to \"$sarifReport\".")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ object UtSettings : AbstractSettings(
*
* False by default (for saving disk space).
*/
var logConcreteExecutionErrors by getBooleanProperty(false)
var logConcreteExecutionErrors by getBooleanProperty(true)

/**
* Number of branch instructions using for clustering executions in the test minimization phase.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.utbot.sarif

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.junit.Test
import org.mockito.Mockito
import org.utbot.framework.plugin.api.ExecutableId
Expand All @@ -19,7 +17,7 @@ class SarifReportTest {
testSets = listOf(),
generatedTestsCode = "",
sourceFindingEmpty
).createReport()
).createReport().toJson()

assert(actualReport.isNotEmpty())
}
Expand All @@ -30,7 +28,7 @@ class SarifReportTest {
testSets = listOf(testSet),
generatedTestsCode = "",
sourceFindingEmpty
).createReport().toSarif()
).createReport()

assert(sarif.runs.first().results.isEmpty())
}
Expand Down Expand Up @@ -60,7 +58,7 @@ class SarifReportTest {
testSets = testSets,
generatedTestsCode = "",
sourceFindingEmpty
).createReport().toSarif()
).createReport()

assert(report.runs.first().results[0].message.text.contains("NullPointerException"))
assert(report.runs.first().results[1].message.text.contains("ArrayIndexOutOfBoundsException"))
Expand All @@ -77,7 +75,7 @@ class SarifReportTest {
Mockito.`when`(mockUtExecution.path.lastOrNull()?.stmt?.javaSourceStartLineNumber).thenReturn(1337)
Mockito.`when`(mockUtExecution.testMethodName).thenReturn("testMain_ThrowArithmeticException")

val report = sarifReportMain.createReport().toSarif()
val report = sarifReportMain.createReport()

val result = report.runs.first().results.first()
val location = result.locations.first().physicalLocation
Expand Down Expand Up @@ -105,7 +103,7 @@ class SarifReportTest {
)
)

val report = sarifReportMain.createReport().toSarif()
val report = sarifReportMain.createReport()

val result = report.runs.first().results.first()
assert(result.message.text.contains("227"))
Expand All @@ -128,7 +126,7 @@ class SarifReportTest {
)
Mockito.`when`(mockUtExecution.stateBefore.parameters).thenReturn(listOf())

val report = sarifReportMain.createReport().toSarif()
val report = sarifReportMain.createReport()

val result = report.runs.first().results.first().codeFlows.first().threadFlows.first().locations.map {
it.location.physicalLocation
Expand All @@ -153,7 +151,7 @@ class SarifReportTest {
Mockito.`when`(mockUtExecution.stateBefore.parameters).thenReturn(listOf())
Mockito.`when`(mockUtExecution.testMethodName).thenReturn("testMain_ThrowArithmeticException")

val report = sarifReportMain.createReport().toSarif()
val report = sarifReportMain.createReport()

val codeFlowPhysicalLocations = report.runs[0].results[0].codeFlows[0].threadFlows[0].locations.map {
it.location.physicalLocation
Expand All @@ -177,7 +175,7 @@ class SarifReportTest {
Mockito.`when`(mockUtExecution.stateBefore.parameters).thenReturn(listOf())
Mockito.`when`(mockUtExecution.testMethodName).thenReturn("testMain_ThrowArithmeticException")

val report = sarifReportPrivateMain.createReport().toSarif()
val report = sarifReportPrivateMain.createReport()

val codeFlowPhysicalLocations = report.runs[0].results[0].codeFlows[0].threadFlows[0].locations.map {
it.location.physicalLocation
Expand All @@ -203,7 +201,7 @@ class SarifReportTest {
testSets = testSets,
generatedTestsCode = "",
sourceFindingMain
).createReport().toSarif()
).createReport()

assert(report.runs.first().results.size == 1) // no duplicates
}
Expand All @@ -228,7 +226,7 @@ class SarifReportTest {
testSets = testSets,
generatedTestsCode = "",
sourceFindingMain
).createReport().toSarif()
).createReport()

assert(report.runs.first().results.size == 2) // no results have been removed
}
Expand Down Expand Up @@ -257,7 +255,7 @@ class SarifReportTest {
testSets = testSets,
generatedTestsCode = "",
sourceFindingMain
).createReport().toSarif()
).createReport()

assert(report.runs.first().results.size == 2) // no results have been removed
}
Expand Down Expand Up @@ -291,7 +289,7 @@ class SarifReportTest {
testSets = testSets,
generatedTestsCode = "",
sourceFindingMain
).createReport().toSarif()
).createReport()

assert(report.runs.first().results.size == 1) // no duplicates
assert(report.runs.first().results.first().totalCodeFlowLocations() == 1) // with a shorter stack trace
Expand All @@ -310,8 +308,6 @@ class SarifReportTest {
Mockito.`when`(mockExecutableId.classId.name).thenReturn("Main")
}

private fun String.toSarif(): Sarif = jacksonObjectMapper().readValue(this)

// constants

private val sourceFindingEmpty = SourceFindingStrategyDefault(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class GenerateTestsAndSarifReportFacade(
testClassBody: String,
sourceFinding: SourceFindingStrategy
) {
val sarifReport = SarifReport(testSets, testClassBody, sourceFinding).createReport()
val sarifReport = SarifReport(testSets, testClassBody, sourceFinding).createReport().toJson()
targetClass.sarifReportFile.writeText(sarifReport)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import org.utbot.rd.findRdPort
import org.utbot.rd.loggers.UtRdKLoggerFactory
import org.utbot.sarif.RdSourceFindingStrategyFacade
import org.utbot.sarif.SarifReport
import org.utbot.sarif.SourceFindingStrategyDefault
import org.utbot.summary.summarize
import soot.SootMethod
import soot.UnitPatchingChain
Expand Down Expand Up @@ -174,14 +175,31 @@ private fun EngineProcessModel.setup(
))
}
synchronizer.measureExecutionForTermination(writeSarifReport) { params ->
val lg = org.utbot.framework.process.logger
lg.error("START")
val reportFilePath = Paths.get(params.reportFilePath)
reportFilePath.toFile().writeText(
SarifReport(
testSets[params.testSetsId]!!,
params.generatedTestsCode,
RdSourceFindingStrategyFacade(realProtocol.rdSourceFindingStrategy)
).createReport()
repeat(10) { lg.error("EEEEEEEEEEEEEEEEEEEEEEEEEEE") }
lg.error(reportFilePath.toString()); repeat(5) { lg.error("\n") }
lg.error(testSets[params.testSetsId].toString()); repeat(5) { lg.error("\n") }
lg.error(params.generatedTestsCode); repeat(5) { lg.error("\n") }
// val rdSourceFindingStrategy = RdSourceFindingStrategyFacade(realProtocol.rdSourceFindingStrategy)
val sfs = SourceFindingStrategyDefault(
"io.github.ideaseeker.Main",
"C:/Users/sWX1137517/IdeaProjects/SarifTest/src/main/java/io/github/ideaseeker/Main.java",
"C:/Users/sWX1137517/IdeaProjects/SarifTest/src/test/java/io/github/ideaseeker/MainTest.java",
"C:/Users/sWX1137517/IdeaProjects/SarifTest/",
)
// lg.error(rdSourceFindingStrategy.toString()); repeat(5) { lg.error("\n") }
val sarifReportAsJson = SarifReport(
testSets[params.testSetsId]!!,
params.generatedTestsCode,
sfs
// rdSourceFindingStrategy
).createReport().toJson()
lg.error(sarifReportAsJson); repeat(5) { lg.error("\n") }
reportFilePath.toFile().writeText(sarifReportAsJson)
lg.error("Success!")
sarifReportAsJson
}
synchronizer.measureExecutionForTermination(generateTestReport) { params ->
val eventLogMessage = params.eventLogMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class EngineProcessModel private constructor(
private val _obtainClassId: RdCall<String, ByteArray>,
private val _findMethodsInClassMatchingSelected: RdCall<FindMethodsInClassMatchingSelectedArguments, FindMethodsInClassMatchingSelectedResult>,
private val _findMethodParamNames: RdCall<FindMethodParamNamesArguments, FindMethodParamNamesResult>,
private val _writeSarifReport: RdCall<WriteSarifReportArguments, Unit>,
private val _writeSarifReport: RdCall<WriteSarifReportArguments, String>,
private val _generateTestReport: RdCall<GenerateTestReportArgs, GenerateTestReportResult>
) : RdExtBase() {
//companion
Expand Down Expand Up @@ -73,7 +73,7 @@ class EngineProcessModel private constructor(
}


const val serializationHash = 4674749231408610997L
const val serializationHash = 1019152229801641560L

}
override val serializersOwner: ISerializersOwner get() = EngineProcessModel
Expand All @@ -89,7 +89,7 @@ class EngineProcessModel private constructor(
val obtainClassId: RdCall<String, ByteArray> get() = _obtainClassId
val findMethodsInClassMatchingSelected: RdCall<FindMethodsInClassMatchingSelectedArguments, FindMethodsInClassMatchingSelectedResult> get() = _findMethodsInClassMatchingSelected
val findMethodParamNames: RdCall<FindMethodParamNamesArguments, FindMethodParamNamesResult> get() = _findMethodParamNames
val writeSarifReport: RdCall<WriteSarifReportArguments, Unit> get() = _writeSarifReport
val writeSarifReport: RdCall<WriteSarifReportArguments, String> get() = _writeSarifReport
val generateTestReport: RdCall<GenerateTestReportArgs, GenerateTestReportResult> get() = _generateTestReport
//methods
//initializer
Expand Down Expand Up @@ -133,7 +133,7 @@ class EngineProcessModel private constructor(
RdCall<String, ByteArray>(FrameworkMarshallers.String, FrameworkMarshallers.ByteArray),
RdCall<FindMethodsInClassMatchingSelectedArguments, FindMethodsInClassMatchingSelectedResult>(FindMethodsInClassMatchingSelectedArguments, FindMethodsInClassMatchingSelectedResult),
RdCall<FindMethodParamNamesArguments, FindMethodParamNamesResult>(FindMethodParamNamesArguments, FindMethodParamNamesResult),
RdCall<WriteSarifReportArguments, Unit>(WriteSarifReportArguments, FrameworkMarshallers.Void),
RdCall<WriteSarifReportArguments, String>(WriteSarifReportArguments, FrameworkMarshallers.String),
RdCall<GenerateTestReportArgs, GenerateTestReportResult>(GenerateTestReportArgs, GenerateTestReportResult)
)

Expand Down
19 changes: 17 additions & 2 deletions utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.utbot.sarif

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonValue
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue

/**
* Useful links:
Expand All @@ -24,7 +27,19 @@ data class Sarif(

fun fromRun(run: SarifRun) =
Sarif(defaultSchema, defaultVersion, listOf(run))

fun fromJson(reportInJson: String): Sarif =
jacksonObjectMapper().readValue(reportInJson)
}

fun toJson(): String =
jacksonObjectMapper()
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.writerWithDefaultPrettyPrinter()
.writeValueAsString(this)

fun getAllResults(): List<SarifResult> =
runs.flatMap { it.results }
}

/**
Expand Down Expand Up @@ -104,8 +119,8 @@ data class SarifResult(
* Returns the total number of locations in all [codeFlows].
*/
fun totalCodeFlowLocations() =
codeFlows.sumBy { codeFlow ->
codeFlow.threadFlows.sumBy { threadFlow ->
codeFlows.sumOf { codeFlow ->
codeFlow.threadFlows.sumOf { threadFlow ->
threadFlow.locations.size
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.utbot.sarif

import kotlinx.coroutines.runBlocking
import org.utbot.engine.logger
import org.utbot.framework.process.generated.RdSourceFindingStrategy
import org.utbot.framework.process.generated.SourceStrategeMethodArgs
import java.io.File
Expand All @@ -10,11 +11,14 @@ class RdSourceFindingStrategyFacade(private val realStrategy: RdSourceFindingStr
get() = runBlocking { realStrategy.testsRelativePath.startSuspending(Unit) }

override fun getSourceRelativePath(classFqn: String, extension: String?): String = runBlocking {
logger.error("getSourceRelativePath for $classFqn, $extension")
realStrategy.getSourceRelativePath.startSuspending(SourceStrategeMethodArgs(classFqn, extension))
}

override fun getSourceFile(classFqn: String, extension: String?): File? = runBlocking {
logger.error("getSourceFile for $classFqn, $extension")
realStrategy.getSourceFile.startSuspending(SourceStrategeMethodArgs(classFqn, extension))?.let {
logger.error("it: $it")
File(it)
}
}
Expand Down
Loading