Skip to content

Commit 7856434

Browse files
authored
A hot fix for processing expected file (#573)
- rewritten reading expected warnings in a single file
1 parent 02e8d03 commit 7856434

File tree

5 files changed

+45
-15
lines changed

5 files changed

+45
-15
lines changed

save-plugins/fix-and-warn-plugin/src/commonTest/kotlin/com/saveourtool/save/plugins/fixandwarn/FixAndWarnPluginTest.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,19 @@ class FixAndWarnPluginTest {
7777
FixAndWarnPluginConfig(
7878
FixPluginConfig(fixExecutionCmd),
7979
WarnPluginConfig(warnExecutionCmd,
80-
Regex("(.+):(\\d+):(\\d+): (.+)"),
81-
true, true, 1, 2, 3, 1, 1, 1, 2, 3, 4
80+
actualWarningsPattern = Regex("(.+):(\\d+):(\\d+): (.+)"),
81+
warningTextHasLine = true,
82+
warningTextHasColumn = true,
83+
fileNameCaptureGroup = 1,
84+
lineCaptureGroup = null,
85+
columnCaptureGroup = 2,
86+
messageCaptureGroup = 3,
87+
messageCaptureGroupMiddle = 1,
88+
messageCaptureGroupEnd = 1,
89+
fileNameCaptureGroupOut = 1,
90+
lineCaptureGroupOut = 2,
91+
columnCaptureGroupOut = 3,
92+
messageCaptureGroupOut = 4,
8293
)
8394
),
8495
GeneralConfig("", 1, ", ", listOf(""), "", "", expectedWarningsPattern = Regex("// ;warn:(\\d+):(\\d+): (.*)"), runConfigPattern = defaultExtraConfigPattern)

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import com.saveourtool.save.plugin.warn.utils.collectWarningsFromSarif
3131
import com.saveourtool.save.plugin.warn.utils.collectionMultilineWarnings
3232
import com.saveourtool.save.plugin.warn.utils.collectionSingleWarnings
3333
import com.saveourtool.save.plugin.warn.utils.extractWarning
34-
import com.saveourtool.save.plugin.warn.utils.getLineNumber
3534

3635
import io.github.detekt.sarif4k.SarifSchema210
3736
import okio.FileSystem
@@ -276,11 +275,17 @@ class WarnPlugin(
276275
}
277276
return when (warnPluginConfig.expectedWarningsFormat) {
278277
ExpectedWarningsFormat.PLAIN -> {
279-
val warningsFromPlain = collectWarningsFromPlain(expectedWarningsFileName, originalPaths, fs) { plainFile ->
280-
plainFile.collectExpectedWarningsWithLineNumbers(
281-
warnPluginConfig,
282-
generalConfig
283-
)
278+
val warningsFromPlain = collectWarningsFromPlain(expectedWarningsFileName, originalPaths, fs) { line ->
279+
with(warnPluginConfig) {
280+
line.extractWarning(
281+
generalConfig.expectedWarningsPattern!!,
282+
fileNameCaptureGroup!!,
283+
lineCaptureGroup,
284+
columnCaptureGroup,
285+
messageCaptureGroup!!,
286+
benchmarkMode!!,
287+
)
288+
}
284289
}
285290
copyPaths.associate { copyPath ->
286291
copyPath.name to warningsFromPlain.filter { it.fileName == copyPath.name }
@@ -363,7 +368,7 @@ class WarnPlugin(
363368
it.extractWarning(
364369
actualWarningsPattern!!,
365370
fileNameCaptureGroupOut!!,
366-
it.getLineNumber(actualWarningsPattern, lineCaptureGroupOut),
371+
lineCaptureGroupOut,
367372
columnCaptureGroupOut,
368373
messageCaptureGroupOut!!,
369374
benchmarkMode!!

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import kotlinx.serialization.UseSerializers
2626
* @property actualWarningsPattern a regular expression by which warnings will be discovered in the process output
2727
* @property warningTextHasLine whether line number is included in [actualWarningsPattern]
2828
* @property warningTextHasColumn whether column number is included in [actualWarningsPattern]
29+
* @property fileNameCaptureGroup an index of capture group in regular expressions, corresponding to file number. Indices start at 0 with 0
30+
* corresponding to the whole string.
2931
* @property lineCaptureGroup an index of capture group in regular expressions, corresponding to line number. Indices start at 0 with 0
3032
* corresponding to the whole string.
3133
* @property columnCaptureGroup an index of capture group in regular expressions, corresponding to column number. Indices start at 0 with 0
@@ -65,6 +67,7 @@ data class WarnPluginConfig(
6567
val actualWarningsPattern: Regex? = null,
6668
val warningTextHasLine: Boolean? = null,
6769
val warningTextHasColumn: Boolean? = null,
70+
val fileNameCaptureGroup: Long? = null,
6871
val lineCaptureGroup: Long? = null,
6972
val columnCaptureGroup: Long? = null,
7073
val messageCaptureGroup: Long? = null,
@@ -115,6 +118,7 @@ data class WarnPluginConfig(
115118
this.actualWarningsPattern ?: other.actualWarningsPattern,
116119
this.warningTextHasLine ?: other.warningTextHasLine,
117120
this.warningTextHasColumn ?: other.warningTextHasColumn,
121+
this.fileNameCaptureGroup ?: other.fileNameCaptureGroup,
118122
this.lineCaptureGroup ?: other.lineCaptureGroup,
119123
this.columnCaptureGroup ?: other.columnCaptureGroup,
120124
this.messageCaptureGroup ?: other.messageCaptureGroup,
@@ -188,6 +192,7 @@ data class WarnPluginConfig(
188192
actualWarningsPattern ?: defaultOutputPattern,
189193
newWarningTextHasLine,
190194
newWarningTextHasColumn,
195+
fileNameCaptureGroup,
191196
newLineCaptureGroup,
192197
newColumnCaptureGroup,
193198
newMessageCaptureGroup,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ internal fun String.extractWarning(warningRegex: Regex,
9393
* @param columnGroupIdx index of capture group for column number
9494
* @param messageGroupIdx index of capture group for waring text
9595
* @param fileNameGroupIdx index of capture group for file name
96-
* @param lineNum line number of warning
96+
* @param lineNumGroupIdx index of capture group for line number of warning
9797
* @param benchmarkMode whether to ignore the warning messages
9898
* @return a [Warning] or null if [this] string doesn't match [warningRegex]
9999
* @throws ResourceFormatException when parsing a file
@@ -106,13 +106,14 @@ internal fun String.extractWarning(warningRegex: Regex,
106106
)
107107
internal fun String.extractWarning(warningRegex: Regex,
108108
fileNameGroupIdx: Long,
109-
lineNum: Int?,
109+
lineNumGroupIdx: Long?,
110110
columnGroupIdx: Long?,
111111
messageGroupIdx: Long,
112112
benchmarkMode: Boolean,
113113
): Warning? {
114114
val groups = warningRegex.find(this)?.groups ?: return null
115115
val fileName = getRegexGroupSafe(fileNameGroupIdx, groups, this, "file name")!!
116+
val lineNum = getRegexGroupSafe(lineNumGroupIdx, groups, this, "line number")?.toInt()
116117

117118
return extractWarning(warningRegex, fileName, lineNum, columnGroupIdx, messageGroupIdx, benchmarkMode)
118119
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,31 @@ internal fun collectionSingleWarnings(
100100
* @param plainFileName
101101
* @param originalPaths
102102
* @param fs
103-
* @param warningExtractor extractor of warning from [Path]
104-
* @return a list of warnings extracted from PLAIN file for test [file]
103+
* @param warningExtractor extractor of warning from [String]
104+
* @return a list of warnings extracted from PLAIN file for all tests
105105
* @throws PluginException
106106
*/
107107
internal fun collectWarningsFromPlain(
108108
plainFileName: String,
109109
originalPaths: List<Path>,
110110
fs: FileSystem,
111-
warningExtractor: (Path) -> List<Warning>,
111+
warningExtractor: (String) -> Warning?,
112112
): List<Warning> {
113113
// Since we have one <PLAIN> file for all tests, just take the first of them as anchor for calculation of paths
114114
val anchorTestFilePath = originalPaths.first()
115115
val plainFile = fs.findFileInAncestorDir(anchorTestFilePath, plainFileName) ?: throw PluginException(
116116
"Could not find PLAIN file with expected warnings/fixes for file $anchorTestFilePath. " +
117117
"Please check if correct `WarningsFormat`/`FixFormat` is set (should be PLAIN) and if the file is present and called `$plainFileName`."
118118
)
119-
return warningExtractor(plainFile)
119+
120+
return fs.read(plainFile) {
121+
generateSequence { readUtf8Line() }
122+
.map {
123+
warningExtractor(it)
124+
}
125+
.filterNotNull()
126+
.toList()
127+
}
120128
}
121129

122130
/**

0 commit comments

Comments
 (0)