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

Supported ExpectedWarningsFormat.PLAIN #569

Merged
merged 4 commits into from
Feb 28, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
enum class ExpectedWarningsFormat {
IN_PLACE,
PLAIN,

Check warning on line 21 in save-common/src/commonMain/kotlin/com/saveourtool/save/core/config/WarningFormats.kt

View check run for this annotation

Codecov / codecov/patch

save-common/src/commonMain/kotlin/com/saveourtool/save/core/config/WarningFormats.kt#L21

Added line #L21 was not covered by tests
SARIF,
;
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ fun FileSystem.findAncestorDirContainingFile(path: Path, fileName: String): Path
}
}

/**
* Find a file in any of parent directories and return this file
*
* @param path path for which ancestors should be checked
* @param fileName a name of the file that will be searched for
* @return a path to a file with name [fileName] or null
*/
fun FileSystem.findFileInAncestorDir(path: Path, fileName: String): Path? = findAncestorDirContainingFile(
path, fileName
)?.let {
it / fileName
}

/**
* @return current working directory
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package com.saveourtool.save.core.utils

import com.saveourtool.save.core.files.findAncestorDirContainingFile
import com.saveourtool.save.core.files.findFileInAncestorDir
import com.saveourtool.save.core.files.fs
import com.saveourtool.save.core.files.parents
import com.saveourtool.save.core.plugin.PluginException
Expand Down Expand Up @@ -51,11 +51,9 @@
* @return path to sarif
* @throws PluginException in case of absence of sarif file
*/
fun calculatePathToSarifFile(sarifFileName: String, anchorTestFilePath: Path): Path = fs.findAncestorDirContainingFile(
fun calculatePathToSarifFile(sarifFileName: String, anchorTestFilePath: Path): Path = fs.findFileInAncestorDir(
anchorTestFilePath, sarifFileName
)?.let {
it / sarifFileName
} ?: throw PluginException(
) ?: throw PluginException(

Check warning on line 56 in save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt

View check run for this annotation

Codecov / codecov/patch

save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt#L56

Added line #L56 was not covered by tests
"Could not find SARIF file with expected warnings/fixes for file $anchorTestFilePath. " +
"Please check if correct `FarningsFormat`/`FixFormat` is set (should be SARIF) and if the file is present and called `$sarifFileName`."
"Please check if correct `WarningsFormat`/`FixFormat` is set (should be SARIF) and if the file is present and called `$sarifFileName`."

Check warning on line 58 in save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt

View check run for this annotation

Codecov / codecov/patch

save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt#L58

Added line #L58 was not covered by tests
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.saveourtool.save.plugin.warn.utils.CmdExecutorWarn
import com.saveourtool.save.plugin.warn.utils.ResultsChecker
import com.saveourtool.save.plugin.warn.utils.Warning
import com.saveourtool.save.plugin.warn.utils.collectWarningsFromPlain
import com.saveourtool.save.plugin.warn.utils.collectWarningsFromSarif
import com.saveourtool.save.plugin.warn.utils.collectionMultilineWarnings
import com.saveourtool.save.plugin.warn.utils.collectionSingleWarnings
Expand All @@ -37,7 +38,6 @@
import okio.Path

import kotlin.random.Random
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

private typealias WarningMap = Map<String, List<Warning>>
Expand Down Expand Up @@ -258,30 +258,52 @@
)
}.asSequence()

@Suppress("TooGenericExceptionCaught", "SwallowedException")
@Suppress(
"TooGenericExceptionCaught",
"SwallowedException",
"TOO_LONG_FUNCTION"
)
private fun collectExpectedWarnings(
generalConfig: GeneralConfig,
warnPluginConfig: WarnPluginConfig,
originalPaths: List<Path>,
copyPaths: List<Path>,
workingDirectory: Path,
): WarningMap = if (warnPluginConfig.expectedWarningsFormat == ExpectedWarningsFormat.SARIF) {
val warningsFromSarif = try {
collectWarningsFromSarif(warnPluginConfig, originalPaths, fs, workingDirectory)
} catch (e: Exception) {
throw SarifParsingException("We failed to parse sarif. Check the your tool generation of sarif report, cause: ${e.message}", e.cause)
}
copyPaths.associate { copyPath ->
copyPath.name to warningsFromSarif.filter { it.fileName == copyPath.name }
): WarningMap {
val expectedWarningsFileName: String by lazy {
warnPluginConfig.expectedWarningsFileName
?: throw IllegalArgumentException("<expectedWarningsFileName> is not provided for expectedWarningsFormat=${warnPluginConfig.expectedWarningsFormat}")

Check warning on line 275 in save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/WarnPlugin.kt

View check run for this annotation

Codecov / codecov/patch

save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/WarnPlugin.kt#L275

Added line #L275 was not covered by tests
nulls marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
copyPaths.associate { copyPath ->
val warningsForCurrentPath =
copyPath.collectExpectedWarningsWithLineNumbers(
return when (warnPluginConfig.expectedWarningsFormat) {
ExpectedWarningsFormat.PLAIN -> {
val warningsFromPlain = collectWarningsFromPlain(expectedWarningsFileName, originalPaths, fs) { plainFile ->
plainFile.collectExpectedWarningsWithLineNumbers(

Check warning on line 280 in save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/WarnPlugin.kt

View check run for this annotation

Codecov / codecov/patch

save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/WarnPlugin.kt#L279-L280

Added lines #L279 - L280 were not covered by tests
warnPluginConfig,
generalConfig
)
copyPath.name to warningsForCurrentPath
}
copyPaths.associate { copyPath ->
copyPath.name to warningsFromPlain.filter { it.fileName == copyPath.name }

Check warning on line 286 in save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/WarnPlugin.kt

View check run for this annotation

Codecov / codecov/patch

save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/WarnPlugin.kt#L285-L286

Added lines #L285 - L286 were not covered by tests
}
}
Fixed Show fixed Hide fixed
ExpectedWarningsFormat.SARIF -> {
val warningsFromSarif = try {
collectWarningsFromSarif(expectedWarningsFileName, originalPaths, fs, workingDirectory)
} catch (e: Exception) {
throw SarifParsingException("We failed to parse sarif. Check the your tool generation of sarif report, cause: ${e.message}", e.cause)

Check warning on line 293 in save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/WarnPlugin.kt

View check run for this annotation

Codecov / codecov/patch

save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/WarnPlugin.kt#L290-L293

Added lines #L290 - L293 were not covered by tests
}
copyPaths.associate { copyPath ->
copyPath.name to warningsFromSarif.filter { it.fileName == copyPath.name }

Check warning on line 296 in save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/WarnPlugin.kt

View check run for this annotation

Codecov / codecov/patch

save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/WarnPlugin.kt#L295-L296

Added lines #L295 - L296 were not covered by tests
}
}
else -> copyPaths.associate { copyPath ->
val warningsForCurrentPath =
copyPath.collectExpectedWarningsWithLineNumbers(
warnPluginConfig,
generalConfig
)
copyPath.name to warningsForCurrentPath
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package com.saveourtool.save.plugin.warn.utils

import com.saveourtool.save.core.files.findFileInAncestorDir
import com.saveourtool.save.core.files.readFile
import com.saveourtool.save.core.plugin.GeneralConfig
import com.saveourtool.save.core.plugin.PluginException
Expand All @@ -18,7 +19,6 @@
import okio.FileSystem
import okio.Path

import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

/**
Expand Down Expand Up @@ -97,21 +97,42 @@
.sortedBy { warn -> warn.message }

/**
* @param warnPluginConfig
* @param plainFileName
* @param originalPaths
* @param fs
* @param warningExtractor extractor of warning from [Path]
* @return a list of warnings extracted from PLAIN file for test [file]
* @throws PluginException
*/
internal fun collectWarningsFromPlain(
plainFileName: String,
originalPaths: List<Path>,
fs: FileSystem,
warningExtractor: (Path) -> List<Warning>,
): List<Warning> {
// Since we have one <PLAIN> file for all tests, just take the first of them as anchor for calculation of paths
val anchorTestFilePath = originalPaths.first()

Check warning on line 114 in save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt

View check run for this annotation

Codecov / codecov/patch

save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt#L114

Added line #L114 was not covered by tests
val plainFile = fs.findFileInAncestorDir(anchorTestFilePath, plainFileName) ?: throw PluginException(
"Could not find PLAIN file with expected warnings/fixes for file $anchorTestFilePath. " +
"Please check if correct `WarningsFormat`/`FixFormat` is set (should be PLAIN) and if the file is present and called `$plainFileName`."

Check warning on line 117 in save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt

View check run for this annotation

Codecov / codecov/patch

save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt#L116-L117

Added lines #L116 - L117 were not covered by tests
)
return warningExtractor(plainFile)

Check warning on line 119 in save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt

View check run for this annotation

Codecov / codecov/patch

save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt#L119

Added line #L119 was not covered by tests
}

/**
* @param sarifFileName
* @param originalPaths
* @param fs
* @param workingDirectory initial working directory, when SAVE started
* @return a list of warnings extracted from SARIF file for test [file]
* @throws PluginException
*/
internal fun collectWarningsFromSarif(
warnPluginConfig: WarnPluginConfig,
sarifFileName: String,
originalPaths: List<Path>,
fs: FileSystem,
workingDirectory: Path,
): List<Warning> {
val sarifFileName = warnPluginConfig.expectedWarningsFileName!!

// Since we have one .sarif file for all tests, just take the first of them as anchor for calculation of paths
val anchorTestFilePath = originalPaths.first()
val sarif = calculatePathToSarifFile(
Expand Down
Loading