From 4756d4a34d36098e9ea7b15e0ed9eb0e93a61beb Mon Sep 17 00:00:00 2001 From: Kirill Gevorkyan <26010098+kgevorkyan@users.noreply.github.com> Date: Thu, 12 Jan 2023 15:29:44 +0300 Subject: [PATCH 1/6] WIP ### What's done: * WIP --- .../com/saveourtool/save/core/utils}/SarifFileUtils.kt | 4 ++-- .../save/plugin/warn/sarif/SarifWarningAdapter.kt | 1 + .../save/plugin/warn/utils/WarningsExtraction.kt | 6 +++--- .../save/plugin/warn/sarif/SarifWarningAdapterTest.kt | 1 + 4 files changed, 7 insertions(+), 5 deletions(-) rename {save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/sarif => save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils}/SarifFileUtils.kt (90%) diff --git a/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/sarif/SarifFileUtils.kt b/save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt similarity index 90% rename from save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/sarif/SarifFileUtils.kt rename to save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt index 84d4024dc..36b40daf3 100644 --- a/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/sarif/SarifFileUtils.kt +++ b/save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt @@ -2,7 +2,7 @@ * Utility methods to work with SARIF files. */ -package com.saveourtool.save.plugin.warn.sarif +package com.saveourtool.save.core.utils import com.saveourtool.save.core.files.parents @@ -47,6 +47,6 @@ fun List.adjustToCommonRoot(root: Path) = map { * @param path a path to start the search * @return one of parent directories */ -internal fun FileSystem.topmostTestDirectory(path: Path): Path = path.parents().last { parent -> +fun FileSystem.topmostTestDirectory(path: Path): Path = path.parents().last { parent -> list(parent).any { it.name == "save.toml" } } diff --git a/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/sarif/SarifWarningAdapter.kt b/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/sarif/SarifWarningAdapter.kt index 576555cbf..8b22b3e5a 100644 --- a/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/sarif/SarifWarningAdapter.kt +++ b/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/sarif/SarifWarningAdapter.kt @@ -4,6 +4,7 @@ package com.saveourtool.save.plugin.warn.sarif +import com.saveourtool.save.core.utils.dropFileProtocol import com.saveourtool.save.core.utils.isCurrentOsWindows import com.saveourtool.save.plugin.warn.utils.Warning diff --git a/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt b/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt index 8917d317e..c2d2f0631 100644 --- a/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt +++ b/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt @@ -9,10 +9,10 @@ import com.saveourtool.save.core.files.readFile import com.saveourtool.save.core.plugin.GeneralConfig import com.saveourtool.save.core.plugin.PluginException import com.saveourtool.save.plugin.warn.WarnPluginConfig -import com.saveourtool.save.plugin.warn.sarif.adjustToCommonRoot -import com.saveourtool.save.plugin.warn.sarif.findAncestorDirContainingFile +import com.saveourtool.save.core.utils.adjustToCommonRoot +import com.saveourtool.save.core.utils.findAncestorDirContainingFile import com.saveourtool.save.plugin.warn.sarif.toWarnings -import com.saveourtool.save.plugin.warn.sarif.topmostTestDirectory +import com.saveourtool.save.core.utils.topmostTestDirectory import io.github.detekt.sarif4k.SarifSchema210 import okio.FileSystem diff --git a/save-plugins/warn-plugin/src/commonNonJsTest/kotlin/com/saveourtool/save/plugin/warn/sarif/SarifWarningAdapterTest.kt b/save-plugins/warn-plugin/src/commonNonJsTest/kotlin/com/saveourtool/save/plugin/warn/sarif/SarifWarningAdapterTest.kt index 22b923d63..5da60063f 100644 --- a/save-plugins/warn-plugin/src/commonNonJsTest/kotlin/com/saveourtool/save/plugin/warn/sarif/SarifWarningAdapterTest.kt +++ b/save-plugins/warn-plugin/src/commonNonJsTest/kotlin/com/saveourtool/save/plugin/warn/sarif/SarifWarningAdapterTest.kt @@ -2,6 +2,7 @@ package com.saveourtool.save.plugin.warn.sarif import com.saveourtool.save.core.files.getWorkingDirectory import com.saveourtool.save.core.logging.logInfo +import com.saveourtool.save.core.utils.adjustToCommonRoot import com.saveourtool.save.plugin.warn.utils.Warning import io.github.detekt.sarif4k.ArtifactLocation From 8485083f8aa2721cfb34aa045ceb91e2504c9b64 Mon Sep 17 00:00:00 2001 From: Kirill Gevorkyan <26010098+kgevorkyan@users.noreply.github.com> Date: Thu, 12 Jan 2023 15:46:30 +0300 Subject: [PATCH 2/6] WIP ### What's done: * WIP --- examples/kotlin-diktat/fix/{sarif => }/save-fixes.sarif | 8 ++++---- .../saveourtool/save/core/test/utils/TestUtilsCommon.kt | 2 +- .../kotlin/com/saveourtool/save/plugins/fix/FixPlugin.kt | 6 +++++- .../com/saveourtool/save/plugins/fix/FixPluginConfig.kt | 4 ++-- 4 files changed, 12 insertions(+), 8 deletions(-) rename examples/kotlin-diktat/fix/{sarif => }/save-fixes.sarif (90%) diff --git a/examples/kotlin-diktat/fix/sarif/save-fixes.sarif b/examples/kotlin-diktat/fix/save-fixes.sarif similarity index 90% rename from examples/kotlin-diktat/fix/sarif/save-fixes.sarif rename to examples/kotlin-diktat/fix/save-fixes.sarif index 913dbdba4..7935b33f9 100644 --- a/examples/kotlin-diktat/fix/sarif/save-fixes.sarif +++ b/examples/kotlin-diktat/fix/save-fixes.sarif @@ -15,7 +15,7 @@ "artifactChanges": [ { "artifactLocation": { - "uri": "src/main/kotlin/com/saveourtool/save/Example1Test.kt" + "uri": "sarif/src/main/kotlin/com/saveourtool/save/Example1Test.kt" }, "replacements": [ { @@ -40,7 +40,7 @@ { "physicalLocation": { "artifactLocation": { - "uri": "src/main/kotlin/com/saveourtool/save/Example1Test.kt", + "uri": "sarif/src/main/kotlin/com/saveourtool/save/Example1Test.kt", "uriBaseId": "%SRCROOT%" }, "region": { @@ -65,7 +65,7 @@ "artifactChanges": [ { "artifactLocation": { - "uri": "src/main/kotlin/com/saveourtool/save/Example1Test.kt" + "uri": "sarif/src/main/kotlin/com/saveourtool/save/Example1Test.kt" }, "replacements": [ { @@ -90,7 +90,7 @@ { "physicalLocation": { "artifactLocation": { - "uri": "src/main/kotlin/com/saveourtool/save/Example1Test.kt", + "uri": "sarif/src/main/kotlin/com/saveourtool/save/Example1Test.kt", "uriBaseId": "%SRCROOT%" }, "region": { diff --git a/save-core/src/commonNonJsTest/kotlin/com/saveourtool/save/core/test/utils/TestUtilsCommon.kt b/save-core/src/commonNonJsTest/kotlin/com/saveourtool/save/core/test/utils/TestUtilsCommon.kt index 7d614fa87..aeddf1063 100644 --- a/save-core/src/commonNonJsTest/kotlin/com/saveourtool/save/core/test/utils/TestUtilsCommon.kt +++ b/save-core/src/commonNonJsTest/kotlin/com/saveourtool/save/core/test/utils/TestUtilsCommon.kt @@ -53,7 +53,7 @@ fun runTestsWithDiktat( ).let(overrideProperties) // logger is not set from save properties without a config reader, need to set it explicitly - logType.set(LogType.ALL) + logType.set(LogType.WARN) // In this test we need to merge with emulated empty save.properties file in aim to use default values, // since initially all fields are null val save = Save(saveProperties, fs) diff --git a/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPlugin.kt b/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPlugin.kt index 0d241b663..140f43264 100644 --- a/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPlugin.kt +++ b/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPlugin.kt @@ -26,6 +26,7 @@ import com.saveourtool.save.core.utils.ProcessTimeoutException import com.saveourtool.save.core.utils.singleIsInstance import com.saveourtool.sarifutils.cli.adapter.SarifFixAdapter +import com.saveourtool.save.core.utils.findAncestorDirContainingFile import io.github.petertrr.diffutils.diff import io.github.petertrr.diffutils.patch.ChangeDelta import io.github.petertrr.diffutils.patch.Delta @@ -186,10 +187,13 @@ class FixPlugin( testsPaths: List, testCopyToExpectedFilesMap: List, ): List { + val sarifFileName = fixPluginConfig.actualFixSarifFileName!! + val sarif = com.saveourtool.save.core.files.fs.findAncestorDirContainingFile(testsPaths.first(), sarifFileName)?.let { it / sarifFileName }!! + println("SARIF PATH $sarif ${fs.exists(sarif)}") // In this case fixes weren't performed by tool into the test files directly, // instead, there was created sarif file with list of fixes, which we will apply ourselves val fixedFiles = SarifFixAdapter( - sarifFile = fixPluginConfig.actualFixSarifFileName!!.toPath(), + sarifFile = sarif!!, targetFiles = testsPaths ).process() diff --git a/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPluginConfig.kt b/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPluginConfig.kt index 3628943fa..ab040efe4 100644 --- a/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPluginConfig.kt +++ b/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPluginConfig.kt @@ -82,7 +82,7 @@ data class FixPluginConfig( resourceNameExpectedSuffix = resourceNameExpected, ignoreLines = ignoreLines, actualFixFormat = actualFixFormat ?: ActualFixFormat.IN_PLACE, - actualFixSarifFileName = calculateActualFixSarifFilePath(), + actualFixSarifFileName = (actualFixSarifFileName ?: "save-fixes.sarif"), ).also { it.configLocation = this.configLocation } @@ -90,7 +90,7 @@ data class FixPluginConfig( // we require from sarif file to be located at the same level as corresponding save.toml private fun calculateActualFixSarifFilePath(): String? = if (actualFixFormat == ActualFixFormat.SARIF) { ( - configLocation.parent!! / + (actualFixSarifFileName ?: "save-fixes.sarif").toPath() ).toString() } else { From 30a5c7d6cab55a11de059406e820640d25af1d54 Mon Sep 17 00:00:00 2001 From: Kirill Gevorkyan <26010098+kgevorkyan@users.noreply.github.com> Date: Thu, 12 Jan 2023 16:28:50 +0300 Subject: [PATCH 3/6] WIP ### What's done: * WIP --- .../saveourtool/save/core/files/FileUtils.kt | 13 +++++++ .../save/core/utils/SarifFileUtils.kt | 36 ++++++++++++------- .../saveourtool/save/plugins/fix/FixPlugin.kt | 12 ++++--- .../save/plugins/fix/FixPluginConfig.kt | 10 ------ .../plugin/warn/utils/WarningsExtraction.kt | 11 +++--- 5 files changed, 48 insertions(+), 34 deletions(-) diff --git a/save-common/src/commonMain/kotlin/com/saveourtool/save/core/files/FileUtils.kt b/save-common/src/commonMain/kotlin/com/saveourtool/save/core/files/FileUtils.kt index d8f335848..9042cb18a 100644 --- a/save-common/src/commonMain/kotlin/com/saveourtool/save/core/files/FileUtils.kt +++ b/save-common/src/commonMain/kotlin/com/saveourtool/save/core/files/FileUtils.kt @@ -245,3 +245,16 @@ private fun createRelativePathFromThisToTheRoot(currentPath: Path, rootPath: Pat } return relativePath + currentPath.name } + +/** + * Find a file in any of parent directories and return this directory + * + * @param path path for which ancestors should be checked + * @param fileName a name of the file that will be searched for + * @return a path to one of parent directories or null if no directory contains [fileName] + */ +fun FileSystem.findAncestorDirContainingFile(path: Path, fileName: String): Path? = path.parents().firstOrNull { parent -> + metadata(parent).isDirectory && list(parent).any { + it.name == fileName + } +} diff --git a/save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt b/save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt index 36b40daf3..7db3bf685 100644 --- a/save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt +++ b/save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt @@ -4,7 +4,10 @@ package com.saveourtool.save.core.utils +import com.saveourtool.save.core.files.findAncestorDirContainingFile +import com.saveourtool.save.core.files.fs import com.saveourtool.save.core.files.parents +import com.saveourtool.save.core.plugin.PluginException import okio.FileSystem import okio.Path @@ -18,19 +21,6 @@ fun String.dropFileProtocol() = substringAfter("file://") if (it[0] == '/' && it[2] == ':') it.drop(1) else it } -/** - * Find a file in any of parent directories and return this directory - * - * @param path path for which ancestors should be checked - * @param fileName a name of the file that will be searched for - * @return a path to one of parent directories or null if no directory contains [fileName] - */ -fun FileSystem.findAncestorDirContainingFile(path: Path, fileName: String): Path? = path.parents().firstOrNull { parent -> - metadata(parent).isDirectory && list(parent).any { - it.name == fileName - } -} - /** * Make all paths in [this] collection relative to [root] * @@ -50,3 +40,23 @@ fun List.adjustToCommonRoot(root: Path) = map { fun FileSystem.topmostTestDirectory(path: Path): Path = path.parents().last { parent -> list(parent).any { it.name == "save.toml" } } + +/** + * Calculate the path to sarif file; we expect, that it located at the same level, as top save.toml config + * from current hierarchy tree + * + * @param sarifFileName sarif file name + * @param anchorTestFilePath anchor file for calculating corresponding sarif file; + * since .sarif file expected to be the one for all test files, it could be any of test file + * @return path to sarif + */ +fun calculatePathToSarifFile(sarifFileName: String, anchorTestFilePath: Path): Path { + return fs.findAncestorDirContainingFile( + anchorTestFilePath, sarifFileName + )?.let { + it / sarifFileName + } ?: throw PluginException( + "Could not find SARIF file with expected warnings/fixes for file $anchorTestFilePath. " + + "Please check if correct `FarningsFormat`/`FixFormat` is set (should be SARIF) and if the file is present and called `$sarifFileName`." + ) +} \ No newline at end of file diff --git a/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPlugin.kt b/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPlugin.kt index 140f43264..b3c1ec4c5 100644 --- a/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPlugin.kt +++ b/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPlugin.kt @@ -26,7 +26,7 @@ import com.saveourtool.save.core.utils.ProcessTimeoutException import com.saveourtool.save.core.utils.singleIsInstance import com.saveourtool.sarifutils.cli.adapter.SarifFixAdapter -import com.saveourtool.save.core.utils.findAncestorDirContainingFile +import com.saveourtool.save.core.utils.calculatePathToSarifFile import io.github.petertrr.diffutils.diff import io.github.petertrr.diffutils.patch.ChangeDelta import io.github.petertrr.diffutils.patch.Delta @@ -187,13 +187,15 @@ class FixPlugin( testsPaths: List, testCopyToExpectedFilesMap: List, ): List { - val sarifFileName = fixPluginConfig.actualFixSarifFileName!! - val sarif = com.saveourtool.save.core.files.fs.findAncestorDirContainingFile(testsPaths.first(), sarifFileName)?.let { it / sarifFileName }!! - println("SARIF PATH $sarif ${fs.exists(sarif)}") + val sarif = calculatePathToSarifFile( + sarifFileName = fixPluginConfig.actualFixSarifFileName!!, + // Since we have one .sarif file for all tests, just take the first of them as anchor for calculation of paths + anchorTestFilePath = testsPaths.first() + ) // In this case fixes weren't performed by tool into the test files directly, // instead, there was created sarif file with list of fixes, which we will apply ourselves val fixedFiles = SarifFixAdapter( - sarifFile = sarif!!, + sarifFile = sarif, targetFiles = testsPaths ).process() diff --git a/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPluginConfig.kt b/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPluginConfig.kt index ab040efe4..bb2fb3837 100644 --- a/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPluginConfig.kt +++ b/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPluginConfig.kt @@ -86,14 +86,4 @@ data class FixPluginConfig( ).also { it.configLocation = this.configLocation } - - // we require from sarif file to be located at the same level as corresponding save.toml - private fun calculateActualFixSarifFilePath(): String? = if (actualFixFormat == ActualFixFormat.SARIF) { - ( - - (actualFixSarifFileName ?: "save-fixes.sarif").toPath() - ).toString() - } else { - null - } } diff --git a/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt b/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt index c2d2f0631..2dd16af72 100644 --- a/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt +++ b/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt @@ -10,7 +10,7 @@ import com.saveourtool.save.core.plugin.GeneralConfig import com.saveourtool.save.core.plugin.PluginException import com.saveourtool.save.plugin.warn.WarnPluginConfig import com.saveourtool.save.core.utils.adjustToCommonRoot -import com.saveourtool.save.core.utils.findAncestorDirContainingFile +import com.saveourtool.save.core.utils.calculatePathToSarifFile import com.saveourtool.save.plugin.warn.sarif.toWarnings import com.saveourtool.save.core.utils.topmostTestDirectory @@ -114,11 +114,10 @@ internal fun collectWarningsFromSarif( // Since we have one .sarif file for all tests, just take the first of them as anchor for calculation of paths val anchorTestFilePath = originalPaths.first() - val sarif = fs.findAncestorDirContainingFile(anchorTestFilePath, sarifFileName)?.let { it / sarifFileName } - ?: throw PluginException( - "Could not find SARIF file with expected warnings for file $anchorTestFilePath. " + - "Please check if correct `expectedWarningsFormat` is set and if the file is present and called `$sarifFileName`." - ) + val sarif = calculatePathToSarifFile( + sarifFileName = sarifFileName, + anchorTestFilePath = anchorTestFilePath + ) val topmostTestDirectory = fs.topmostTestDirectory(anchorTestFilePath) return Json.decodeFromString( fs.readFile(sarif) From 56068eed9783c0a3292ec9d40b460bf946c234ab Mon Sep 17 00:00:00 2001 From: Kirill Gevorkyan <26010098+kgevorkyan@users.noreply.github.com> Date: Thu, 12 Jan 2023 16:37:42 +0300 Subject: [PATCH 4/6] WIP ### What's done: * WIP --- examples/kotlin-diktat/fix/{ => sarif}/save-fixes.sarif | 8 ++++---- .../com/saveourtool/save/core/utils/SarifFileUtils.kt | 6 ++++-- 2 files changed, 8 insertions(+), 6 deletions(-) rename examples/kotlin-diktat/fix/{ => sarif}/save-fixes.sarif (90%) diff --git a/examples/kotlin-diktat/fix/save-fixes.sarif b/examples/kotlin-diktat/fix/sarif/save-fixes.sarif similarity index 90% rename from examples/kotlin-diktat/fix/save-fixes.sarif rename to examples/kotlin-diktat/fix/sarif/save-fixes.sarif index 7935b33f9..913dbdba4 100644 --- a/examples/kotlin-diktat/fix/save-fixes.sarif +++ b/examples/kotlin-diktat/fix/sarif/save-fixes.sarif @@ -15,7 +15,7 @@ "artifactChanges": [ { "artifactLocation": { - "uri": "sarif/src/main/kotlin/com/saveourtool/save/Example1Test.kt" + "uri": "src/main/kotlin/com/saveourtool/save/Example1Test.kt" }, "replacements": [ { @@ -40,7 +40,7 @@ { "physicalLocation": { "artifactLocation": { - "uri": "sarif/src/main/kotlin/com/saveourtool/save/Example1Test.kt", + "uri": "src/main/kotlin/com/saveourtool/save/Example1Test.kt", "uriBaseId": "%SRCROOT%" }, "region": { @@ -65,7 +65,7 @@ "artifactChanges": [ { "artifactLocation": { - "uri": "sarif/src/main/kotlin/com/saveourtool/save/Example1Test.kt" + "uri": "src/main/kotlin/com/saveourtool/save/Example1Test.kt" }, "replacements": [ { @@ -90,7 +90,7 @@ { "physicalLocation": { "artifactLocation": { - "uri": "sarif/src/main/kotlin/com/saveourtool/save/Example1Test.kt", + "uri": "src/main/kotlin/com/saveourtool/save/Example1Test.kt", "uriBaseId": "%SRCROOT%" }, "region": { diff --git a/save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt b/save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt index 7db3bf685..e016c82da 100644 --- a/save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt +++ b/save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt @@ -42,8 +42,8 @@ fun FileSystem.topmostTestDirectory(path: Path): Path = path.parents().last { pa } /** - * Calculate the path to sarif file; we expect, that it located at the same level, as top save.toml config - * from current hierarchy tree + * Calculate the path to sarif file; we expect, that it single for the all tests and located in one of parent directories + * for evaluated test files * * @param sarifFileName sarif file name * @param anchorTestFilePath anchor file for calculating corresponding sarif file; @@ -51,9 +51,11 @@ fun FileSystem.topmostTestDirectory(path: Path): Path = path.parents().last { pa * @return path to sarif */ fun calculatePathToSarifFile(sarifFileName: String, anchorTestFilePath: Path): Path { + println("anchorTestFilePath $anchorTestFilePath") return fs.findAncestorDirContainingFile( anchorTestFilePath, sarifFileName )?.let { + println("${it / sarifFileName}") it / sarifFileName } ?: throw PluginException( "Could not find SARIF file with expected warnings/fixes for file $anchorTestFilePath. " + From 5d9a0d74a1b6337475cbada59cff29e6d2ae4817 Mon Sep 17 00:00:00 2001 From: Kirill Gevorkyan <26010098+kgevorkyan@users.noreply.github.com> Date: Fri, 13 Jan 2023 12:33:24 +0300 Subject: [PATCH 5/6] WIP ### What's done: * WIP --- .../com/saveourtool/save/core/test/utils/TestUtilsCommon.kt | 2 +- save-plugins/fix-plugin/README.md | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/save-core/src/commonNonJsTest/kotlin/com/saveourtool/save/core/test/utils/TestUtilsCommon.kt b/save-core/src/commonNonJsTest/kotlin/com/saveourtool/save/core/test/utils/TestUtilsCommon.kt index aeddf1063..7d614fa87 100644 --- a/save-core/src/commonNonJsTest/kotlin/com/saveourtool/save/core/test/utils/TestUtilsCommon.kt +++ b/save-core/src/commonNonJsTest/kotlin/com/saveourtool/save/core/test/utils/TestUtilsCommon.kt @@ -53,7 +53,7 @@ fun runTestsWithDiktat( ).let(overrideProperties) // logger is not set from save properties without a config reader, need to set it explicitly - logType.set(LogType.WARN) + logType.set(LogType.ALL) // In this test we need to merge with emulated empty save.properties file in aim to use default values, // since initially all fields are null val save = Save(saveProperties, fs) diff --git a/save-plugins/fix-plugin/README.md b/save-plugins/fix-plugin/README.md index 9d2fb31f3..e8f73be56 100644 --- a/save-plugins/fix-plugin/README.md +++ b/save-plugins/fix-plugin/README.md @@ -1,5 +1,11 @@ ## Save fix plugin Plugin that runs provided executable on the initial file with a test source code and compares its output with an expected result. + +Fix plugin supports two types of execution: `IN_PLACE` and `SARIF`, which could be specified by `actualFixFormat` flag. +In case of `IN_PLACE` mode, `save` will apply fixes, obtained by static analysis tool by executing it with provided configuration, +while in `SARIF` mode, it will expect the `.sarif` file, with the list of fixes, which could be provided by `actualFixSarifFileName` flag. +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). + Please note, that it is important for test resources to have specific postfixes. By the default test file it should be `Test` , for the file with expected result - it should be `Expected`. From d155eaeaa7ce926dafc5c76aae221b665fa2daf2 Mon Sep 17 00:00:00 2001 From: Kirill Gevorkyan <26010098+kgevorkyan@users.noreply.github.com> Date: Fri, 13 Jan 2023 12:38:30 +0300 Subject: [PATCH 6/6] WIP ### What's done: * WIP --- .../saveourtool/save/core/files/FileUtils.kt | 26 +++++++++---------- .../save/core/utils/SarifFileUtils.kt | 21 +++++++-------- .../saveourtool/save/plugins/fix/FixPlugin.kt | 2 +- .../plugin/warn/utils/WarningsExtraction.kt | 4 +-- 4 files changed, 25 insertions(+), 28 deletions(-) diff --git a/save-common/src/commonMain/kotlin/com/saveourtool/save/core/files/FileUtils.kt b/save-common/src/commonMain/kotlin/com/saveourtool/save/core/files/FileUtils.kt index 9042cb18a..6f11cc420 100644 --- a/save-common/src/commonMain/kotlin/com/saveourtool/save/core/files/FileUtils.kt +++ b/save-common/src/commonMain/kotlin/com/saveourtool/save/core/files/FileUtils.kt @@ -217,6 +217,19 @@ fun Path.parentsWithSelf() = listOf(this) + this.parents().toList() */ expect fun FileSystem.myDeleteRecursively(path: Path) +/** + * Find a file in any of parent directories and return this directory + * + * @param path path for which ancestors should be checked + * @param fileName a name of the file that will be searched for + * @return a path to one of parent directories or null if no directory contains [fileName] + */ +fun FileSystem.findAncestorDirContainingFile(path: Path, fileName: String): Path? = path.parents().firstOrNull { parent -> + metadata(parent).isDirectory && list(parent).any { + it.name == fileName + } +} + /** * @return current working directory */ @@ -245,16 +258,3 @@ private fun createRelativePathFromThisToTheRoot(currentPath: Path, rootPath: Pat } return relativePath + currentPath.name } - -/** - * Find a file in any of parent directories and return this directory - * - * @param path path for which ancestors should be checked - * @param fileName a name of the file that will be searched for - * @return a path to one of parent directories or null if no directory contains [fileName] - */ -fun FileSystem.findAncestorDirContainingFile(path: Path, fileName: String): Path? = path.parents().firstOrNull { parent -> - metadata(parent).isDirectory && list(parent).any { - it.name == fileName - } -} diff --git a/save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt b/save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt index e016c82da..cfc9e9f84 100644 --- a/save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt +++ b/save-common/src/commonMain/kotlin/com/saveourtool/save/core/utils/SarifFileUtils.kt @@ -49,16 +49,13 @@ fun FileSystem.topmostTestDirectory(path: Path): Path = path.parents().last { pa * @param anchorTestFilePath anchor file for calculating corresponding sarif file; * since .sarif file expected to be the one for all test files, it could be any of test file * @return path to sarif + * @throws PluginException in case of absence of sarif file */ -fun calculatePathToSarifFile(sarifFileName: String, anchorTestFilePath: Path): Path { - println("anchorTestFilePath $anchorTestFilePath") - return fs.findAncestorDirContainingFile( - anchorTestFilePath, sarifFileName - )?.let { - println("${it / sarifFileName}") - it / sarifFileName - } ?: throw PluginException( - "Could not find SARIF file with expected warnings/fixes for file $anchorTestFilePath. " + - "Please check if correct `FarningsFormat`/`FixFormat` is set (should be SARIF) and if the file is present and called `$sarifFileName`." - ) -} \ No newline at end of file +fun calculatePathToSarifFile(sarifFileName: String, anchorTestFilePath: Path): Path = fs.findAncestorDirContainingFile( + anchorTestFilePath, sarifFileName +)?.let { + it / sarifFileName +} ?: throw PluginException( + "Could not find SARIF file with expected warnings/fixes for file $anchorTestFilePath. " + + "Please check if correct `FarningsFormat`/`FixFormat` is set (should be SARIF) and if the file is present and called `$sarifFileName`." +) diff --git a/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPlugin.kt b/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPlugin.kt index b3c1ec4c5..ac561a0cb 100644 --- a/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPlugin.kt +++ b/save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPlugin.kt @@ -23,10 +23,10 @@ import com.saveourtool.save.core.utils.ExecutionResult import com.saveourtool.save.core.utils.PathSerializer import com.saveourtool.save.core.utils.ProcessExecutionException import com.saveourtool.save.core.utils.ProcessTimeoutException +import com.saveourtool.save.core.utils.calculatePathToSarifFile import com.saveourtool.save.core.utils.singleIsInstance import com.saveourtool.sarifutils.cli.adapter.SarifFixAdapter -import com.saveourtool.save.core.utils.calculatePathToSarifFile import io.github.petertrr.diffutils.diff import io.github.petertrr.diffutils.patch.ChangeDelta import io.github.petertrr.diffutils.patch.Delta diff --git a/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt b/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt index 2dd16af72..3130f3a76 100644 --- a/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt +++ b/save-plugins/warn-plugin/src/commonMain/kotlin/com/saveourtool/save/plugin/warn/utils/WarningsExtraction.kt @@ -8,11 +8,11 @@ package com.saveourtool.save.plugin.warn.utils import com.saveourtool.save.core.files.readFile import com.saveourtool.save.core.plugin.GeneralConfig import com.saveourtool.save.core.plugin.PluginException -import com.saveourtool.save.plugin.warn.WarnPluginConfig import com.saveourtool.save.core.utils.adjustToCommonRoot import com.saveourtool.save.core.utils.calculatePathToSarifFile -import com.saveourtool.save.plugin.warn.sarif.toWarnings import com.saveourtool.save.core.utils.topmostTestDirectory +import com.saveourtool.save.plugin.warn.WarnPluginConfig +import com.saveourtool.save.plugin.warn.sarif.toWarnings import io.github.detekt.sarif4k.SarifSchema210 import okio.FileSystem