Skip to content

Commit

Permalink
Support getting sarif report from stdout in FixPlugin (#499)
Browse files Browse the repository at this point in the history
### What's done:
* Support getting sarif report from stdout in FixPlugin
  • Loading branch information
kgevorkyan authored Feb 16, 2023
1 parent c520f6b commit 4b10c24
Showing 1 changed file with 58 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("FILE_IS_TOO_LONG")

package com.saveourtool.save.plugins.fix

import com.saveourtool.save.core.config.ActualFixFormat
Expand Down Expand Up @@ -117,7 +119,12 @@ class FixPlugin(
testCopyToExpectedFilesMap
} else {
// replace test files with modified copies, obtained from sarif lib
val fixedTestCopyToExpectedFilesMap = applyFixesFromSarif(fixPluginConfig, testsPaths, testCopyToExpectedFilesMap)
val fixedTestCopyToExpectedFilesMap = applyFixesFromSarif(
executionResult.stdout,
fixPluginConfig,
testsPaths,
testCopyToExpectedFilesMap
)
fixedTestCopyToExpectedFilesMap
}

Expand Down Expand Up @@ -183,29 +190,34 @@ class FixPlugin(
/**
* In this case fixes would be provided by sarif library, which will extract appropriate fixes from SARIF file
*
* @param fixPluginConfig
* @param testsPaths path to tests file, which need to be modificated
* @param testCopyToExpectedFilesMap list of paths to the copy of tests files, which will be replaced by modificated files
* @param executionResultStdout sarif report data: if sarif file is not provided,
* supposed, that sarif report is present in stdout of execution result
* @param fixPluginConfig fix plugin configuration
* @param testsPaths path to tests file, which need to be modified
* @param testCopyToExpectedFilesMap list of paths to the copy of tests files, which will be replaced by modified files
* @return updated list of test files copies
*/
// TODO: support case, when sarif report is provided via stdout
private fun applyFixesFromSarif(
executionResultStdout: List<String>,
fixPluginConfig: FixPluginConfig,
testsPaths: List<Path>,
testCopyToExpectedFilesMap: List<PathPair>,
): List<PathPair> {
val sarif = calculatePathToSarifFile(
sarifFileName = fixPluginConfig.actualFixSarifFileName!!,
// Since we have one .sarif file for all tests, just take the first of them as anchor for calculation of paths
anchorTestFilePath = testsPaths.first()
)
// In this case fixes weren't performed by tool into the test files directly,
val tmpDirName = "${FixPlugin::class.simpleName!!}-${Random.nextInt()}".toPath()
val (sarifFile, isSarifFileEmulated) = getSarifFile(executionResultStdout, fixPluginConfig, testsPaths, tmpDirName)

// Fixes weren't performed by tool into the test files directly,
// instead, there was created sarif file with list of fixes, which we will apply ourselves
val fixedFiles = SarifFixAdapter(
sarifFile = sarif,
sarifFile = sarifFile,
targetFiles = testsPaths
).process()

// sarif file was created by us, remove tmp data
if (isSarifFileEmulated) {
fs.deleteRecursively(tmpDirName, mustExist = true)
}

// modify existing map, replace test copies to fixed test copies
val fixedTestCopyToExpectedFilesMap = testCopyToExpectedFilesMap.toMutableList().map { (testCopy, expected) ->
val fixedTestCopy = fixedFiles.first {
Expand All @@ -219,6 +231,40 @@ class FixPlugin(
return fixedTestCopyToExpectedFilesMap
}

@Suppress("SAY_NO_TO_VAR")
private fun getSarifFile(
executionResultStdout: List<String>,
fixPluginConfig: FixPluginConfig,
testsPaths: List<Path>,
tmpDirName: Path,
): Pair<Path, Boolean> {
var isSarifFileEmulated = false

val sarifFile = if (fixPluginConfig.actualFixSarifFileName != null) {
// get provided sarif file
calculatePathToSarifFile(
sarifFileName = fixPluginConfig.actualFixSarifFileName,
// Since we have one .sarif file for all tests, just take the first of them as anchor for calculation of paths
anchorTestFilePath = testsPaths.first()
)
} else {
// TODO: This case should be actually covered by tests too,
// TODO: however, need to find analysis tool which create sarif report with fixes in stdout
// sarif report is passed via stdout of executed tool
// since sarif-utils lib need a real sarif file for processing,
// emulate it here
isSarifFileEmulated = true
createTempDir(tmpDirName)
val sarifFile = fs.createFile(tmpDirName / "emulated-sarif-report.sarif")
fs.write(sarifFile) {
writeUtf8(executionResultStdout.joinToString("\n"))
}
sarifFile
}

return sarifFile to isSarifFileEmulated
}

/**
* For each chunk, build test results
*
Expand Down

0 comments on commit 4b10c24

Please sign in to comment.