From fe8f8eb87cef57452b87248b5075273dc05d653c Mon Sep 17 00:00:00 2001 From: Vfrolov Date: Tue, 12 Jul 2022 14:57:05 +0300 Subject: [PATCH] Migrate diktat smoke tests to SAVE-cli mechanism ### What's done: * migrated diktat smoke tests to SAVE-cli mechanism Closes #1383 --- .../ruleset/smoke/DiktatSaveSmokeTest.kt | 41 +++++++++++- .../diktat/ruleset/smoke/DiktatSmokeTest.kt | 13 +++- .../ruleset/smoke/DiktatSmokeTestBase.kt | 32 ++++----- .../org/cqfn/diktat/util/FixTestBase.kt | 66 ++++--------------- 4 files changed, 78 insertions(+), 74 deletions(-) diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt index 6feadce82d..04798a5212 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt @@ -5,6 +5,7 @@ import org.apache.http.client.methods.CloseableHttpResponse import org.apache.http.client.methods.HttpGet import org.apache.http.impl.client.HttpClients import org.junit.jupiter.api.AfterAll +import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.BeforeAll import java.io.File import java.io.FileOutputStream @@ -15,12 +16,48 @@ import kotlin.io.path.name import kotlin.io.path.pathString class DiktatSaveSmokeTest : DiktatSmokeTestBase() { - override fun fixAndCompareBase( + override fun fixAndCompare( config: String, test: String, expected: String ) { - saveSmokeTest(config, test, expected) + saveSmokeTest(config, test) + } + + /** + * @param testPath path to file with code that will be transformed by formatter, relative to [resourceFilePath] + * @param configFilePath path of diktat-analysis file + */ + @Suppress("TOO_LONG_FUNCTION") + protected fun saveSmokeTest( + configFilePath: String, + testPath: String + ) { + val processBuilder = createProcessBuilderWithCmd(testPath) + + val file = File("src/test/resources/test/smoke/tmpSave.txt") + val configFile = File("src/test/resources/test/smoke/diktat-analysis.yml") + val configFileFrom = File(configFilePath) + + configFile.createNewFile() + file.createNewFile() + + FileUtils.copyFile(configFileFrom, configFile) + + processBuilder.redirectErrorStream(true) + processBuilder.redirectOutput(ProcessBuilder.Redirect.appendTo(file)) + + val process = processBuilder.start() + process.waitFor() + + val output = file.readLines() + val saveOutput = output.joinToString("\n") + + file.delete() + + Assertions.assertTrue( + saveOutput.contains("SUCCESS") + ) } companion object { diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt index d5ab3493ff..82dcf767fa 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt @@ -27,7 +27,7 @@ import java.io.File * may change after some changes to text or other rules. */ class DiktatSmokeTest : DiktatSmokeTestBase() { - override fun fixAndCompareBase( + override fun fixAndCompare( config: String, test: String, expected: String @@ -105,4 +105,15 @@ class DiktatSmokeTest : DiktatSmokeTestBase() { LintError(18, 40, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false) ) } + + /** + * @param expectedPath path to file with expected result, relative to [resourceFilePath] + * @param testPath path to file with code that will be transformed by formatter, relative to [resourceFilePath] + */ + protected fun fixAndCompareSmokeTest(expectedPath: String, testPath: String) { + Assertions.assertTrue( + testComparatorUnit + .compareFilesFromResources(expectedPath, testPath, true) + ) + } } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt index 1a01783a2d..cee94847b0 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt @@ -78,19 +78,19 @@ abstract class DiktatSmokeTestBase : FixTestBase("test/smoke/src/main/kotlin", fun `regression - should not fail if package is not set`() { overrideRulesConfig(listOf(Warnings.PACKAGE_NAME_MISSING, Warnings.PACKAGE_NAME_INCORRECT_PATH, Warnings.PACKAGE_NAME_INCORRECT_PREFIX)) - fixAndCompareBase(configFilePath, "DefaultPackageExpected.kt", "DefaultPackageTest.kt") + fixAndCompare(configFilePath, "DefaultPackageExpected.kt", "DefaultPackageTest.kt") } @Test @Tag("DiktatRuleSetProvider") fun `smoke test #8 - anonymous function`() { - fixAndCompareBase(configFilePath, "Example8Expected.kt", "Example8Test.kt") + fixAndCompare(configFilePath, "Example8Expected.kt", "Example8Test.kt") } @Test @Tag("DiktatRuleSetProvider") fun `smoke test #7`() { - fixAndCompareBase(configFilePath, "Example7Expected.kt", "Example7Test.kt") + fixAndCompare(configFilePath, "Example7Expected.kt", "Example7Test.kt") } @Test @@ -105,7 +105,7 @@ abstract class DiktatSmokeTestBase : FixTestBase("test/smoke/src/main/kotlin", ) ) ) - fixAndCompareBase(configFilePath, "Example6Expected.kt", "Example6Test.kt") + fixAndCompare(configFilePath, "Example6Expected.kt", "Example6Test.kt") } @Test @@ -129,7 +129,7 @@ abstract class DiktatSmokeTestBase : FixTestBase("test/smoke/src/main/kotlin", ) ) ) - fixAndCompareBase(configFilePath, "Example5Expected.kt", "Example5Test.kt") + fixAndCompare(configFilePath, "Example5Expected.kt", "Example5Test.kt") } @Test @@ -144,19 +144,19 @@ abstract class DiktatSmokeTestBase : FixTestBase("test/smoke/src/main/kotlin", ) ) ) - fixAndCompareBase(configFilePath, "Example4Expected.kt", "Example4Test.kt") + fixAndCompare(configFilePath, "Example4Expected.kt", "Example4Test.kt") } @Test @Tag("DiktatRuleSetProvider") fun `smoke test #3`() { - fixAndCompareBase(configFilePath, "Example3Expected.kt", "Example3Test.kt") + fixAndCompare(configFilePath, "Example3Expected.kt", "Example3Test.kt") } @Test @Tag("DiktatRuleSetProvider") fun `regression - shouldn't throw exception in cases similar to #371`() { - fixAndCompareBase(configFilePath, "Bug1Expected.kt", "Bug1Test.kt") + fixAndCompare(configFilePath, "Bug1Expected.kt", "Bug1Test.kt") } @Test @@ -171,7 +171,7 @@ abstract class DiktatSmokeTestBase : FixTestBase("test/smoke/src/main/kotlin", ) ) ) - fixAndCompareBase(configFilePath, "Example2Expected.kt", "Example2Test.kt") + fixAndCompare(configFilePath, "Example2Expected.kt", "Example2Test.kt") } @Test @@ -186,40 +186,40 @@ abstract class DiktatSmokeTestBase : FixTestBase("test/smoke/src/main/kotlin", ) ) ) - fixAndCompareBase(configFilePath, "Example1Expected.kt", "Example1Test.kt") + fixAndCompare(configFilePath, "Example1Expected.kt", "Example1Test.kt") } @Test @Tag("DiktatRuleSetProvider") fun `smoke test with kts files #2`() { - fixAndCompareBase(configFilePath, "script/SimpleRunInScriptExpected.kts", "script/SimpleRunInScriptTest.kts") + fixAndCompare(configFilePath, "script/SimpleRunInScriptExpected.kts", "script/SimpleRunInScriptTest.kts") } @Test @Tag("DiktatRuleSetProvider") fun `smoke test with kts files with package name`() { - fixAndCompareBase(configFilePath, "script/PackageInScriptExpected.kts", "script/PackageInScriptTest.kts") + fixAndCompare(configFilePath, "script/PackageInScriptExpected.kts", "script/PackageInScriptTest.kts") } @Test @Tag("DiktatRuleSetProvider") fun `regression - should correctly handle tags with empty lines`() { - fixAndCompareBase(configFilePath, "KdocFormattingMultilineTagsExpected.kt", "KdocFormattingMultilineTagsTest.kt") + fixAndCompare(configFilePath, "KdocFormattingMultilineTagsExpected.kt", "KdocFormattingMultilineTagsTest.kt") } @Test @Tag("DiktatRuleSetProvider") fun `regression - FP of local variables rule`() { - fixAndCompareBase(configFilePath, "LocalVariableWithOffsetExpected.kt", "LocalVariableWithOffsetTest.kt") + fixAndCompare(configFilePath, "LocalVariableWithOffsetExpected.kt", "LocalVariableWithOffsetTest.kt") } @Test @Tag("DiktatRuleSetProvider") fun `fix can cause long line`() { - fixAndCompareBase(configFilePath, "ManyLineTransformInLongLineExpected.kt", "ManyLineTransformInLongLineTest.kt") + fixAndCompare(configFilePath, "ManyLineTransformInLongLineExpected.kt", "ManyLineTransformInLongLineTest.kt") } - abstract fun fixAndCompareBase( + abstract fun fixAndCompare( config: String, test: String, expected: String diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBase.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBase.kt index 773ac43cc6..076fa94659 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBase.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBase.kt @@ -5,10 +5,8 @@ import org.cqfn.diktat.test.framework.processing.FileComparisonResult import org.cqfn.diktat.test.framework.processing.TestComparatorUnit import com.pinterest.ktlint.core.Rule import com.pinterest.ktlint.core.RuleSetProvider -import org.apache.commons.io.FileUtils.copyFile import org.intellij.lang.annotations.Language import org.junit.jupiter.api.Assertions -import java.io.File import java.nio.file.Path import kotlin.io.path.bufferedWriter import kotlin.io.path.div @@ -22,7 +20,10 @@ open class FixTestBase( private val cb: LintErrorCallback = defaultCallback, private val rulesConfigList: List? = null, ) { - private val testComparatorUnit = TestComparatorUnit(resourceFilePath) { text, fileName -> + /** + * testComparatorUnit + */ + protected val testComparatorUnit = TestComparatorUnit(resourceFilePath) { text, fileName -> format(ruleSetProviderRef, text, fileName, rulesConfigList, cb = cb) } @@ -48,17 +49,6 @@ open class FixTestBase( ) } - /** - * @param expectedPath path to file with expected result, relative to [resourceFilePath] - * @param testPath path to file with code that will be transformed by formatter, relative to [resourceFilePath] - */ - protected fun fixAndCompareSmokeTest(expectedPath: String, testPath: String) { - Assertions.assertTrue( - testComparatorUnit - .compareFilesFromResources(expectedPath, testPath, true) - ) - } - private fun getSaveForCurrentOs() = when { System.getProperty("os.name").startsWith("Linux", ignoreCase = true) -> "save-$SAVE_VERSION-linuxX64.kexe" System.getProperty("os.name").startsWith("Mac", ignoreCase = true) -> "save-$SAVE_VERSION-macosX64.kexe" @@ -66,57 +56,23 @@ open class FixTestBase( else -> "" } - private fun createProcessBuilderWithCmd(expectedPath: String, testPath: String): ProcessBuilder { + /** + * @param testPath path to file with code that will be transformed by formatter, relative to [resourceFilePath] + * @return ProcessBuilder + */ + protected fun createProcessBuilderWithCmd(testPath: String): ProcessBuilder { val filesDir = "src/test/resources/test/smoke" val savePath = "$filesDir/${getSaveForCurrentOs()}" val systemName = System.getProperty("os.name") val result = when { systemName.startsWith("Linux", ignoreCase = true) || systemName.startsWith("Mac", ignoreCase = true) -> - ProcessBuilder("sh", "-c", "pwd ; chmod 777 $savePath ; ./$savePath src/test/resources/test/smoke/src/main/kotlin $testPath --log all") - else -> ProcessBuilder(savePath, "src/test/resources/test/smoke/src/main/kotlin", expectedPath, testPath) + ProcessBuilder("sh", "-c", "chmod 777 $savePath ; ./$savePath src/test/resources/test/smoke/src/main/kotlin $testPath --log all") + else -> ProcessBuilder(savePath, "src/test/resources/test/smoke/src/main/kotlin", testPath) } return result } - /** - * @param expectedPath path to file with expected result, relative to [resourceFilePath] - * @param testPath path to file with code that will be transformed by formatter, relative to [resourceFilePath] - * @param configFilePath path of diktat-analysis file - */ - @Suppress("TOO_LONG_FUNCTION") - protected fun saveSmokeTest( - configFilePath: String, - expectedPath: String, - testPath: String - ) { - val processBuilder = createProcessBuilderWithCmd(expectedPath, testPath) - - val file = File("src/test/resources/test/smoke/tmpSave.txt") - val configFile = File("src/test/resources/test/smoke/diktat-analysis.yml") - val configFileFrom = File(configFilePath) - - configFile.createNewFile() - file.createNewFile() - - copyFile(configFileFrom, configFile) - - processBuilder.redirectErrorStream(true) - processBuilder.redirectOutput(ProcessBuilder.Redirect.appendTo(file)) - - val process = processBuilder.start() - process.waitFor() - - val output = file.readLines() - val saveOutput = output.joinToString("\n") - - file.delete() - - Assertions.assertTrue( - saveOutput.contains("SUCCESS") - ) - } - /** * @param expectedPath path to file with expected result, relative to [resourceFilePath] * @param testPath path to file with code that will be transformed by formatter, relative to [resourceFilePath]