Skip to content

Commit

Permalink
Merge branch 'main' into kononov-summarization-timeout-smart
Browse files Browse the repository at this point in the history
  • Loading branch information
Domonion authored Oct 14, 2022
2 parents b95d74a + 31ae5b1 commit 5208c5f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 54 deletions.
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
@@ -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 @@ -181,7 +181,7 @@ private fun EngineProcessModel.setup(
testSets[params.testSetsId]!!,
params.generatedTestsCode,
RdSourceFindingStrategyFacade(realProtocol.rdSourceFindingStrategy)
).createReport()
).createReport().toJson()
)
}
synchronizer.measureExecutionForTermination(generateTestReport) { params ->
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
52 changes: 19 additions & 33 deletions utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.utbot.sarif

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.utbot.common.PathUtil.fileExtension
import org.utbot.common.PathUtil.toPath
import org.utbot.framework.UtSettings
import org.utbot.framework.plugin.api.*
import kotlin.io.path.nameWithoutExtension

/**
* Used for the SARIF report creation by given test cases and generated tests code.
Expand All @@ -21,45 +19,20 @@ class SarifReport(
private val generatedTestsCode: String,
private val sourceFinding: SourceFindingStrategy
) {

companion object {

/**
* Merges several SARIF reports given as JSON-strings into one
*/
fun mergeReports(reports: List<String>): String =
reports.fold(Sarif.empty()) { sarif: Sarif, report: String ->
sarif.copy(runs = sarif.runs + report.jsonToSarif().runs)
}.sarifToJson()

// internal

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

private fun Sarif.sarifToJson(): String =
jacksonObjectMapper()
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.writerWithDefaultPrettyPrinter()
.writeValueAsString(this)
sarif.copy(runs = sarif.runs + Sarif.fromJson(report).runs)
}.toJson()
}

/**
* Creates a SARIF report and returns it as string
*/
fun createReport(): String =
constructSarif().sarifToJson()

// internal

private val defaultLineNumber = 1 // if the line in the source code where the exception is thrown is unknown

/**
* [Read more about links to locations](https://github.com/microsoft/sarif-tutorials/blob/main/docs/3-Beyond-basics.md#msg-links-location)
* Creates a SARIF report.
*/
private val relatedLocationId = 1 // for attaching link to generated test in related locations

private fun constructSarif(): Sarif {
fun createReport(): Sarif {
val sarifResults = mutableListOf<SarifResult>()
val sarifRules = mutableSetOf<SarifRule>()

Expand All @@ -85,6 +58,15 @@ class SarifReport(
)
}

// internal

private val defaultLineNumber = 1 // if the line in the source code where the exception is thrown is unknown

/**
* [Read more about links to locations](https://github.com/microsoft/sarif-tutorials/blob/main/docs/3-Beyond-basics.md#msg-links-location)
*/
private val relatedLocationId = 1 // for attaching link to generated test in related locations

/**
* Minimizes detected errors and removes duplicates.
*
Expand Down Expand Up @@ -227,10 +209,14 @@ class SarifReport(
val methodCallLocation: SarifPhysicalLocation? =
findMethodCallInTestBody(utExecution.testMethodName, method.name)
if (methodCallLocation != null) {
val testFileName = sourceFinding.testsRelativePath.toPath().fileName
val testClassName = testFileName.nameWithoutExtension
val testMethodName = utExecution.testMethodName
val methodCallLineNumber = methodCallLocation.region.startLine
val methodCallLocationWrapper = SarifFlowLocationWrapper(
SarifFlowLocation(
message = Message(
text = "${sourceFinding.testsRelativePath.toPath().fileName}:${methodCallLocation.region.startLine}"
text = "$testClassName.$testMethodName($testFileName:$methodCallLineNumber)"
),
physicalLocation = methodCallLocation
)
Expand Down

0 comments on commit 5208c5f

Please sign in to comment.