Skip to content

Commit cf1d71d

Browse files
authored
Adapt FixPlugin for library for applying of SARIF fix patches (#464)
### What's done: * Adapt FixPlugin for library for applying of SARIF fix patches
1 parent e557722 commit cf1d71d

4 files changed

Lines changed: 62 additions & 21 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Classes that describe format of expected and actual fixes
3+
*/
4+
5+
@file:Suppress("FILE_NAME_MATCH_CLASS", "MatchingDeclarationName")
6+
7+
package com.saveourtool.save.core.config
8+
9+
/**
10+
* Possible formats of actual set of fixes, provided by user
11+
*/
12+
enum class ActualFixFormat {
13+
IN_PLACE,
14+
SARIF,
15+
;
16+
}

save-plugins/fix-and-warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fixandwarn/FixAndWarnPlugin.kt

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,7 @@ class FixAndWarnPlugin(
9292
}
9393

9494
// Fill back original data with warnings
95-
filesAndTheirWarningsMap.forEach { (filePath, warningsList) ->
96-
val fileData = fs.readLines(filePath) as MutableList
97-
// Append warnings into appropriate place
98-
warningsList.forEach { (line, warningMsg) ->
99-
fileData.add(line, warningMsg)
100-
}
101-
fs.write(filePath) {
102-
fileData.forEach {
103-
write((it + "\n").encodeToByteArray())
104-
}
105-
}
106-
}
95+
restoreWarningsIntoExpectedFiles(filesAndTheirWarningsMap)
10796

10897
// TODO: If we receive just one command for execution, and want to avoid extra executions
10998
// TODO: then warn plugin should look at the fix plugin output for actual warnings, and not execute command one more time.
@@ -136,8 +125,7 @@ class FixAndWarnPlugin(
136125
/**
137126
* Remove warnings from the given files, which satisfy pattern from <warn> plugin and save data about warnings, which were deleted
138127
*
139-
* @files files to be modified
140-
*
128+
* @param files files to be modified
141129
* @return map of files and theirs list of warnings
142130
*/
143131
private fun removeWarningsFromExpectedFiles(files: Sequence<Path>): MutableMap<Path, WarningsList> {
@@ -157,6 +145,26 @@ class FixAndWarnPlugin(
157145
return filesAndTheirWarningsMap
158146
}
159147

148+
/**
149+
* Remove warnings into the given files, which were deleted in aim to provide clear fix changes by FixPlugin
150+
*
151+
* @param filesAndTheirWarningsMap map of files with corresponding warnings list, which need to be restored
152+
*/
153+
private fun restoreWarningsIntoExpectedFiles(filesAndTheirWarningsMap: MutableMap<Path, WarningsList>) {
154+
filesAndTheirWarningsMap.forEach { (filePath, warningsList) ->
155+
val fileData = fs.readLines(filePath) as MutableList
156+
// Append warnings into appropriate place
157+
warningsList.forEach { (line, warningMsg) ->
158+
fileData.add(line, warningMsg)
159+
}
160+
fs.write(filePath) {
161+
fileData.forEach {
162+
write((it + "\n").encodeToByteArray())
163+
}
164+
}
165+
}
166+
}
167+
160168
private fun writeDataWithoutWarnings(
161169
file: Path,
162170
filesAndTheirWarningsMap: MutableMap<Path, WarningsList>,

save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPlugin.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.saveourtool.save.plugins.fix
22

3+
import com.saveourtool.save.core.config.ActualFixFormat
34
import com.saveourtool.save.core.config.TestConfig
45
import com.saveourtool.save.core.files.createFile
56
import com.saveourtool.save.core.files.createRelativePathToTheRoot
@@ -62,8 +63,8 @@ class FixPlugin(
6263
newTag = { _, start -> if (start) "<" else ">" },
6364
)
6465

65-
// fixme: consider refactoring under https://github.com/saveourtool/save/issues/156
66-
// fixme: should not be common for a class instance during https://github.com/saveourtool/save/issues/28
66+
// fixme: consider refactoring under https://github.com/saveourtool/save-cli/issues/156
67+
// fixme: should not be common for a class instance during https://github.com/saveourtool/save-cli/issues/28
6768
private var tmpDirectory: Path? = null
6869
private lateinit var extraFlagsExtractor: ExtraFlagsExtractor
6970

@@ -94,7 +95,7 @@ class FixPlugin(
9495

9596
val pathMap = chunk.map { it.test to it.expected }
9697
val pathCopyMap = pathMap.map { (test, expected) ->
97-
createTestFile(test, generalConfig, fixPluginConfig) to expected
98+
createCopyOfTestFile(test, generalConfig, fixPluginConfig) to expected
9899
}
99100
val testCopyNames =
100101
pathCopyMap.joinToString(separator = batchSeparator) { (testCopy, _) -> testCopy.toString() }
@@ -117,6 +118,13 @@ class FixPlugin(
117118
val stdout = executionResult.stdout
118119
val stderr = executionResult.stderr
119120

121+
// In this case fixes weren't performed by tool into the test files directly,
122+
// instead, there was created sarif file with list of fixes, which we will apply ourselves
123+
if (fixPluginConfig.actualFixFormat == ActualFixFormat.SARIF) {
124+
// TODO: Apply fixes from sarif file on `testCopyNames` here
125+
// applySarifFixesToFiles(fixPluginConfig.actualFixSarifFileName, testCopyNames)
126+
}
127+
120128
pathCopyMap.map { (testCopy, expected) ->
121129
val fixedLines = fs.readLines(testCopy)
122130
val expectedLines = fs.readLines(expected)
@@ -160,7 +168,7 @@ class FixPlugin(
160168
}
161169
}
162170

163-
private fun createTestFile(
171+
private fun createCopyOfTestFile(
164172
path: Path,
165173
generalConfig: GeneralConfig,
166174
fixPluginConfig: FixPluginConfig,

save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPluginConfig.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
package com.saveourtool.save.plugins.fix
44

5+
import com.saveourtool.save.core.config.ActualFixFormat
56
import com.saveourtool.save.core.config.TestConfigSections
67
import com.saveourtool.save.core.plugin.PluginConfig
78
import com.saveourtool.save.core.utils.RegexSerializer
@@ -22,13 +23,17 @@ import kotlinx.serialization.UseSerializers
2223
* @property resourceNameTestSuffix suffix name of the test file.
2324
* @property resourceNameExpectedSuffix suffix name of the expected file.
2425
* @property ignoreLines mutable list of patterns that later will be used to filter lines in test file
26+
* @property actualFixFormat format for type for fixes: they could be done in place or provided via Sarif file
27+
* @property actualFixSarifFileName name of sarif file with list of fixes, that were made by tool
2528
*/
2629
@Serializable
2730
data class FixPluginConfig(
2831
val execFlags: String? = null,
2932
val resourceNameTestSuffix: String? = null,
3033
val resourceNameExpectedSuffix: String? = null,
31-
val ignoreLines: MutableList<String>? = null
34+
val ignoreLines: MutableList<String>? = null,
35+
val actualFixFormat: ActualFixFormat? = null,
36+
val actualFixSarifFileName: String? = null,
3237
) : PluginConfig {
3338
override val type = TestConfigSections.FIX
3439

@@ -62,7 +67,9 @@ data class FixPluginConfig(
6267
this.resourceNameExpectedSuffix ?: other.resourceNameExpectedSuffix,
6368
other.ignoreLines?.let {
6469
this.ignoreLines?.let { other.ignoreLines.union(this.ignoreLines) } ?: other.ignoreLines
65-
}?.toMutableList() ?: this.ignoreLines
70+
}?.toMutableList() ?: this.ignoreLines,
71+
this.actualFixFormat ?: other.actualFixFormat,
72+
this.actualFixSarifFileName ?: other.actualFixSarifFileName
6673
).also {
6774
it.configLocation = this.configLocation
6875
}
@@ -73,7 +80,9 @@ data class FixPluginConfig(
7380
execFlags ?: "",
7481
resourceNameTest,
7582
resourceNameExpected,
76-
ignoreLines
83+
ignoreLines,
84+
actualFixFormat ?: ActualFixFormat.IN_PLACE,
85+
actualFixSarifFileName ?: "save-fixes.sarif",
7786
).also {
7887
it.configLocation = this.configLocation
7988
}

0 commit comments

Comments
 (0)