Skip to content

Commit

Permalink
A hot fix for processing expected file (#573)
Browse files Browse the repository at this point in the history
- rewritten reading expected warnings in a single file
  • Loading branch information
nulls authored Mar 12, 2024
1 parent 02e8d03 commit 7856434
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,19 @@ class FixAndWarnPluginTest {
FixAndWarnPluginConfig(
FixPluginConfig(fixExecutionCmd),
WarnPluginConfig(warnExecutionCmd,
Regex("(.+):(\\d+):(\\d+): (.+)"),
true, true, 1, 2, 3, 1, 1, 1, 2, 3, 4
actualWarningsPattern = Regex("(.+):(\\d+):(\\d+): (.+)"),
warningTextHasLine = true,
warningTextHasColumn = true,
fileNameCaptureGroup = 1,
lineCaptureGroup = null,
columnCaptureGroup = 2,
messageCaptureGroup = 3,
messageCaptureGroupMiddle = 1,
messageCaptureGroupEnd = 1,
fileNameCaptureGroupOut = 1,
lineCaptureGroupOut = 2,
columnCaptureGroupOut = 3,
messageCaptureGroupOut = 4,
)
),
GeneralConfig("", 1, ", ", listOf(""), "", "", expectedWarningsPattern = Regex("// ;warn:(\\d+):(\\d+): (.*)"), runConfigPattern = defaultExtraConfigPattern)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import com.saveourtool.save.plugin.warn.utils.collectWarningsFromSarif
import com.saveourtool.save.plugin.warn.utils.collectionMultilineWarnings
import com.saveourtool.save.plugin.warn.utils.collectionSingleWarnings
import com.saveourtool.save.plugin.warn.utils.extractWarning
import com.saveourtool.save.plugin.warn.utils.getLineNumber

import io.github.detekt.sarif4k.SarifSchema210
import okio.FileSystem
Expand Down Expand Up @@ -276,11 +275,17 @@ class WarnPlugin(
}
return when (warnPluginConfig.expectedWarningsFormat) {
ExpectedWarningsFormat.PLAIN -> {
val warningsFromPlain = collectWarningsFromPlain(expectedWarningsFileName, originalPaths, fs) { plainFile ->
plainFile.collectExpectedWarningsWithLineNumbers(
warnPluginConfig,
generalConfig
)
val warningsFromPlain = collectWarningsFromPlain(expectedWarningsFileName, originalPaths, fs) { line ->
with(warnPluginConfig) {
line.extractWarning(
generalConfig.expectedWarningsPattern!!,
fileNameCaptureGroup!!,
lineCaptureGroup,
columnCaptureGroup,
messageCaptureGroup!!,
benchmarkMode!!,
)
}
}
copyPaths.associate { copyPath ->
copyPath.name to warningsFromPlain.filter { it.fileName == copyPath.name }
Expand Down Expand Up @@ -363,7 +368,7 @@ class WarnPlugin(
it.extractWarning(
actualWarningsPattern!!,
fileNameCaptureGroupOut!!,
it.getLineNumber(actualWarningsPattern, lineCaptureGroupOut),
lineCaptureGroupOut,
columnCaptureGroupOut,
messageCaptureGroupOut!!,
benchmarkMode!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import kotlinx.serialization.UseSerializers
* @property actualWarningsPattern a regular expression by which warnings will be discovered in the process output
* @property warningTextHasLine whether line number is included in [actualWarningsPattern]
* @property warningTextHasColumn whether column number is included in [actualWarningsPattern]
* @property fileNameCaptureGroup an index of capture group in regular expressions, corresponding to file number. Indices start at 0 with 0
* corresponding to the whole string.
* @property lineCaptureGroup an index of capture group in regular expressions, corresponding to line number. Indices start at 0 with 0
* corresponding to the whole string.
* @property columnCaptureGroup an index of capture group in regular expressions, corresponding to column number. Indices start at 0 with 0
Expand Down Expand Up @@ -65,6 +67,7 @@ data class WarnPluginConfig(
val actualWarningsPattern: Regex? = null,
val warningTextHasLine: Boolean? = null,
val warningTextHasColumn: Boolean? = null,
val fileNameCaptureGroup: Long? = null,
val lineCaptureGroup: Long? = null,
val columnCaptureGroup: Long? = null,
val messageCaptureGroup: Long? = null,
Expand Down Expand Up @@ -115,6 +118,7 @@ data class WarnPluginConfig(
this.actualWarningsPattern ?: other.actualWarningsPattern,
this.warningTextHasLine ?: other.warningTextHasLine,
this.warningTextHasColumn ?: other.warningTextHasColumn,
this.fileNameCaptureGroup ?: other.fileNameCaptureGroup,
this.lineCaptureGroup ?: other.lineCaptureGroup,
this.columnCaptureGroup ?: other.columnCaptureGroup,
this.messageCaptureGroup ?: other.messageCaptureGroup,
Expand Down Expand Up @@ -188,6 +192,7 @@ data class WarnPluginConfig(
actualWarningsPattern ?: defaultOutputPattern,
newWarningTextHasLine,
newWarningTextHasColumn,
fileNameCaptureGroup,
newLineCaptureGroup,
newColumnCaptureGroup,
newMessageCaptureGroup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ internal fun String.extractWarning(warningRegex: Regex,
* @param columnGroupIdx index of capture group for column number
* @param messageGroupIdx index of capture group for waring text
* @param fileNameGroupIdx index of capture group for file name
* @param lineNum line number of warning
* @param lineNumGroupIdx index of capture group for line number of warning
* @param benchmarkMode whether to ignore the warning messages
* @return a [Warning] or null if [this] string doesn't match [warningRegex]
* @throws ResourceFormatException when parsing a file
Expand All @@ -106,13 +106,14 @@ internal fun String.extractWarning(warningRegex: Regex,
)
internal fun String.extractWarning(warningRegex: Regex,
fileNameGroupIdx: Long,
lineNum: Int?,
lineNumGroupIdx: Long?,
columnGroupIdx: Long?,
messageGroupIdx: Long,
benchmarkMode: Boolean,
): Warning? {
val groups = warningRegex.find(this)?.groups ?: return null
val fileName = getRegexGroupSafe(fileNameGroupIdx, groups, this, "file name")!!
val lineNum = getRegexGroupSafe(lineNumGroupIdx, groups, this, "line number")?.toInt()

return extractWarning(warningRegex, fileName, lineNum, columnGroupIdx, messageGroupIdx, benchmarkMode)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,31 @@ internal fun collectionSingleWarnings(
* @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]
* @param warningExtractor extractor of warning from [String]
* @return a list of warnings extracted from PLAIN file for all tests
* @throws PluginException
*/
internal fun collectWarningsFromPlain(
plainFileName: String,
originalPaths: List<Path>,
fs: FileSystem,
warningExtractor: (Path) -> List<Warning>,
warningExtractor: (String) -> 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()
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`."
)
return warningExtractor(plainFile)

return fs.read(plainFile) {
generateSequence { readUtf8Line() }
.map {
warningExtractor(it)
}
.filterNotNull()
.toList()
}
}

/**
Expand Down

0 comments on commit 7856434

Please sign in to comment.