Skip to content

Commit e5ced44

Browse files
authored
Update README and move sarif utils into common module (#477)
### What's done: * Update README and move sarif utils into common module
1 parent 69b23ee commit e5ced44

File tree

9 files changed

+97
-72
lines changed

9 files changed

+97
-72
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,19 @@ fun Path.parentsWithSelf() = listOf(this) + this.parents().toList()
217217
*/
218218
expect fun FileSystem.myDeleteRecursively(path: Path)
219219

220+
/**
221+
* Find a file in any of parent directories and return this directory
222+
*
223+
* @param path path for which ancestors should be checked
224+
* @param fileName a name of the file that will be searched for
225+
* @return a path to one of parent directories or null if no directory contains [fileName]
226+
*/
227+
fun FileSystem.findAncestorDirContainingFile(path: Path, fileName: String): Path? = path.parents().firstOrNull { parent ->
228+
metadata(parent).isDirectory && list(parent).any {
229+
it.name == fileName
230+
}
231+
}
232+
220233
/**
221234
* @return current working directory
222235
*/
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Utility methods to work with SARIF files.
3+
*/
4+
5+
package com.saveourtool.save.core.utils
6+
7+
import com.saveourtool.save.core.files.findAncestorDirContainingFile
8+
import com.saveourtool.save.core.files.fs
9+
import com.saveourtool.save.core.files.parents
10+
import com.saveourtool.save.core.plugin.PluginException
11+
12+
import okio.FileSystem
13+
import okio.Path
14+
15+
/**
16+
* @return string with trimmed `file://` or `file:///`
17+
*/
18+
fun String.dropFileProtocol() = substringAfter("file://")
19+
.let {
20+
// It is a valid format for Windows paths to look like `file:///C:/stuff`
21+
if (it[0] == '/' && it[2] == ':') it.drop(1) else it
22+
}
23+
24+
/**
25+
* Make all paths in [this] collection relative to [root]
26+
*
27+
* @param root a common root for files in [this]
28+
* @return a list of relative paths
29+
*/
30+
fun List<Path>.adjustToCommonRoot(root: Path) = map {
31+
it.relativeTo(root).normalized()
32+
}
33+
34+
/**
35+
* Find the last parent directory containing save.toml.
36+
*
37+
* @param path a path to start the search
38+
* @return one of parent directories
39+
*/
40+
fun FileSystem.topmostTestDirectory(path: Path): Path = path.parents().last { parent ->
41+
list(parent).any { it.name == "save.toml" }
42+
}
43+
44+
/**
45+
* Calculate the path to sarif file; we expect, that it single for the all tests and located in one of parent directories
46+
* for evaluated test files
47+
*
48+
* @param sarifFileName sarif file name
49+
* @param anchorTestFilePath anchor file for calculating corresponding sarif file;
50+
* since .sarif file expected to be the one for all test files, it could be any of test file
51+
* @return path to sarif
52+
* @throws PluginException in case of absence of sarif file
53+
*/
54+
fun calculatePathToSarifFile(sarifFileName: String, anchorTestFilePath: Path): Path = fs.findAncestorDirContainingFile(
55+
anchorTestFilePath, sarifFileName
56+
)?.let {
57+
it / sarifFileName
58+
} ?: throw PluginException(
59+
"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`."
61+
)

save-plugins/fix-plugin/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## Save fix plugin
22
Plugin that runs provided executable on the initial file with a test source code and compares its output with an expected result.
3+
4+
Fix plugin supports two types of execution: `IN_PLACE` and `SARIF`, which could be specified by `actualFixFormat` flag.
5+
In case of `IN_PLACE` mode, `save` will apply fixes, obtained by static analysis tool by executing it with provided configuration,
6+
while in `SARIF` mode, it will expect the `.sarif` file, with the list of fixes, which could be provided by `actualFixSarifFileName` flag.
7+
Plugin will extract all fixes from sarif and apply them to the test files. More information about sarif fix sections could be found [here](https://docs.oasis-open.org/sarif/sarif/v2.1.0/os/sarif-v2.1.0-os.html#_Toc34317881).
8+
39
Please note, that it is important for test resources to have specific postfixes. By the default test file it should be `Test`
410
, for the file with expected result - it should be `Expected`.
511

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.saveourtool.save.core.utils.ExecutionResult
2323
import com.saveourtool.save.core.utils.PathSerializer
2424
import com.saveourtool.save.core.utils.ProcessExecutionException
2525
import com.saveourtool.save.core.utils.ProcessTimeoutException
26+
import com.saveourtool.save.core.utils.calculatePathToSarifFile
2627
import com.saveourtool.save.core.utils.singleIsInstance
2728

2829
import com.saveourtool.sarifutils.cli.adapter.SarifFixAdapter
@@ -186,10 +187,15 @@ class FixPlugin(
186187
testsPaths: List<Path>,
187188
testCopyToExpectedFilesMap: List<PathPair>,
188189
): List<PathPair> {
190+
val sarif = calculatePathToSarifFile(
191+
sarifFileName = fixPluginConfig.actualFixSarifFileName!!,
192+
// Since we have one .sarif file for all tests, just take the first of them as anchor for calculation of paths
193+
anchorTestFilePath = testsPaths.first()
194+
)
189195
// In this case fixes weren't performed by tool into the test files directly,
190196
// instead, there was created sarif file with list of fixes, which we will apply ourselves
191197
val fixedFiles = SarifFixAdapter(
192-
sarifFile = fixPluginConfig.actualFixSarifFileName!!.toPath(),
198+
sarifFile = sarif,
193199
targetFiles = testsPaths
194200
).process()
195201

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,8 @@ data class FixPluginConfig(
8282
resourceNameExpectedSuffix = resourceNameExpected,
8383
ignoreLines = ignoreLines,
8484
actualFixFormat = actualFixFormat ?: ActualFixFormat.IN_PLACE,
85-
actualFixSarifFileName = calculateActualFixSarifFilePath(),
85+
actualFixSarifFileName = (actualFixSarifFileName ?: "save-fixes.sarif"),
8686
).also {
8787
it.configLocation = this.configLocation
8888
}
89-
90-
// we require from sarif file to be located at the same level as corresponding save.toml
91-
private fun calculateActualFixSarifFilePath(): String? = if (actualFixFormat == ActualFixFormat.SARIF) {
92-
(
93-
configLocation.parent!! /
94-
(actualFixSarifFileName ?: "save-fixes.sarif").toPath()
95-
).toString()
96-
} else {
97-
null
98-
}
9989
}

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

Lines changed: 0 additions & 52 deletions
This file was deleted.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package com.saveourtool.save.plugin.warn.sarif
66

7+
import com.saveourtool.save.core.utils.dropFileProtocol
78
import com.saveourtool.save.core.utils.isCurrentOsWindows
89
import com.saveourtool.save.plugin.warn.utils.Warning
910

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ package com.saveourtool.save.plugin.warn.utils
88
import com.saveourtool.save.core.files.readFile
99
import com.saveourtool.save.core.plugin.GeneralConfig
1010
import com.saveourtool.save.core.plugin.PluginException
11+
import com.saveourtool.save.core.utils.adjustToCommonRoot
12+
import com.saveourtool.save.core.utils.calculatePathToSarifFile
13+
import com.saveourtool.save.core.utils.topmostTestDirectory
1114
import com.saveourtool.save.plugin.warn.WarnPluginConfig
12-
import com.saveourtool.save.plugin.warn.sarif.adjustToCommonRoot
13-
import com.saveourtool.save.plugin.warn.sarif.findAncestorDirContainingFile
1415
import com.saveourtool.save.plugin.warn.sarif.toWarnings
15-
import com.saveourtool.save.plugin.warn.sarif.topmostTestDirectory
1616

1717
import io.github.detekt.sarif4k.SarifSchema210
1818
import okio.FileSystem
@@ -114,11 +114,10 @@ internal fun collectWarningsFromSarif(
114114

115115
// Since we have one .sarif file for all tests, just take the first of them as anchor for calculation of paths
116116
val anchorTestFilePath = originalPaths.first()
117-
val sarif = fs.findAncestorDirContainingFile(anchorTestFilePath, sarifFileName)?.let { it / sarifFileName }
118-
?: throw PluginException(
119-
"Could not find SARIF file with expected warnings for file $anchorTestFilePath. " +
120-
"Please check if correct `expectedWarningsFormat` is set and if the file is present and called `$sarifFileName`."
121-
)
117+
val sarif = calculatePathToSarifFile(
118+
sarifFileName = sarifFileName,
119+
anchorTestFilePath = anchorTestFilePath
120+
)
122121
val topmostTestDirectory = fs.topmostTestDirectory(anchorTestFilePath)
123122
return Json.decodeFromString<SarifSchema210>(
124123
fs.readFile(sarif)

save-plugins/warn-plugin/src/commonNonJsTest/kotlin/com/saveourtool/save/plugin/warn/sarif/SarifWarningAdapterTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.saveourtool.save.plugin.warn.sarif
22

33
import com.saveourtool.save.core.files.getWorkingDirectory
44
import com.saveourtool.save.core.logging.logInfo
5+
import com.saveourtool.save.core.utils.adjustToCommonRoot
56
import com.saveourtool.save.plugin.warn.utils.Warning
67

78
import io.github.detekt.sarif4k.ArtifactLocation

0 commit comments

Comments
 (0)