Skip to content
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

Execute tool in sarif mode before report checking #498

Merged
merged 7 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,22 @@ class FixPlugin(

logDebug("Executing fix plugin in ${fixPluginConfig.actualFixFormat?.name} mode")

val (executionResult, adjustedTestCopyToExpectedFilesMap) = if (fixPluginConfig.actualFixFormat == ActualFixFormat.IN_PLACE) {
try {
// hold testCopyToExpectedFilesMap as is
pb.exec(execCmd, testConfig.getRootConfig().directory.toString(), redirectTo, time) to testCopyToExpectedFilesMap
} catch (ex: ProcessTimeoutException) {
logWarn("The following tests took too long to run and were stopped: ${chunk.map { it.test }}, timeout for single test: ${ex.timeoutMillis}")
return@map failTestResult(chunk, ex, execCmd)
} catch (ex: ProcessExecutionException) {
return@map failTestResult(chunk, ex, execCmd)
}
val executionResult = try {
pb.exec(execCmd, testConfig.getRootConfig().directory.toString(), redirectTo, time)
} catch (ex: ProcessTimeoutException) {
logWarn("The following tests took too long to run and were stopped: ${chunk.map { it.test }}, timeout for single test: ${ex.timeoutMillis}")
return@map failTestResult(chunk, ex, execCmd)
} catch (ex: ProcessExecutionException) {
return@map failTestResult(chunk, ex, execCmd)
}

val adjustedTestCopyToExpectedFilesMap = if (fixPluginConfig.actualFixFormat == ActualFixFormat.IN_PLACE) {
// hold testCopyToExpectedFilesMap as is
testCopyToExpectedFilesMap
} else {
// replace test files by modificated copies, obtained from sarif lib
// replace test files with modified copies, obtained from sarif lib
val fixedTestCopyToExpectedFilesMap = applyFixesFromSarif(fixPluginConfig, testsPaths, testCopyToExpectedFilesMap)
val dbgMsg = "Fixes were obtained from SARIF file, no debug info is available"
ExecutionResult(0, listOf(dbgMsg), listOf(dbgMsg)) to fixedTestCopyToExpectedFilesMap
fixedTestCopyToExpectedFilesMap
}

val stdout = executionResult.stdout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,29 +143,26 @@ class WarnPlugin(
val execCmd = cmdExecutor.constructExecCmd(tmpDirName)

val expectedWarningsMap = try {
collectExpectedWarnings(generalConfig, warnPluginConfig, originalPaths, copyPaths, workingDirectory)
} catch (ex: SarifParsingException) {
processExpectedWarnings(generalConfig, warnPluginConfig, originalPaths, copyPaths, workingDirectory)
}
Fixed Show fixed Hide fixed
catch (ex: SarifParsingException) {
return failTestResult(originalPaths, ex, execCmd)
}

if (expectedWarningsMap.isEmpty()) {
warnMissingExpectedWarnings(warnPluginConfig, generalConfig, originalPaths)
val executionResult = try {
cmdExecutor.execCmdAndGetExecutionResults(redirectTo)
} catch (ex: ProcessTimeoutException) {
logWarn("The following tests took too long to run and were stopped: $originalPaths, timeout for single test: ${ex.timeoutMillis}")
return failTestResult(originalPaths, ex, execCmd)
} catch (ex: ProcessExecutionException) {
return failTestResult(originalPaths, ex, execCmd)
}

val (actualWarningsMap, result) = try {
actualWarningsIfExistActualWarningsFile(warnPluginConfig, originalPaths, workingDirectory)
} catch (ex: SarifParsingException) {
val actualWarningsMap = try {
processActualWarnings(executionResult, warnPluginConfig, originalPaths, workingDirectory)
}
Fixed Show fixed Hide fixed
catch (ex: SarifParsingException) {
return failTestResult(originalPaths, ex, execCmd)
} ?: run {
val result = try {
cmdExecutor.execCmdAndGetExecutionResults(redirectTo)
} catch (ex: ProcessTimeoutException) {
logWarn("The following tests took too long to run and were stopped: $originalPaths, timeout for single test: ${ex.timeoutMillis}")
return failTestResult(originalPaths, ex, execCmd)
} catch (ex: ProcessExecutionException) {
return failTestResult(originalPaths, ex, execCmd)
}
collectActualWarningsWithLineNumbers(result, warnPluginConfig, workingDirectory) to result
}

val resultsChecker = ResultsChecker(
Expand All @@ -181,32 +178,15 @@ class WarnPlugin(
resultsStatus.first,
DebugInfo(
execCmd,
result.stdout.filter { it.contains(path.name) }.joinToString("\n"),
result.stderr.filter { it.contains(path.name) }.joinToString("\n"),
executionResult.stdout.filter { it.contains(path.name) }.joinToString("\n"),
executionResult.stderr.filter { it.contains(path.name) }.joinToString("\n"),
null,
resultsStatus.second,
),
)
}.asSequence()
}

private fun actualWarningsIfExistActualWarningsFile(
warnPluginConfig: WarnPluginConfig,
originalPaths: List<Path>,
workingDirectory: Path,
) = warnPluginConfig.actualWarningsFileName?.let {
val sarif = calculatePathToSarifFile(
sarifFileName = warnPluginConfig.actualWarningsFileName,
anchorTestFilePath = originalPaths.first()
)
val execResult = ExecutionResult(
0,
fs.readLines(sarif),
listOf("Warnings were obtained from SARIF file, no debug info is available")
)
collectActualWarningsWithLineNumbers(execResult, warnPluginConfig, workingDirectory) to execResult
}

private fun createTestFiles(paths: List<Path>, warnPluginConfig: WarnPluginConfig): List<Path> {
logDebug("Creating temp copy files of resources for WarnPlugin...")
logTrace("Trying to create temp files for: $paths")
Expand All @@ -230,6 +210,41 @@ class WarnPlugin(
}
}

private fun processExpectedWarnings(
generalConfig: GeneralConfig,
warnPluginConfig: WarnPluginConfig,
originalPaths: List<Path>,
copyPaths: List<Path>,
workingDirectory: Path,
): WarningMap {
val expectedWarningsMap = collectExpectedWarnings(generalConfig, warnPluginConfig, originalPaths, copyPaths, workingDirectory)

if (expectedWarningsMap.isEmpty()) {
warnMissingExpectedWarnings(warnPluginConfig, generalConfig, originalPaths)
}
return expectedWarningsMap
}

private fun processActualWarnings(
executionResult: ExecutionResult,
warnPluginConfig: WarnPluginConfig,
originalPaths: List<Path>,
workingDirectory: Path,
): WarningMap {
val actualResult = if (warnPluginConfig.actualWarningsFormat == ActualWarningsFormat.SARIF) {
// in this case, after tool execution, there was created sarif report, extract warnings from it,
// not from stdout
val sarif = calculatePathToSarifFile(
sarifFileName = warnPluginConfig.actualWarningsFileName!!,
anchorTestFilePath = originalPaths.first()
)
ExecutionResult(executionResult.code, fs.readLines(sarif), executionResult.stderr)
} else {
executionResult
}
return collectActualWarningsWithLineNumbers(actualResult, warnPluginConfig, workingDirectory)
}

private fun failTestResult(
paths: List<Path>,
ex: Exception,
Expand Down Expand Up @@ -317,7 +332,7 @@ class WarnPlugin(
.groupBy { it.fileName }
.mapValues { (_, warning) -> warning.sortedBy { it.message } }
} catch (e: Exception) {
throw SarifParsingException("We failed to parse sarif. Check the your tool generation of sarif report, cause: ${e.message}", e.cause)
throw SarifParsingException("Failed to parse sarif. Check the your tool generation of sarif report, cause: ${e.message}", e.cause)
}

else -> result.stdout.mapNotNull {
Expand Down