Skip to content

Commit e7d0d24

Browse files
authored
Supported ExpectedWarningsFormat.PLAIN (#569)
- supported reading expected results from external plain file
1 parent 29a7c36 commit e7d0d24

File tree

5 files changed

+81
-26
lines changed

5 files changed

+81
-26
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ enum class ActualWarningsFormat {
1818
*/
1919
enum class ExpectedWarningsFormat {
2020
IN_PLACE,
21+
PLAIN,
2122
SARIF,
2223
;
2324
}

save-common/src/commonMain/kotlin/com/saveourtool/save/core/files/FileUtils.kt

+13
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,19 @@ fun FileSystem.findAncestorDirContainingFile(path: Path, fileName: String): Path
230230
}
231231
}
232232

233+
/**
234+
* Find a file in any of parent directories and return this file
235+
*
236+
* @param path path for which ancestors should be checked
237+
* @param fileName a name of the file that will be searched for
238+
* @return a path to a file with name [fileName] or null
239+
*/
240+
fun FileSystem.findFileInAncestorDir(path: Path, fileName: String): Path? = findAncestorDirContainingFile(
241+
path, fileName
242+
)?.let {
243+
it / fileName
244+
}
245+
233246
/**
234247
* @return current working directory
235248
*/

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
package com.saveourtool.save.core.utils
66

7-
import com.saveourtool.save.core.files.findAncestorDirContainingFile
7+
import com.saveourtool.save.core.files.findFileInAncestorDir
88
import com.saveourtool.save.core.files.fs
99
import com.saveourtool.save.core.files.parents
1010
import com.saveourtool.save.core.plugin.PluginException
@@ -51,11 +51,9 @@ fun FileSystem.topmostTestDirectory(path: Path): Path = path.parents().last { pa
5151
* @return path to sarif
5252
* @throws PluginException in case of absence of sarif file
5353
*/
54-
fun calculatePathToSarifFile(sarifFileName: String, anchorTestFilePath: Path): Path = fs.findAncestorDirContainingFile(
54+
fun calculatePathToSarifFile(sarifFileName: String, anchorTestFilePath: Path): Path = fs.findFileInAncestorDir(
5555
anchorTestFilePath, sarifFileName
56-
)?.let {
57-
it / sarifFileName
58-
} ?: throw PluginException(
56+
) ?: throw PluginException(
5957
"Could not find SARIF file with expected warnings/fixes for file $anchorTestFilePath. " +
60-
"Please check if correct `FarningsFormat`/`FixFormat` is set (should be SARIF) and if the file is present and called `$sarifFileName`."
58+
"Please check if correct `WarningsFormat`/`FixFormat` is set (should be SARIF) and if the file is present and called `$sarifFileName`."
6159
)

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

+37-15
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.saveourtool.save.plugin.warn.sarif.toWarnings
2626
import com.saveourtool.save.plugin.warn.utils.CmdExecutorWarn
2727
import com.saveourtool.save.plugin.warn.utils.ResultsChecker
2828
import com.saveourtool.save.plugin.warn.utils.Warning
29+
import com.saveourtool.save.plugin.warn.utils.collectWarningsFromPlain
2930
import com.saveourtool.save.plugin.warn.utils.collectWarningsFromSarif
3031
import com.saveourtool.save.plugin.warn.utils.collectionMultilineWarnings
3132
import com.saveourtool.save.plugin.warn.utils.collectionSingleWarnings
@@ -37,7 +38,6 @@ import okio.FileSystem
3738
import okio.Path
3839

3940
import kotlin.random.Random
40-
import kotlinx.serialization.decodeFromString
4141
import kotlinx.serialization.json.Json
4242

4343
private typealias WarningMap = Map<String, List<Warning>>
@@ -258,30 +258,52 @@ class WarnPlugin(
258258
)
259259
}.asSequence()
260260

261-
@Suppress("TooGenericExceptionCaught", "SwallowedException")
261+
@Suppress(
262+
"TooGenericExceptionCaught",
263+
"SwallowedException",
264+
"TOO_LONG_FUNCTION"
265+
)
262266
private fun collectExpectedWarnings(
263267
generalConfig: GeneralConfig,
264268
warnPluginConfig: WarnPluginConfig,
265269
originalPaths: List<Path>,
266270
copyPaths: List<Path>,
267271
workingDirectory: Path,
268-
): WarningMap = if (warnPluginConfig.expectedWarningsFormat == ExpectedWarningsFormat.SARIF) {
269-
val warningsFromSarif = try {
270-
collectWarningsFromSarif(warnPluginConfig, originalPaths, fs, workingDirectory)
271-
} catch (e: Exception) {
272-
throw SarifParsingException("We failed to parse sarif. Check the your tool generation of sarif report, cause: ${e.message}", e.cause)
273-
}
274-
copyPaths.associate { copyPath ->
275-
copyPath.name to warningsFromSarif.filter { it.fileName == copyPath.name }
272+
): WarningMap {
273+
val expectedWarningsFileName: String by lazy {
274+
warnPluginConfig.expectedWarningsFileName
275+
?: throw IllegalArgumentException("<expectedWarningsFileName> is not provided for expectedWarningsFormat=${warnPluginConfig.expectedWarningsFormat}")
276276
}
277-
} else {
278-
copyPaths.associate { copyPath ->
279-
val warningsForCurrentPath =
280-
copyPath.collectExpectedWarningsWithLineNumbers(
277+
return when (warnPluginConfig.expectedWarningsFormat) {
278+
ExpectedWarningsFormat.PLAIN -> {
279+
val warningsFromPlain = collectWarningsFromPlain(expectedWarningsFileName, originalPaths, fs) { plainFile ->
280+
plainFile.collectExpectedWarningsWithLineNumbers(
281281
warnPluginConfig,
282282
generalConfig
283283
)
284-
copyPath.name to warningsForCurrentPath
284+
}
285+
copyPaths.associate { copyPath ->
286+
copyPath.name to warningsFromPlain.filter { it.fileName == copyPath.name }
287+
}
288+
}
289+
ExpectedWarningsFormat.SARIF -> {
290+
val warningsFromSarif = try {
291+
collectWarningsFromSarif(expectedWarningsFileName, originalPaths, fs, workingDirectory)
292+
} catch (e: Exception) {
293+
throw SarifParsingException("We failed to parse sarif. Check the your tool generation of sarif report, cause: ${e.message}", e.cause)
294+
}
295+
copyPaths.associate { copyPath ->
296+
copyPath.name to warningsFromSarif.filter { it.fileName == copyPath.name }
297+
}
298+
}
299+
else -> copyPaths.associate { copyPath ->
300+
val warningsForCurrentPath =
301+
copyPath.collectExpectedWarningsWithLineNumbers(
302+
warnPluginConfig,
303+
generalConfig
304+
)
305+
copyPath.name to warningsForCurrentPath
306+
}
285307
}
286308
}
287309

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

+26-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

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

8+
import com.saveourtool.save.core.files.findFileInAncestorDir
89
import com.saveourtool.save.core.files.readFile
910
import com.saveourtool.save.core.plugin.GeneralConfig
1011
import com.saveourtool.save.core.plugin.PluginException
@@ -18,7 +19,6 @@ import io.github.detekt.sarif4k.SarifSchema210
1819
import okio.FileSystem
1920
import okio.Path
2021

21-
import kotlinx.serialization.decodeFromString
2222
import kotlinx.serialization.json.Json
2323

2424
/**
@@ -97,21 +97,42 @@ internal fun collectionSingleWarnings(
9797
.sortedBy { warn -> warn.message }
9898

9999
/**
100-
* @param warnPluginConfig
100+
* @param plainFileName
101+
* @param originalPaths
102+
* @param fs
103+
* @param warningExtractor extractor of warning from [Path]
104+
* @return a list of warnings extracted from PLAIN file for test [file]
105+
* @throws PluginException
106+
*/
107+
internal fun collectWarningsFromPlain(
108+
plainFileName: String,
109+
originalPaths: List<Path>,
110+
fs: FileSystem,
111+
warningExtractor: (Path) -> List<Warning>,
112+
): List<Warning> {
113+
// Since we have one <PLAIN> file for all tests, just take the first of them as anchor for calculation of paths
114+
val anchorTestFilePath = originalPaths.first()
115+
val plainFile = fs.findFileInAncestorDir(anchorTestFilePath, plainFileName) ?: throw PluginException(
116+
"Could not find PLAIN file with expected warnings/fixes for file $anchorTestFilePath. " +
117+
"Please check if correct `WarningsFormat`/`FixFormat` is set (should be PLAIN) and if the file is present and called `$plainFileName`."
118+
)
119+
return warningExtractor(plainFile)
120+
}
121+
122+
/**
123+
* @param sarifFileName
101124
* @param originalPaths
102125
* @param fs
103126
* @param workingDirectory initial working directory, when SAVE started
104127
* @return a list of warnings extracted from SARIF file for test [file]
105128
* @throws PluginException
106129
*/
107130
internal fun collectWarningsFromSarif(
108-
warnPluginConfig: WarnPluginConfig,
131+
sarifFileName: String,
109132
originalPaths: List<Path>,
110133
fs: FileSystem,
111134
workingDirectory: Path,
112135
): List<Warning> {
113-
val sarifFileName = warnPluginConfig.expectedWarningsFileName!!
114-
115136
// Since we have one .sarif file for all tests, just take the first of them as anchor for calculation of paths
116137
val anchorTestFilePath = originalPaths.first()
117138
val sarif = calculatePathToSarifFile(

0 commit comments

Comments
 (0)