From 5479d9b29b8f81f0baa0b32e60adfad189824558 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 26 Oct 2022 13:00:29 +0300 Subject: [PATCH 01/39] Smoke tests in diktat ### What's done: * moved smoke tests to diktat-ruleset --- diktat-rules/pom.xml | 14 + .../diktat/ruleset/smoke/DiktatSmokeTest.kt | 120 ------- .../org/cqfn/diktat/util/FixTestBase.kt | 7 +- diktat-ruleset/pom.xml | 70 +++++ .../ruleset/smoke/DiktatSaveSmokeTest.kt | 4 +- .../diktat/ruleset/smoke/DiktatSmokeTest.kt | 19 ++ .../ruleset/smoke/DiktatSmokeTestBase.kt | 85 ++++- .../kotlin/org/cqfn/diktat/util/TestUtils.kt | 293 ++++++++++++++++++ .../resources/test/smoke/build.gradle_.kts | 0 .../src/test/resources/test/smoke/save.toml | 0 .../org/cqfn/diktat/scripts/ScriptExpected.kt | 0 .../org/cqfn/diktat/scripts/ScriptTest.kt | 0 .../smoke/src/main/kotlin/Bug1Expected.kt | 0 .../test/smoke/src/main/kotlin/Bug1Test.kt | 0 .../src/main/kotlin/DefaultPackageExpected.kt | 0 .../src/main/kotlin/DefaultPackageTest.kt | 0 .../src/main/kotlin/Example1-2Expected.kt | 0 .../smoke/src/main/kotlin/Example1Expected.kt | 0 .../smoke/src/main/kotlin/Example1Test.kt | 0 .../smoke/src/main/kotlin/Example2Expected.kt | 0 .../smoke/src/main/kotlin/Example2Test.kt | 0 .../smoke/src/main/kotlin/Example3Expected.kt | 0 .../smoke/src/main/kotlin/Example3Test.kt | 0 .../smoke/src/main/kotlin/Example4Expected.kt | 0 .../smoke/src/main/kotlin/Example4Test.kt | 0 .../smoke/src/main/kotlin/Example5Expected.kt | 0 .../smoke/src/main/kotlin/Example5Test.kt | 0 .../smoke/src/main/kotlin/Example6Expected.kt | 0 .../smoke/src/main/kotlin/Example6Test.kt | 0 .../smoke/src/main/kotlin/Example7Expected.kt | 0 .../smoke/src/main/kotlin/Example7Test.kt | 0 .../smoke/src/main/kotlin/Example8Expected.kt | 0 .../smoke/src/main/kotlin/Example8Test.kt | 0 .../KdocFormattingMultilineTagsExpected.kt | 0 .../kotlin/KdocFormattingMultilineTagsTest.kt | 0 .../kotlin/LocalVariableWithOffsetExpected.kt | 0 .../kotlin/LocalVariableWithOffsetTest.kt | 0 .../ManyLineTransformInLongLineExpected.kt | 0 .../kotlin/ManyLineTransformInLongLineTest.kt | 0 .../kotlin/kotlin-library-expected.gradle.kts | 0 .../src/main/kotlin/kotlin-library.gradle.kts | 0 .../test/smoke/src/main/kotlin/save.toml | 0 .../kotlin/script/PackageInScriptExpected.kts | 0 .../kotlin/script/PackageInScriptTest.kts | 0 .../script/SimpleRunInScriptExpected.kts | 0 .../kotlin/script/SimpleRunInScriptTest.kts | 0 pom.xml | 6 + 47 files changed, 489 insertions(+), 129 deletions(-) delete mode 100644 diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt rename {diktat-rules => diktat-ruleset}/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt (98%) create mode 100644 diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt rename {diktat-rules => diktat-ruleset}/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt (77%) create mode 100644 diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/build.gradle_.kts (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/save.toml (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/jsMain/kotlin/org/cqfn/diktat/scripts/ScriptExpected.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/jsMain/kotlin/org/cqfn/diktat/scripts/ScriptTest.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Bug1Expected.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Bug1Test.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/DefaultPackageExpected.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/DefaultPackageTest.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Example1-2Expected.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Example1Expected.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Example1Test.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Example2Expected.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Example2Test.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Example3Expected.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Example3Test.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Example4Expected.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Example4Test.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Example5Expected.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Example5Test.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Example6Expected.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Example6Test.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Example7Expected.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Example7Test.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Example8Expected.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/Example8Test.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/KdocFormattingMultilineTagsExpected.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/KdocFormattingMultilineTagsTest.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/LocalVariableWithOffsetExpected.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/LocalVariableWithOffsetTest.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineExpected.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineTest.kt (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/kotlin-library-expected.gradle.kts (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/kotlin-library.gradle.kts (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/save.toml (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/script/PackageInScriptExpected.kts (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/script/PackageInScriptTest.kts (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/script/SimpleRunInScriptExpected.kts (100%) rename {diktat-rules => diktat-ruleset}/src/test/resources/test/smoke/src/main/kotlin/script/SimpleRunInScriptTest.kts (100%) diff --git a/diktat-rules/pom.xml b/diktat-rules/pom.xml index b843e027b0..c0762a8a59 100644 --- a/diktat-rules/pom.xml +++ b/diktat-rules/pom.xml @@ -304,6 +304,20 @@ + + + + org.apache.maven.plugins + maven-jar-plugin + + + test-jar + + test-jar + + + + 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 deleted file mode 100644 index 6276dc6556..0000000000 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt +++ /dev/null @@ -1,120 +0,0 @@ -package org.cqfn.diktat.ruleset.smoke - -import org.cqfn.diktat.common.config.rules.DIKTAT_COMMON -import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID -import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_NO_EMPTY_TAGS -import org.cqfn.diktat.ruleset.constants.Warnings.MISSING_KDOC_CLASS_ELEMENTS -import org.cqfn.diktat.ruleset.constants.Warnings.MISSING_KDOC_ON_FUNCTION -import org.cqfn.diktat.ruleset.constants.Warnings.MISSING_KDOC_TOP_LEVEL -import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_INDENTATION -import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider -import org.cqfn.diktat.ruleset.rules.chapter2.comments.CommentsRule -import org.cqfn.diktat.ruleset.rules.chapter2.kdoc.KdocComments -import org.cqfn.diktat.ruleset.rules.chapter2.kdoc.KdocFormatting -import org.cqfn.diktat.ruleset.rules.chapter2.kdoc.KdocMethods -import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.NEWLINE_AT_END -import org.cqfn.diktat.util.assertEquals - -import com.pinterest.ktlint.core.LintError -import org.junit.jupiter.api.Assertions -import org.junit.jupiter.api.Tag -import org.junit.jupiter.api.Test - -import java.io.File - -/** - * Test for [DiktatRuleSetProvider] in autocorrect mode as a whole. All rules are applied to a file. - * Note: ktlint uses initial text from a file to calculate line and column from offset. Because of that line/col of unfixed errors - * may change after some changes to text or other rules. - */ -class DiktatSmokeTest : DiktatSmokeTestBase() { - override val isLintErrors = true - override fun fixAndCompare( - config: String, - expected: String, - test: String, - ) { - fixAndCompareSmokeTest(expected, test) - } - - @Test - @Tag("DiktatRuleSetProvider") - fun `smoke test with multiplatform project layout`() { - fixAndCompareSmokeTest("../../jsMain/kotlin/org/cqfn/diktat/scripts/ScriptExpected.kt", - "../../jsMain/kotlin/org/cqfn/diktat/scripts/ScriptTest.kt") - } - - @Test - @Tag("DiktatRuleSetProvider") - fun `smoke test with kts files`() { - overrideRulesConfig( - emptyList(), - mapOf( - WRONG_INDENTATION.name to mapOf( - NEWLINE_AT_END to "false", - ) - ) - ) // so that trailing newline isn't checked, because it's incorrectly read in tests and we are comparing file with itself - // file name is `gradle_` so that IDE doesn't suggest to import gradle project - val tmpTestFile = javaClass.classLoader - .getResource("$resourceFilePath/../../../build.gradle_.kts")!! - .toURI() - .let { - val tmpTestFile = File(it).parentFile.resolve("build.gradle.kts") - File(it).copyTo(tmpTestFile) - tmpTestFile - } - val tmpFilePath = "../../../build.gradle.kts" - fixAndCompare(tmpFilePath, tmpFilePath) - Assertions.assertTrue(unfixedLintErrors.isEmpty()) - tmpTestFile.delete() - } - - @Test - @Tag("DiktatRuleSetProvider") - fun `smoke test with gradle script plugin`() { - fixAndCompareSmokeTest("kotlin-library-expected.gradle.kts", "kotlin-library.gradle.kts") - Assertions.assertEquals( - LintError(2, 1, "$DIKTAT_RULE_SET_ID:${CommentsRule.NAME_ID}", "[COMMENTED_OUT_CODE] you should not comment out code, " + - "use VCS to save it in history and delete this block: import org.jetbrains.kotlin.gradle.dsl.jvm", false), - unfixedLintErrors.single() - ) - } - - @Test - @Tag("DiktatRuleSetProvider") - fun `disable chapters`() { - overrideRulesConfig( - emptyList(), - mapOf( - DIKTAT_COMMON to mapOf( - "domainName" to "org.cqfn.diktat", - "disabledChapters" to "Naming,3,4,5,Classes" - ) - ) - ) - fixAndCompareSmokeTest("Example1-2Expected.kt", "Example1Test.kt") - unfixedLintErrors.assertEquals( - LintError(1, 1, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false), - LintError(3, 1, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_TOP_LEVEL.warnText()} example", false), - LintError(3, 16, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} isValid", false), - LintError(6, 5, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} foo", false), - LintError(6, 5, "$DIKTAT_RULE_SET_ID:${KdocMethods.NAME_ID}", "${MISSING_KDOC_ON_FUNCTION.warnText()} foo", false), - LintError(8, 5, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} foo", false), - LintError(10, 4, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false), - LintError(13, 9, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false), - 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] - */ - private fun fixAndCompareSmokeTest(expectedPath: String, testPath: String) { - Assertions.assertTrue( - testComparatorUnit - .compareFilesFromResources(expectedPath, testPath, true) - ) - } -} 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 c26808cc7d..f3696f66c7 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 @@ -44,12 +44,15 @@ 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] * @param overrideRulesConfigList optional override to [rulesConfigList] + * @param trimLastEmptyLine whether the last (empty) line should be + * discarded when reading the content of [testFileStr]. * @see fixAndCompareContent */ protected fun fixAndCompare( expectedPath: String, testPath: String, - overrideRulesConfigList: List = emptyList() + overrideRulesConfigList: List = emptyList(), + trimLastEmptyLine: Boolean = false, ) { val testComparatorUnit = if (overrideRulesConfigList.isNotEmpty()) { TestComparatorUnit(resourceFilePath) { text, fileName -> @@ -61,7 +64,7 @@ open class FixTestBase( Assertions.assertTrue( testComparatorUnit - .compareFilesFromResources(expectedPath, testPath) + .compareFilesFromResources(expectedPath, testPath, trimLastEmptyLine) ) } diff --git a/diktat-ruleset/pom.xml b/diktat-ruleset/pom.xml index a7275178e1..dcddd8e8e9 100644 --- a/diktat-ruleset/pom.xml +++ b/diktat-ruleset/pom.xml @@ -9,6 +9,7 @@ org.cqfn.diktat diktat-parent 1.2.4-SNAPSHOT + ../pom.xml @@ -36,6 +37,75 @@ + + + org.cqfn.diktat + diktat-rules + ${project.version} + test-jar + test + + + org.cqfn.diktat + diktat-test-framework + ${project.version} + test + + + org.jetbrains.kotlin + kotlin-stdlib-common + test + + + org.jetbrains.kotlin + kotlin-stdlib-jdk7 + test + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + test + + + org.jetbrains.kotlin + kotlin-stdlib + test + + + org.jetbrains.kotlin + kotlin-compiler-embeddable + test + + + org.apache.logging.log4j + log4j-core + test + + + org.apache.logging.log4j + log4j-slf4j-impl + test + + + org.junit.jupiter + junit-jupiter + test + + + org.junit.platform + junit-platform-suite-engine + test + + + org.assertj + assertj-core + test + + + org.mockito + mockito-all + test + diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt similarity index 98% rename from diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt rename to diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt index 401e9b33dc..8d8e13c883 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt @@ -1,7 +1,6 @@ package org.cqfn.diktat.ruleset.smoke import org.cqfn.diktat.common.utils.loggerWithKtlintConfig -import org.cqfn.diktat.util.SAVE_VERSION import org.cqfn.diktat.util.deleteIfExistsSilently import org.cqfn.diktat.util.isSameJavaHomeAs import org.cqfn.diktat.util.prependPath @@ -100,8 +99,9 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { @Suppress("EMPTY_BLOCK_STRUCTURE_ERROR") private val logger = KotlinLogging.loggerWithKtlintConfig { } private const val BUILD_DIRECTORY = "target" - private const val FAT_JAR_GLOB = "diktat-rules-*-fat-jar-for-smoke-tests.jar" + private const val FAT_JAR_GLOB = "diktat-*.jar" private const val KTLINT_VERSION = "0.46.1" + private const val SAVE_VERSION: String = "0.3.2" private val baseDirectory = Path("src/test/resources/test/smoke").absolute() private fun getSaveForCurrentOs(): String { diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt new file mode 100644 index 0000000000..068ebc6579 --- /dev/null +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt @@ -0,0 +1,19 @@ +package org.cqfn.diktat.ruleset.smoke + +import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider + +/** + * Test for [DiktatRuleSetProvider] in autocorrect mode as a whole. All rules are applied to a file. + * Note: ktlint uses initial text from a file to calculate line and column from offset. Because of that line/col of unfixed errors + * may change after some changes to text or other rules. + */ +class DiktatSmokeTest : DiktatSmokeTestBase() { + override val isLintErrors = true + override fun fixAndCompare( + config: String, + expected: String, + test: String, + ) { + fixAndCompare(expected, test, emptyList(), true) + } +} diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt similarity index 77% rename from diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt rename to diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt index 6d53ea9590..9da31e9d6f 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt @@ -29,6 +29,7 @@ import org.cqfn.diktat.ruleset.rules.chapter2.kdoc.KdocFormatting import org.cqfn.diktat.ruleset.rules.chapter2.kdoc.KdocMethods import org.cqfn.diktat.ruleset.rules.chapter3.EmptyBlock import org.cqfn.diktat.ruleset.rules.chapter6.classes.InlineClassesRule +import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_AFTER_OPERATORS import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_BEFORE_DOT import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_FOR_EXPRESSION_BODIES @@ -44,6 +45,7 @@ import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Tag import org.junit.jupiter.api.Test import org.junit.jupiter.api.Timeout +import java.io.File import java.time.LocalDate import java.util.concurrent.TimeUnit.SECONDS @@ -55,7 +57,11 @@ typealias RuleToConfig = Map> /** * Base class for smoke test classes */ -abstract class DiktatSmokeTestBase : FixTestBase("test/smoke/src/main/kotlin", +abstract class DiktatSmokeTestBase( + private val unfixedLintErrors: MutableList = mutableListOf(), + // by default using same yml config as for diktat code style check, but allow to override + private var configFilePath: String = DEFAULT_CONFIG_PATH, +) : FixTestBase("test/smoke/src/main/kotlin", { DiktatRuleSetProvider(configFilePath) }, { lintError, _ -> unfixedLintErrors.add(lintError) }, ) { @@ -303,6 +309,79 @@ abstract class DiktatSmokeTestBase : FixTestBase("test/smoke/src/main/kotlin", fixAndCompare(configFilePath, "ManyLineTransformInLongLineExpected.kt", "ManyLineTransformInLongLineTest.kt") } + @Test + @Tag("DiktatRuleSetProvider") + fun `smoke test with multiplatform project layout`() { + fixAndCompare( + configFilePath, + "../../jsMain/kotlin/org/cqfn/diktat/scripts/ScriptExpected.kt", + "../../jsMain/kotlin/org/cqfn/diktat/scripts/ScriptTest.kt" + ) + } + + @Test + @Tag("DiktatRuleSetProvider") + fun `smoke test with kts files`() { + overrideRulesConfig( + emptyList(), + mapOf( + WRONG_INDENTATION.name to mapOf( + IndentationConfig.NEWLINE_AT_END to "false", + ) + ) + ) // so that trailing newline isn't checked, because it's incorrectly read in tests and we are comparing file with itself + // file name is `gradle_` so that IDE doesn't suggest to import gradle project + val tmpTestFile = javaClass.classLoader + .getResource("$resourceFilePath/../../../build.gradle_.kts")!! + .toURI() + .let { + val tmpTestFile = File(it).parentFile.resolve("build.gradle.kts") + File(it).copyTo(tmpTestFile) + tmpTestFile + } + val tmpFilePath = "../../../build.gradle.kts" + fixAndCompare(configFilePath, tmpFilePath, tmpFilePath) + Assertions.assertTrue(unfixedLintErrors.isEmpty()) + tmpTestFile.delete() + } + + @Test + @Tag("DiktatRuleSetProvider") + fun `smoke test with gradle script plugin`() { + fixAndCompare(configFilePath, "kotlin-library-expected.gradle.kts", "kotlin-library.gradle.kts") + Assertions.assertEquals( + LintError(2, 1, "$DIKTAT_RULE_SET_ID:${CommentsRule.NAME_ID}", "[COMMENTED_OUT_CODE] you should not comment out code, " + + "use VCS to save it in history and delete this block: import org.jetbrains.kotlin.gradle.dsl.jvm", false), + unfixedLintErrors.single() + ) + } + + @Test + @Tag("DiktatRuleSetProvider") + fun `disable chapters`() { + overrideRulesConfig( + emptyList(), + mapOf( + DIKTAT_COMMON to mapOf( + "domainName" to "org.cqfn.diktat", + "disabledChapters" to "Naming,3,4,5,Classes" + ) + ) + ) + fixAndCompare(configFilePath, "Example1-2Expected.kt", "Example1Test.kt") + unfixedLintErrors.assertEquals( + LintError(1, 1, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false), + LintError(3, 1, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_TOP_LEVEL.warnText()} example", false), + LintError(3, 16, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} isValid", false), + LintError(6, 5, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} foo", false), + LintError(6, 5, "$DIKTAT_RULE_SET_ID:${KdocMethods.NAME_ID}", "${MISSING_KDOC_ON_FUNCTION.warnText()} foo", false), + LintError(8, 5, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} foo", false), + LintError(10, 4, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false), + LintError(13, 9, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false), + LintError(18, 40, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false) + ) + } + abstract fun fixAndCompare( config: String, expected: String, @@ -312,9 +391,5 @@ abstract class DiktatSmokeTestBase : FixTestBase("test/smoke/src/main/kotlin", companion object { const val DEFAULT_CONFIG_PATH = "../diktat-analysis.yml" private const val TEST_TIMEOUT_SECONDS = 20L - val unfixedLintErrors: MutableList = mutableListOf() - - // by default using same yml config as for diktat code style check, but allow to override - var configFilePath = DEFAULT_CONFIG_PATH } } diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt new file mode 100644 index 0000000000..28d2522f58 --- /dev/null +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt @@ -0,0 +1,293 @@ +/** + * Utility classes and methods for tests + */ + +package org.cqfn.diktat.util + +import org.cqfn.diktat.common.config.rules.RulesConfig +import org.cqfn.diktat.common.utils.loggerWithKtlintConfig +import org.cqfn.diktat.ruleset.constants.EmitType + +import com.pinterest.ktlint.core.KtLint +import com.pinterest.ktlint.core.LintError +import com.pinterest.ktlint.core.Rule +import com.pinterest.ktlint.core.RuleSet +import com.pinterest.ktlint.core.RuleSetProvider +import com.pinterest.ktlint.core.api.FeatureInAlphaState +import mu.KotlinLogging +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.fail +import org.assertj.core.api.SoftAssertions.assertSoftly +import org.intellij.lang.annotations.Language +import org.jetbrains.kotlin.com.intellij.lang.ASTNode + +import java.io.File +import java.nio.file.NoSuchFileException +import java.nio.file.Path +import java.util.concurrent.atomic.AtomicInteger +import java.util.function.Consumer +import kotlin.io.path.absolute +import kotlin.io.path.deleteIfExists +import kotlin.io.path.isDirectory +import kotlin.io.path.isSameFileAs + +internal const val TEST_FILE_NAME = "TestFileName.kt" + +@Suppress("EMPTY_BLOCK_STRUCTURE_ERROR") +private val log = KotlinLogging.loggerWithKtlintConfig {} + +@Suppress("TYPE_ALIAS") +internal val defaultCallback: (lintError: LintError, corrected: Boolean) -> Unit = { lintError, _ -> + log.warn("Received linting error: $lintError") +} + +typealias LintErrorCallback = (LintError, Boolean) -> Unit + +/** + * Compare [LintError]s from [this] with [expectedLintErrors] + * + * @param expectedLintErrors expected [LintError]s + */ +internal fun List.assertEquals(vararg expectedLintErrors: LintError) { + if (size == expectedLintErrors.size) { + assertThat(this) + .allSatisfy(Consumer { actual -> + val expected = expectedLintErrors[this@assertEquals.indexOf(actual)] + assertSoftly { sa -> + sa + .assertThat(actual.line) + .`as`("Line") + .isEqualTo(expected.line) + sa + .assertThat(actual.col) + .`as`("Column") + .isEqualTo(expected.col) + sa + .assertThat(actual.ruleId) + .`as`("Rule id") + .isEqualTo(expected.ruleId) + sa + .assertThat(actual.detail) + .`as`("Detailed message") + .isEqualTo(expected.detail) + sa + .assertThat(actual.canBeAutoCorrected) + .`as`("Can be autocorrected") + .isEqualTo(expected.canBeAutoCorrected) + } + }) + } else { + assertThat(this).containsExactly(*expectedLintErrors) + } +} + +/** + * Deletes the file if it exists, retrying as necessary if the file is + * blocked by another process (on Windows). + * + * @receiver the file or empty directory. + * @see Path.deleteIfExists + */ +@Suppress( + "EMPTY_BLOCK_STRUCTURE_ERROR", + "MAGIC_NUMBER", +) +internal fun Path.deleteIfExistsSilently() { + val attempts = 10 + + val deleted = retry(attempts, delayMillis = 100L, lazyDefault = { false }) { + deleteIfExists() + + /* + * Ignore the return code of `deleteIfExists()` (will be `false` + * if the file doesn't exist). + */ + true + } + + if (!deleted) { + log.warn { + "File \"${absolute()}\" not deleted after $attempts attempt(s)." + } + } +} + +/** + * @receiver the 1st operand. + * @param other the 2nd operand. + * @return `true` if, and only if, the two paths locate the same `JAVA_HOME`. + */ +internal fun Path.isSameJavaHomeAs(other: Path): Boolean = + isDirectory() && + (isSameFileAsSafe(other) || + resolve("jre").isSameFileAsSafe(other) || + other.resolve("jre").isSameFileAsSafe(this)) + +/** + * The same as [Path.isSameFileAs], but doesn't throw any [NoSuchFileException] + * if either of the operands doesn't exist. + * + * @receiver the 1st operand. + * @param other the 2nd operand. + * @return `true` if, and only if, the two paths locate the same file. + * @see Path.isSameFileAs + */ +internal fun Path.isSameFileAsSafe(other: Path): Boolean = + try { + isSameFileAs(other) + } catch (_: NoSuchFileException) { + false + } + +/** + * Prepends the `PATH` of this process builder with [pathEntry]. + * + * @param pathEntry the entry to be prepended to the `PATH`. + */ +internal fun ProcessBuilder.prependPath(pathEntry: Path) { + require(pathEntry.isDirectory()) { + "$pathEntry is not a directory" + } + + val environment = environment() + + val defaultPathKey = "PATH" + val defaultWindowsPathKey = "Path" + + val pathKey = when { + /*- + * Keys of the Windows environment are case-insensitive ("PATH" == "Path"). + * Keys of the Java interface to the environment are not ("PATH" != "Path"). + * This is an attempt to work around the inconsistency. + */ + System.getProperty("os.name").startsWith("Windows") -> environment.keys.firstOrNull { key -> + key.equals(defaultPathKey, ignoreCase = true) + } ?: defaultWindowsPathKey + + else -> defaultPathKey + } + + val pathSeparator = File.pathSeparatorChar + val oldPath = environment[pathKey] + + val newPath = when { + oldPath.isNullOrEmpty() -> pathEntry.toString() + else -> "$pathEntry$pathSeparator$oldPath" + } + + environment[pathKey] = newPath +} + +/** + * Casts a nullable value to a non-`null` one, similarly to the `!!` + * operator. + * + * @param lazyFailureMessage the message to evaluate in case of a failure. + * @return a non-`null` value. + */ +internal fun T?.assertNotNull(lazyFailureMessage: () -> String = { "Expecting actual not to be null" }): T = + this ?: fail(lazyFailureMessage()) + +/** + * @param ruleSetProviderRef + * @param text + * @param fileName + * @param rulesConfigList + * @param cb callback to be called on unhandled [LintError]s + * @return formatted code + */ +@Suppress("LAMBDA_IS_NOT_LAST_PARAMETER") +internal fun format(ruleSetProviderRef: (rulesConfigList: List?) -> RuleSetProvider, + @Language("kotlin") text: String, + fileName: String, + rulesConfigList: List? = null, + cb: LintErrorCallback = defaultCallback +): String { + val ruleSets = listOf(ruleSetProviderRef.invoke(rulesConfigList).get()) + return KtLint.format( + KtLint.ExperimentalParams( + text = text, + ruleSets = ruleSets, + fileName = fileName.removeSuffix("_copy"), + script = fileName.removeSuffix("_copy").endsWith("kts"), + cb = cb, + debug = true, + ) + ) +} + +/** + * This utility function lets you run arbitrary code on every node of given [code]. + * It also provides you with counter which can be incremented inside [applyToNode] and then will be compared to [expectedAsserts]. + * This allows you to keep track of how many assertions have actually been run on your code during tests. + * + * @param code + * @param expectedAsserts Number of expected times of assert invocation + * @param applyToNode Function to be called on each AST node, should increment counter if assert is called + */ +@OptIn(FeatureInAlphaState::class) +@Suppress("TYPE_ALIAS") +internal fun applyToCode(code: String, + expectedAsserts: Int, + applyToNode: (node: ASTNode, counter: AtomicInteger) -> Unit +) { + val counter = AtomicInteger(0) + KtLint.lint( + KtLint.ExperimentalParams( + text = code, + ruleSets = listOf( + RuleSet("test", object : Rule("astnode-utils-test") { + override fun visit(node: ASTNode, + autoCorrect: Boolean, + emit: EmitType + ) { + applyToNode(node, counter) + } + }) + ), + cb = { _, _ -> } + ) + ) + assertThat(counter.get()) + .`as`("Number of expected asserts") + .isEqualTo(expectedAsserts) +} + +/** + * Retries the execution of the [block]. + * + * @param attempts the number of attempts (must be positive). + * @param delayMillis the timeout (in milliseconds) between the consecutive + * attempts. The default is 0. Ignored if [attempts] is 1. + * @param lazyDefault allows to override the return value if none of the + * attempts succeeds. By default, the last exception is thrown. + * @param block the block to execute. + * @return the result of the execution of the [block], or whatever [lazyDefault] + * evaluates to if none of the attempts is successful. + */ +internal fun retry( + attempts: Int, + delayMillis: Long = 0L, + lazyDefault: (Throwable) -> T = { error -> throw error }, + block: () -> T +): T { + require(attempts > 0) { + "The number of attempts should be positive: $attempts" + } + + var lastError: Throwable? = null + + for (i in 1..attempts) { + try { + return block() + } catch (error: Throwable) { + lastError = error + } + + if (delayMillis > 0L) { + Thread.sleep(delayMillis) + } + } + + return lazyDefault(lastError ?: Exception("The block was never executed")) +} diff --git a/diktat-rules/src/test/resources/test/smoke/build.gradle_.kts b/diktat-ruleset/src/test/resources/test/smoke/build.gradle_.kts similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/build.gradle_.kts rename to diktat-ruleset/src/test/resources/test/smoke/build.gradle_.kts diff --git a/diktat-rules/src/test/resources/test/smoke/save.toml b/diktat-ruleset/src/test/resources/test/smoke/save.toml similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/save.toml rename to diktat-ruleset/src/test/resources/test/smoke/save.toml diff --git a/diktat-rules/src/test/resources/test/smoke/src/jsMain/kotlin/org/cqfn/diktat/scripts/ScriptExpected.kt b/diktat-ruleset/src/test/resources/test/smoke/src/jsMain/kotlin/org/cqfn/diktat/scripts/ScriptExpected.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/jsMain/kotlin/org/cqfn/diktat/scripts/ScriptExpected.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/jsMain/kotlin/org/cqfn/diktat/scripts/ScriptExpected.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/jsMain/kotlin/org/cqfn/diktat/scripts/ScriptTest.kt b/diktat-ruleset/src/test/resources/test/smoke/src/jsMain/kotlin/org/cqfn/diktat/scripts/ScriptTest.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/jsMain/kotlin/org/cqfn/diktat/scripts/ScriptTest.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/jsMain/kotlin/org/cqfn/diktat/scripts/ScriptTest.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Bug1Expected.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Bug1Expected.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Bug1Expected.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Bug1Expected.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Bug1Test.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Bug1Test.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Bug1Test.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Bug1Test.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/DefaultPackageExpected.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/DefaultPackageExpected.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/DefaultPackageExpected.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/DefaultPackageExpected.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/DefaultPackageTest.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/DefaultPackageTest.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/DefaultPackageTest.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/DefaultPackageTest.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example1-2Expected.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example1-2Expected.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example1-2Expected.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example1-2Expected.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example1Expected.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example1Expected.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example1Expected.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example1Expected.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example1Test.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example1Test.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example1Test.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example1Test.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example2Expected.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example2Expected.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example2Expected.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example2Expected.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example2Test.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example2Test.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example2Test.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example2Test.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Expected.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example3Expected.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Expected.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example3Expected.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Test.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example3Test.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Test.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example3Test.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Expected.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example4Expected.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Expected.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example4Expected.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Test.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example4Test.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Test.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example4Test.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example5Expected.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example5Expected.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example5Expected.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example5Expected.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example5Test.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example5Test.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example5Test.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example5Test.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example6Expected.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example6Expected.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example6Expected.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example6Expected.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example6Test.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example6Test.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example6Test.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example6Test.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example7Expected.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example7Expected.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example7Expected.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example7Expected.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example7Test.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example7Test.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example7Test.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example7Test.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Expected.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example8Expected.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Expected.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example8Expected.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Test.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example8Test.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Test.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example8Test.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/KdocFormattingMultilineTagsExpected.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/KdocFormattingMultilineTagsExpected.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/KdocFormattingMultilineTagsExpected.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/KdocFormattingMultilineTagsExpected.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/KdocFormattingMultilineTagsTest.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/KdocFormattingMultilineTagsTest.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/KdocFormattingMultilineTagsTest.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/KdocFormattingMultilineTagsTest.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/LocalVariableWithOffsetExpected.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/LocalVariableWithOffsetExpected.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/LocalVariableWithOffsetExpected.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/LocalVariableWithOffsetExpected.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/LocalVariableWithOffsetTest.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/LocalVariableWithOffsetTest.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/LocalVariableWithOffsetTest.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/LocalVariableWithOffsetTest.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineExpected.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineExpected.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineExpected.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineExpected.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineTest.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineTest.kt similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineTest.kt rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineTest.kt diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/kotlin-library-expected.gradle.kts b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/kotlin-library-expected.gradle.kts similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/kotlin-library-expected.gradle.kts rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/kotlin-library-expected.gradle.kts diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/kotlin-library.gradle.kts b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/kotlin-library.gradle.kts similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/kotlin-library.gradle.kts rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/kotlin-library.gradle.kts diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/save.toml b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/save.toml similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/save.toml rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/save.toml diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/script/PackageInScriptExpected.kts b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/script/PackageInScriptExpected.kts similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/script/PackageInScriptExpected.kts rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/script/PackageInScriptExpected.kts diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/script/PackageInScriptTest.kts b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/script/PackageInScriptTest.kts similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/script/PackageInScriptTest.kts rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/script/PackageInScriptTest.kts diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/script/SimpleRunInScriptExpected.kts b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/script/SimpleRunInScriptExpected.kts similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/script/SimpleRunInScriptExpected.kts rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/script/SimpleRunInScriptExpected.kts diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/script/SimpleRunInScriptTest.kts b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/script/SimpleRunInScriptTest.kts similarity index 100% rename from diktat-rules/src/test/resources/test/smoke/src/main/kotlin/script/SimpleRunInScriptTest.kts rename to diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/script/SimpleRunInScriptTest.kts diff --git a/pom.xml b/pom.xml index 90d6e31120..4dc73d8651 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,7 @@ 1.8 1.8 + 8 UTF-8 1.7.20 true @@ -91,6 +92,11 @@ kotlin-stdlib ${kotlin.version} + + org.jetbrains.kotlin + kotlin-stdlib-jdk7 + ${kotlin.version} + org.jetbrains.kotlin kotlin-stdlib-jdk8 From f22066629b499236b058de440ae36033fd13abd6 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 26 Oct 2022 13:08:57 +0300 Subject: [PATCH 02/39] split TestUtils.kt --- .../utils/indentation/IndentationConfig.kt | 16 +- .../kotlin/org/cqfn/diktat/util/TestUtils.kt | 183 ------------------ .../kotlin/org/cqfn/diktat/util/TestUtils.kt | 75 ------- 3 files changed, 8 insertions(+), 266 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/IndentationConfig.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/IndentationConfig.kt index ac063c352d..fc416e69c5 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/IndentationConfig.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/IndentationConfig.kt @@ -5,7 +5,7 @@ import org.cqfn.diktat.common.config.rules.RuleConfiguration /** * [RuleConfiguration] for indentation logic */ -internal class IndentationConfig(config: Map) : RuleConfiguration(config) { +class IndentationConfig(config: Map) : RuleConfiguration(config) { /** * Is newline at the end of a file needed */ @@ -87,7 +87,7 @@ internal class IndentationConfig(config: Map) : RuleConfiguratio override fun toString(): String = "${javaClass.simpleName}$configWithExplicitDefaults" - internal companion object { + companion object { internal const val ALIGNED_PARAMETERS = "alignedParameters" /** @@ -95,12 +95,12 @@ internal class IndentationConfig(config: Map) : RuleConfiguratio * `indentationSize`. */ private const val DEFAULT_INDENTATION_SIZE = 4 - internal const val EXTENDED_INDENT_AFTER_OPERATORS = "extendedIndentAfterOperators" - internal const val EXTENDED_INDENT_BEFORE_DOT = "extendedIndentBeforeDot" - internal const val EXTENDED_INDENT_FOR_EXPRESSION_BODIES = "extendedIndentForExpressionBodies" - internal const val EXTENDED_INDENT_OF_PARAMETERS = "extendedIndentOfParameters" - internal const val INDENTATION_SIZE = "indentationSize" - internal const val NEWLINE_AT_END = "newlineAtEnd" + const val EXTENDED_INDENT_AFTER_OPERATORS = "extendedIndentAfterOperators" + const val EXTENDED_INDENT_BEFORE_DOT = "extendedIndentBeforeDot" + const val EXTENDED_INDENT_FOR_EXPRESSION_BODIES = "extendedIndentForExpressionBodies" + const val EXTENDED_INDENT_OF_PARAMETERS = "extendedIndentOfParameters" + const val INDENTATION_SIZE = "indentationSize" + const val NEWLINE_AT_END = "newlineAtEnd" @Suppress("CUSTOM_GETTERS_SETTERS") private val IndentationConfig.configWithExplicitDefaults: Map diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt index 28d2522f58..c7fed5ad25 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt @@ -17,19 +17,10 @@ import com.pinterest.ktlint.core.api.FeatureInAlphaState import mu.KotlinLogging import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.fail -import org.assertj.core.api.SoftAssertions.assertSoftly import org.intellij.lang.annotations.Language import org.jetbrains.kotlin.com.intellij.lang.ASTNode -import java.io.File -import java.nio.file.NoSuchFileException -import java.nio.file.Path import java.util.concurrent.atomic.AtomicInteger -import java.util.function.Consumer -import kotlin.io.path.absolute -import kotlin.io.path.deleteIfExists -import kotlin.io.path.isDirectory -import kotlin.io.path.isSameFileAs internal const val TEST_FILE_NAME = "TestFileName.kt" @@ -43,141 +34,6 @@ internal val defaultCallback: (lintError: LintError, corrected: Boolean) -> Unit typealias LintErrorCallback = (LintError, Boolean) -> Unit -/** - * Compare [LintError]s from [this] with [expectedLintErrors] - * - * @param expectedLintErrors expected [LintError]s - */ -internal fun List.assertEquals(vararg expectedLintErrors: LintError) { - if (size == expectedLintErrors.size) { - assertThat(this) - .allSatisfy(Consumer { actual -> - val expected = expectedLintErrors[this@assertEquals.indexOf(actual)] - assertSoftly { sa -> - sa - .assertThat(actual.line) - .`as`("Line") - .isEqualTo(expected.line) - sa - .assertThat(actual.col) - .`as`("Column") - .isEqualTo(expected.col) - sa - .assertThat(actual.ruleId) - .`as`("Rule id") - .isEqualTo(expected.ruleId) - sa - .assertThat(actual.detail) - .`as`("Detailed message") - .isEqualTo(expected.detail) - sa - .assertThat(actual.canBeAutoCorrected) - .`as`("Can be autocorrected") - .isEqualTo(expected.canBeAutoCorrected) - } - }) - } else { - assertThat(this).containsExactly(*expectedLintErrors) - } -} - -/** - * Deletes the file if it exists, retrying as necessary if the file is - * blocked by another process (on Windows). - * - * @receiver the file or empty directory. - * @see Path.deleteIfExists - */ -@Suppress( - "EMPTY_BLOCK_STRUCTURE_ERROR", - "MAGIC_NUMBER", -) -internal fun Path.deleteIfExistsSilently() { - val attempts = 10 - - val deleted = retry(attempts, delayMillis = 100L, lazyDefault = { false }) { - deleteIfExists() - - /* - * Ignore the return code of `deleteIfExists()` (will be `false` - * if the file doesn't exist). - */ - true - } - - if (!deleted) { - log.warn { - "File \"${absolute()}\" not deleted after $attempts attempt(s)." - } - } -} - -/** - * @receiver the 1st operand. - * @param other the 2nd operand. - * @return `true` if, and only if, the two paths locate the same `JAVA_HOME`. - */ -internal fun Path.isSameJavaHomeAs(other: Path): Boolean = - isDirectory() && - (isSameFileAsSafe(other) || - resolve("jre").isSameFileAsSafe(other) || - other.resolve("jre").isSameFileAsSafe(this)) - -/** - * The same as [Path.isSameFileAs], but doesn't throw any [NoSuchFileException] - * if either of the operands doesn't exist. - * - * @receiver the 1st operand. - * @param other the 2nd operand. - * @return `true` if, and only if, the two paths locate the same file. - * @see Path.isSameFileAs - */ -internal fun Path.isSameFileAsSafe(other: Path): Boolean = - try { - isSameFileAs(other) - } catch (_: NoSuchFileException) { - false - } - -/** - * Prepends the `PATH` of this process builder with [pathEntry]. - * - * @param pathEntry the entry to be prepended to the `PATH`. - */ -internal fun ProcessBuilder.prependPath(pathEntry: Path) { - require(pathEntry.isDirectory()) { - "$pathEntry is not a directory" - } - - val environment = environment() - - val defaultPathKey = "PATH" - val defaultWindowsPathKey = "Path" - - val pathKey = when { - /*- - * Keys of the Windows environment are case-insensitive ("PATH" == "Path"). - * Keys of the Java interface to the environment are not ("PATH" != "Path"). - * This is an attempt to work around the inconsistency. - */ - System.getProperty("os.name").startsWith("Windows") -> environment.keys.firstOrNull { key -> - key.equals(defaultPathKey, ignoreCase = true) - } ?: defaultWindowsPathKey - - else -> defaultPathKey - } - - val pathSeparator = File.pathSeparatorChar - val oldPath = environment[pathKey] - - val newPath = when { - oldPath.isNullOrEmpty() -> pathEntry.toString() - else -> "$pathEntry$pathSeparator$oldPath" - } - - environment[pathKey] = newPath -} - /** * Casts a nullable value to a non-`null` one, similarly to the `!!` * operator. @@ -252,42 +108,3 @@ internal fun applyToCode(code: String, .`as`("Number of expected asserts") .isEqualTo(expectedAsserts) } - -/** - * Retries the execution of the [block]. - * - * @param attempts the number of attempts (must be positive). - * @param delayMillis the timeout (in milliseconds) between the consecutive - * attempts. The default is 0. Ignored if [attempts] is 1. - * @param lazyDefault allows to override the return value if none of the - * attempts succeeds. By default, the last exception is thrown. - * @param block the block to execute. - * @return the result of the execution of the [block], or whatever [lazyDefault] - * evaluates to if none of the attempts is successful. - */ -internal fun retry( - attempts: Int, - delayMillis: Long = 0L, - lazyDefault: (Throwable) -> T = { error -> throw error }, - block: () -> T -): T { - require(attempts > 0) { - "The number of attempts should be positive: $attempts" - } - - var lastError: Throwable? = null - - for (i in 1..attempts) { - try { - return block() - } catch (error: Throwable) { - lastError = error - } - - if (delayMillis > 0L) { - Thread.sleep(delayMillis) - } - } - - return lazyDefault(lastError ?: Exception("The block was never executed")) -} diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt index 28d2522f58..9e987bac4f 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt @@ -178,81 +178,6 @@ internal fun ProcessBuilder.prependPath(pathEntry: Path) { environment[pathKey] = newPath } -/** - * Casts a nullable value to a non-`null` one, similarly to the `!!` - * operator. - * - * @param lazyFailureMessage the message to evaluate in case of a failure. - * @return a non-`null` value. - */ -internal fun T?.assertNotNull(lazyFailureMessage: () -> String = { "Expecting actual not to be null" }): T = - this ?: fail(lazyFailureMessage()) - -/** - * @param ruleSetProviderRef - * @param text - * @param fileName - * @param rulesConfigList - * @param cb callback to be called on unhandled [LintError]s - * @return formatted code - */ -@Suppress("LAMBDA_IS_NOT_LAST_PARAMETER") -internal fun format(ruleSetProviderRef: (rulesConfigList: List?) -> RuleSetProvider, - @Language("kotlin") text: String, - fileName: String, - rulesConfigList: List? = null, - cb: LintErrorCallback = defaultCallback -): String { - val ruleSets = listOf(ruleSetProviderRef.invoke(rulesConfigList).get()) - return KtLint.format( - KtLint.ExperimentalParams( - text = text, - ruleSets = ruleSets, - fileName = fileName.removeSuffix("_copy"), - script = fileName.removeSuffix("_copy").endsWith("kts"), - cb = cb, - debug = true, - ) - ) -} - -/** - * This utility function lets you run arbitrary code on every node of given [code]. - * It also provides you with counter which can be incremented inside [applyToNode] and then will be compared to [expectedAsserts]. - * This allows you to keep track of how many assertions have actually been run on your code during tests. - * - * @param code - * @param expectedAsserts Number of expected times of assert invocation - * @param applyToNode Function to be called on each AST node, should increment counter if assert is called - */ -@OptIn(FeatureInAlphaState::class) -@Suppress("TYPE_ALIAS") -internal fun applyToCode(code: String, - expectedAsserts: Int, - applyToNode: (node: ASTNode, counter: AtomicInteger) -> Unit -) { - val counter = AtomicInteger(0) - KtLint.lint( - KtLint.ExperimentalParams( - text = code, - ruleSets = listOf( - RuleSet("test", object : Rule("astnode-utils-test") { - override fun visit(node: ASTNode, - autoCorrect: Boolean, - emit: EmitType - ) { - applyToNode(node, counter) - } - }) - ), - cb = { _, _ -> } - ) - ) - assertThat(counter.get()) - .`as`("Number of expected asserts") - .isEqualTo(expectedAsserts) -} - /** * Retries the execution of the [block]. * From ce9a66e6f8fcd4ab62d0d48feff3c6609c47efa1 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 26 Oct 2022 17:01:31 +0300 Subject: [PATCH 03/39] refactored smoke tests --- .../org/cqfn/diktat/util/FixTestBase.kt | 96 ++----------------- .../org/cqfn/diktat/util/FixTestBaseCommon.kt | 94 ++++++++++++++++++ .../kotlin/org/cqfn/diktat/util/TestUtils.kt | 14 ++- .../ruleset/smoke/DiktatSaveSmokeTest.kt | 21 +++- .../diktat/ruleset/smoke/DiktatSmokeTest.kt | 7 +- .../ruleset/smoke/DiktatSmokeTestBase.kt | 78 ++++++++------- .../util/{TestUtils.kt => SmokeTestUtils.kt} | 11 --- .../processing/TestComparatorUnit.kt | 11 ++- 8 files changed, 179 insertions(+), 153 deletions(-) create mode 100644 diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBaseCommon.kt rename diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/{TestUtils.kt => SmokeTestUtils.kt} (92%) 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 f3696f66c7..61160464b9 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 @@ -1,51 +1,28 @@ package org.cqfn.diktat.util import org.cqfn.diktat.common.config.rules.RulesConfig -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.intellij.lang.annotations.Language -import org.junit.jupiter.api.Assertions import java.nio.file.Path -import kotlin.io.path.bufferedWriter -import kotlin.io.path.div - -const val SAVE_VERSION: String = "0.3.2" /** * @property resourceFilePath path to files which will be compared in tests */ open class FixTestBase( - protected val resourceFilePath: String, - private val ruleSetProviderRef: (rulesConfigList: List?) -> RuleSetProvider, - private val cb: LintErrorCallback = defaultCallback, - private val rulesConfigList: List? = null, + resourceFilePath: String, + ruleSupplier: (rulesConfigList: List) -> Rule, + private val defaultRulesConfigList: List = emptyList(), +) : FixTestBaseCommon( + resourceFilePath = resourceFilePath, ) { - /** - * testComparatorUnit - */ - protected val testComparatorUnit = TestComparatorUnit(resourceFilePath) { text, fileName -> - format(ruleSetProviderRef, text, fileName, rulesConfigList, cb = cb) - } - - constructor(resourceFilePath: String, - ruleSupplier: (rulesConfigList: List) -> Rule, - rulesConfigList: List? = null, - cb: LintErrorCallback = defaultCallback - ) : this( - resourceFilePath, - { overrideRulesConfigList -> DiktatRuleSetProvider4Test(ruleSupplier, overrideRulesConfigList) }, - cb, - rulesConfigList - ) + private val ruleSetProviderRef = { rulesConfigList: List? -> DiktatRuleSetProvider4Test(ruleSupplier, rulesConfigList ?: defaultRulesConfigList) } /** * @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 overrideRulesConfigList optional override to [rulesConfigList] + * @param overrideRulesConfigList optional override to [defaultRulesConfigList] * @param trimLastEmptyLine whether the last (empty) line should be - * discarded when reading the content of [testFileStr]. + * discarded when reading the content of [testPath]. * @see fixAndCompareContent */ protected fun fixAndCompare( @@ -53,44 +30,7 @@ open class FixTestBase( testPath: String, overrideRulesConfigList: List = emptyList(), trimLastEmptyLine: Boolean = false, - ) { - val testComparatorUnit = if (overrideRulesConfigList.isNotEmpty()) { - TestComparatorUnit(resourceFilePath) { text, fileName -> - format(ruleSetProviderRef, text, fileName, overrideRulesConfigList) - } - } else { - testComparatorUnit - } - - Assertions.assertTrue( - testComparatorUnit - .compareFilesFromResources(expectedPath, testPath, trimLastEmptyLine) - ) - } - - 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" - System.getProperty("os.name").startsWith("Windows", ignoreCase = true) -> "save-$SAVE_VERSION-mingwX64.exe" - else -> "" - } - - /** - * @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", "chmod 777 $savePath ; ./$savePath $filesDir/src/main/kotlin $testPath --log all") - else -> ProcessBuilder(savePath, "$filesDir/src/main/kotlin", testPath) - } - return result - } + ) = super.fixAndCompare(expectedPath, testPath, ruleSetProviderRef(overrideRulesConfigList), trimLastEmptyLine) /** * Unlike [fixAndCompare], this method doesn't perform any assertions. @@ -111,21 +51,5 @@ open class FixTestBase( @Language("kotlin") expectedContent: String = actualContent, tempDir: Path, overrideRulesConfigList: List? = null - ): FileComparisonResult { - val actual = tempDir / "actual.kt" - actual.bufferedWriter().use { out -> - out.write(actualContent) - } - - val expected = tempDir / "expected.kt" - expected.bufferedWriter().use { out -> - out.write(expectedContent) - } - - val testComparatorUnit = TestComparatorUnit(tempDir.toString()) { text, fileName -> - format(ruleSetProviderRef, text, fileName, overrideRulesConfigList ?: rulesConfigList, cb) - } - - return testComparatorUnit.compareFilesFromFileSystem(expected, actual) - } + ) = super.fixAndCompareContent(actualContent, expectedContent, tempDir, ruleSetProviderRef(overrideRulesConfigList)) } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBaseCommon.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBaseCommon.kt new file mode 100644 index 0000000000..fdc441d779 --- /dev/null +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBaseCommon.kt @@ -0,0 +1,94 @@ +package org.cqfn.diktat.util + +import org.cqfn.diktat.test.framework.processing.FileComparisonResult +import org.cqfn.diktat.test.framework.processing.TestComparatorUnit +import com.pinterest.ktlint.core.RuleSetProvider +import org.intellij.lang.annotations.Language +import org.junit.jupiter.api.Assertions +import java.nio.file.Path +import java.nio.file.Paths +import kotlin.io.path.bufferedWriter +import kotlin.io.path.div + +/** + * @property resourceFilePath path to files which will be compared in tests + */ +open class FixTestBaseCommon( + protected val resourceFilePath: String, + private val cb: LintErrorCallback = defaultCallback, +) { + /** + * @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 ruleSetProvider provider for rules which should be applied + * @param trimLastEmptyLine whether the last (empty) line should be + * discarded when reading the content of [testPath]. + * @see fixAndCompareContent + */ + protected fun fixAndCompare( + expectedPath: String, + testPath: String, + ruleSetProvider: RuleSetProvider, + trimLastEmptyLine: Boolean = false, + ) { + val expectedPathResource = requireNotNull(javaClass.classLoader.getResource("$resourceFilePath/$expectedPath")) { + "Not able to find a file for running test: $expectedPath" + } + val testPathResource = requireNotNull(javaClass.classLoader.getResource("$resourceFilePath/$testPath")) { + "Not able to find a file for running test: $testPath" + } + + val testComparatorUnit = TestComparatorUnit(resourceFilePath) { text, fileName -> + format( + ruleSetProviderRef = { ruleSetProvider }, + text = text, + fileName = fileName, + cb = cb + ) + } + Assertions.assertTrue( + testComparatorUnit + .compareFilesFromFileSystem(Paths.get(expectedPathResource.toURI()), Paths.get(testPathResource.toURI()), trimLastEmptyLine) + .isSuccessful + ) + } + + /** + * @param actualContent the original file content (may well be modified as + * fixes are applied). + * @param expectedContent the content the file is expected to have after the + * fixes are applied. + * @param tempDir the temporary directory (usually injected by _JUnit_). + * @param ruleSetProvider provider for rules which should be applied + * @return the result of file content comparison. + * @see fixAndCompare + */ + @Suppress("FUNCTION_BOOLEAN_PREFIX") + protected fun fixAndCompareContent( + @Language("kotlin") actualContent: String, + @Language("kotlin") expectedContent: String = actualContent, + tempDir: Path, + ruleSetProvider: RuleSetProvider, + ): FileComparisonResult { + val actual = tempDir / "actual.kt" + actual.bufferedWriter().use { out -> + out.write(actualContent) + } + + val expected = tempDir / "expected.kt" + expected.bufferedWriter().use { out -> + out.write(expectedContent) + } + + val testComparatorUnit = TestComparatorUnit(resourceFilePath) { text, fileName -> + format( + ruleSetProviderRef = { ruleSetProvider }, + text = text, + fileName = fileName, + cb = cb + ) + } + return testComparatorUnit + .compareFilesFromFileSystem(expected, actual, false) + } +} diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt index c7fed5ad25..927137fc5a 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt @@ -4,7 +4,6 @@ package org.cqfn.diktat.util -import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.common.utils.loggerWithKtlintConfig import org.cqfn.diktat.ruleset.constants.EmitType @@ -48,18 +47,17 @@ internal fun T?.assertNotNull(lazyFailureMessage: () -> String = { "Expectin * @param ruleSetProviderRef * @param text * @param fileName - * @param rulesConfigList * @param cb callback to be called on unhandled [LintError]s * @return formatted code */ @Suppress("LAMBDA_IS_NOT_LAST_PARAMETER") -internal fun format(ruleSetProviderRef: (rulesConfigList: List?) -> RuleSetProvider, - @Language("kotlin") text: String, - fileName: String, - rulesConfigList: List? = null, - cb: LintErrorCallback = defaultCallback +internal fun format( + ruleSetProviderRef: () -> RuleSetProvider, + @Language("kotlin") text: String, + fileName: String, + cb: LintErrorCallback = defaultCallback ): String { - val ruleSets = listOf(ruleSetProviderRef.invoke(rulesConfigList).get()) + val ruleSets = listOf(ruleSetProviderRef().get()) return KtLint.format( KtLint.ExperimentalParams( text = text, diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt index 8d8e13c883..f173f2db68 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt @@ -33,11 +33,11 @@ import kotlin.system.measureNanoTime class DiktatSaveSmokeTest : DiktatSmokeTestBase() { override val isLintErrors = false override fun fixAndCompare( - config: String, + config: Path, expected: String, test: String, ) { - saveSmokeTest(Path(config), test) + saveSmokeTest(config, test) } /** @@ -95,6 +95,23 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { } } + /** + * @param testPath path to file with code that will be transformed by formatter, relative to [resourceFilePath] + * @return ProcessBuilder + */ + private 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", "chmod 777 $savePath ; ./$savePath $filesDir/src/main/kotlin $testPath --log all") + else -> ProcessBuilder(savePath, "$filesDir/src/main/kotlin", testPath) + } + return result + } + companion object { @Suppress("EMPTY_BLOCK_STRUCTURE_ERROR") private val logger = KotlinLogging.loggerWithKtlintConfig { } diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt index 068ebc6579..25049b2df5 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt @@ -1,6 +1,8 @@ package org.cqfn.diktat.ruleset.smoke import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider +import java.nio.file.Path +import kotlin.io.path.absolutePathString /** * Test for [DiktatRuleSetProvider] in autocorrect mode as a whole. All rules are applied to a file. @@ -9,11 +11,12 @@ import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider */ class DiktatSmokeTest : DiktatSmokeTestBase() { override val isLintErrors = true + override fun fixAndCompare( - config: String, + config: Path, expected: String, test: String, ) { - fixAndCompare(expected, test, emptyList(), true) + fixAndCompare(expected, test, DiktatRuleSetProvider(config.absolutePathString()), true) } } diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt index 9da31e9d6f..771cd8b42f 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt @@ -33,7 +33,7 @@ import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_AFTER_OPERATORS import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_BEFORE_DOT import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_FOR_EXPRESSION_BODIES -import org.cqfn.diktat.util.FixTestBase +import org.cqfn.diktat.util.FixTestBaseCommon import org.cqfn.diktat.util.assertEquals import com.charleskorn.kaml.Yaml @@ -46,9 +46,14 @@ import org.junit.jupiter.api.Tag import org.junit.jupiter.api.Test import org.junit.jupiter.api.Timeout import java.io.File +import java.nio.file.Path +import java.nio.file.Paths import java.time.LocalDate import java.util.concurrent.TimeUnit.SECONDS +import kotlin.io.path.absolutePathString +import kotlin.io.path.createTempFile +import kotlin.io.path.writeText import kotlinx.serialization.builtins.ListSerializer @@ -56,14 +61,14 @@ typealias RuleToConfig = Map> /** * Base class for smoke test classes + * + * @property unfixedLintErrors */ abstract class DiktatSmokeTestBase( private val unfixedLintErrors: MutableList = mutableListOf(), - // by default using same yml config as for diktat code style check, but allow to override - private var configFilePath: String = DEFAULT_CONFIG_PATH, -) : FixTestBase("test/smoke/src/main/kotlin", - { DiktatRuleSetProvider(configFilePath) }, - { lintError, _ -> unfixedLintErrors.add(lintError) }, +) : FixTestBaseCommon( + "test/smoke/src/main/kotlin", + { lintError, _ -> unfixedLintErrors.add(lintError) } ) { /** * Flag to check LintError in smoke tests @@ -77,7 +82,7 @@ abstract class DiktatSmokeTestBase( * @param rulesToOverride */ @Suppress("UnsafeCallOnNullableType") - open fun overrideRulesConfig(rulesToDisable: List, rulesToOverride: RuleToConfig = emptyMap()) { + private fun prepareOverriddenRulesConfig(rulesToDisable: List = emptyList(), rulesToOverride: RuleToConfig = emptyMap()): Path { val rulesConfig = RulesConfigReader(javaClass.classLoader).readResource(DEFAULT_CONFIG_PATH)!! .toMutableList() .also { rulesConfig -> @@ -91,15 +96,13 @@ abstract class DiktatSmokeTestBase( } } .toList() - kotlin.io.path.createTempFile() - .toFile() + return createTempFile() .also { - configFilePath = it.absolutePath + it.writeText( + Yaml(configuration = YamlConfiguration(strictMode = true)) + .encodeToString(ListSerializer(RulesConfig.serializer()), rulesConfig) + ) } - .writeText( - Yaml(configuration = YamlConfiguration(strictMode = true)) - .encodeToString(ListSerializer(RulesConfig.serializer()), rulesConfig) - ) } @BeforeEach @@ -107,16 +110,11 @@ abstract class DiktatSmokeTestBase( unfixedLintErrors.clear() } - @AfterEach - fun tearDown() { - configFilePath = DEFAULT_CONFIG_PATH - } - @Test @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) fun `regression - should not fail if package is not set`() { - overrideRulesConfig(listOf(Warnings.PACKAGE_NAME_MISSING, Warnings.PACKAGE_NAME_INCORRECT_PATH, + val configFilePath = prepareOverriddenRulesConfig(listOf(Warnings.PACKAGE_NAME_MISSING, Warnings.PACKAGE_NAME_INCORRECT_PATH, Warnings.PACKAGE_NAME_INCORRECT_PREFIX)) fixAndCompare(configFilePath, "DefaultPackageExpected.kt", "DefaultPackageTest.kt") } @@ -125,21 +123,21 @@ abstract class DiktatSmokeTestBase( @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) fun `smoke test #8 - anonymous function`() { - fixAndCompare(configFilePath, "Example8Expected.kt", "Example8Test.kt") + fixAndCompare(prepareOverriddenRulesConfig(), "Example8Expected.kt", "Example8Test.kt") } @Test @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) fun `smoke test #7`() { - fixAndCompare(configFilePath, "Example7Expected.kt", "Example7Test.kt") + fixAndCompare(prepareOverriddenRulesConfig(), "Example7Expected.kt", "Example7Test.kt") } @Test @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) fun `smoke test #6`() { - overrideRulesConfig( + val configFilePath = prepareOverriddenRulesConfig( rulesToDisable = emptyList(), rulesToOverride = mapOf( WRONG_INDENTATION.name to mapOf( @@ -156,7 +154,7 @@ abstract class DiktatSmokeTestBase( @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) fun `smoke test #5`() { - overrideRulesConfig(emptyList(), + val configFilePath = prepareOverriddenRulesConfig(emptyList(), mapOf( Warnings.HEADER_MISSING_OR_WRONG_COPYRIGHT.name to mapOf( "isCopyrightMandatory" to "true", @@ -191,28 +189,28 @@ abstract class DiktatSmokeTestBase( @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) fun `smoke test #4`() { - fixAndCompare(configFilePath, "Example4Expected.kt", "Example4Test.kt") + fixAndCompare(prepareOverriddenRulesConfig(), "Example4Expected.kt", "Example4Test.kt") } @Test @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) fun `smoke test #3`() { - fixAndCompare(configFilePath, "Example3Expected.kt", "Example3Test.kt") + fixAndCompare(prepareOverriddenRulesConfig(), "Example3Expected.kt", "Example3Test.kt") } @Test @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) fun `regression - shouldn't throw exception in cases similar to #371`() { - fixAndCompare(configFilePath, "Bug1Expected.kt", "Bug1Test.kt") + fixAndCompare(prepareOverriddenRulesConfig(), "Bug1Expected.kt", "Bug1Test.kt") } @Test @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) fun `smoke test #2`() { - overrideRulesConfig( + val configFilePath = prepareOverriddenRulesConfig( rulesToDisable = emptyList(), rulesToOverride = mapOf( WRONG_INDENTATION.name to mapOf( @@ -238,7 +236,7 @@ abstract class DiktatSmokeTestBase( @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) fun `smoke test #1`() { - overrideRulesConfig( + val configFilePath = prepareOverriddenRulesConfig( rulesToDisable = emptyList(), rulesToOverride = mapOf( WRONG_INDENTATION.name to mapOf( @@ -270,35 +268,35 @@ abstract class DiktatSmokeTestBase( @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) fun `smoke test with kts files #2`() { - fixAndCompare(configFilePath, "script/SimpleRunInScriptExpected.kts", "script/SimpleRunInScriptTest.kts") + fixAndCompare(prepareOverriddenRulesConfig(), "script/SimpleRunInScriptExpected.kts", "script/SimpleRunInScriptTest.kts") } @Test @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) fun `smoke test with kts files with package name`() { - fixAndCompare(configFilePath, "script/PackageInScriptExpected.kts", "script/PackageInScriptTest.kts") + fixAndCompare(prepareOverriddenRulesConfig(), "script/PackageInScriptExpected.kts", "script/PackageInScriptTest.kts") } @Test @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) fun `regression - should correctly handle tags with empty lines`() { - fixAndCompare(configFilePath, "KdocFormattingMultilineTagsExpected.kt", "KdocFormattingMultilineTagsTest.kt") + fixAndCompare(prepareOverriddenRulesConfig(), "KdocFormattingMultilineTagsExpected.kt", "KdocFormattingMultilineTagsTest.kt") } @Test @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) fun `regression - FP of local variables rule`() { - fixAndCompare(configFilePath, "LocalVariableWithOffsetExpected.kt", "LocalVariableWithOffsetTest.kt") + fixAndCompare(prepareOverriddenRulesConfig(), "LocalVariableWithOffsetExpected.kt", "LocalVariableWithOffsetTest.kt") } @Test @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) fun `fix can cause long line`() { - overrideRulesConfig( + val configFilePath = prepareOverriddenRulesConfig( rulesToDisable = emptyList(), rulesToOverride = mapOf( WRONG_INDENTATION.name to mapOf( @@ -313,7 +311,7 @@ abstract class DiktatSmokeTestBase( @Tag("DiktatRuleSetProvider") fun `smoke test with multiplatform project layout`() { fixAndCompare( - configFilePath, + prepareOverriddenRulesConfig(), "../../jsMain/kotlin/org/cqfn/diktat/scripts/ScriptExpected.kt", "../../jsMain/kotlin/org/cqfn/diktat/scripts/ScriptTest.kt" ) @@ -322,7 +320,7 @@ abstract class DiktatSmokeTestBase( @Test @Tag("DiktatRuleSetProvider") fun `smoke test with kts files`() { - overrideRulesConfig( + val configFilePath = prepareOverriddenRulesConfig( emptyList(), mapOf( WRONG_INDENTATION.name to mapOf( @@ -336,7 +334,7 @@ abstract class DiktatSmokeTestBase( .toURI() .let { val tmpTestFile = File(it).parentFile.resolve("build.gradle.kts") - File(it).copyTo(tmpTestFile) + File(it).copyTo(tmpTestFile, true) tmpTestFile } val tmpFilePath = "../../../build.gradle.kts" @@ -348,7 +346,7 @@ abstract class DiktatSmokeTestBase( @Test @Tag("DiktatRuleSetProvider") fun `smoke test with gradle script plugin`() { - fixAndCompare(configFilePath, "kotlin-library-expected.gradle.kts", "kotlin-library.gradle.kts") + fixAndCompare(prepareOverriddenRulesConfig(), "kotlin-library-expected.gradle.kts", "kotlin-library.gradle.kts") Assertions.assertEquals( LintError(2, 1, "$DIKTAT_RULE_SET_ID:${CommentsRule.NAME_ID}", "[COMMENTED_OUT_CODE] you should not comment out code, " + "use VCS to save it in history and delete this block: import org.jetbrains.kotlin.gradle.dsl.jvm", false), @@ -359,7 +357,7 @@ abstract class DiktatSmokeTestBase( @Test @Tag("DiktatRuleSetProvider") fun `disable chapters`() { - overrideRulesConfig( + val configFilePath = prepareOverriddenRulesConfig( emptyList(), mapOf( DIKTAT_COMMON to mapOf( @@ -383,7 +381,7 @@ abstract class DiktatSmokeTestBase( } abstract fun fixAndCompare( - config: String, + config: Path, expected: String, test: String, ) diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt similarity index 92% rename from diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt rename to diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt index 9e987bac4f..4843e06f55 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt @@ -4,27 +4,16 @@ package org.cqfn.diktat.util -import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.common.utils.loggerWithKtlintConfig -import org.cqfn.diktat.ruleset.constants.EmitType -import com.pinterest.ktlint.core.KtLint import com.pinterest.ktlint.core.LintError -import com.pinterest.ktlint.core.Rule -import com.pinterest.ktlint.core.RuleSet -import com.pinterest.ktlint.core.RuleSetProvider -import com.pinterest.ktlint.core.api.FeatureInAlphaState import mu.KotlinLogging import org.assertj.core.api.Assertions.assertThat -import org.assertj.core.api.Assertions.fail import org.assertj.core.api.SoftAssertions.assertSoftly -import org.intellij.lang.annotations.Language -import org.jetbrains.kotlin.com.intellij.lang.ASTNode import java.io.File import java.nio.file.NoSuchFileException import java.nio.file.Path -import java.util.concurrent.atomic.AtomicInteger import java.util.function.Consumer import kotlin.io.path.absolute import kotlin.io.path.deleteIfExists diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestComparatorUnit.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestComparatorUnit.kt index 36d592bcde..ac3e85750c 100644 --- a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestComparatorUnit.kt +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestComparatorUnit.kt @@ -32,7 +32,7 @@ class TestComparatorUnit(private val resourceFilePath: String, * @param testFileStr the name of the resource which has the original content. * @param trimLastEmptyLine whether the last (empty) line should be * discarded when reading the content of [testFileStr]. - * @return `true` if transformed file equals expected result, `false` otherwise. + * @return the result of file comparison by their content. * @see compareFilesFromFileSystem */ @Suppress("FUNCTION_BOOLEAN_PREFIX") @@ -40,18 +40,21 @@ class TestComparatorUnit(private val resourceFilePath: String, expectedResult: String, testFileStr: String, trimLastEmptyLine: Boolean = false - ): Boolean { + ): FileComparisonResult { val expectedPath = javaClass.classLoader.getResource("$resourceFilePath/$expectedResult") val testPath = javaClass.classLoader.getResource("$resourceFilePath/$testFileStr") if (testPath == null || expectedPath == null) { log.error("Not able to find files for running test: $expectedResult and $testFileStr") - return false + return FileComparisonResult( + isSuccessful = false, + actualContent = "// $resourceFilePath/$expectedResult is found: ${testPath != null}", + expectedContent = "// $resourceFilePath/$testFileStr is found: ${expectedPath != null}") } return compareFilesFromFileSystem( Paths.get(expectedPath.toURI()), Paths.get(testPath.toURI()), - trimLastEmptyLine).isSuccessful + trimLastEmptyLine) } /** From 24a376cdd095242f696126cf4e8368035e371f21 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 26 Oct 2022 17:33:17 +0300 Subject: [PATCH 04/39] fixed test with kts --- .../kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt index 25049b2df5..6de98eadf5 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt @@ -17,6 +17,6 @@ class DiktatSmokeTest : DiktatSmokeTestBase() { expected: String, test: String, ) { - fixAndCompare(expected, test, DiktatRuleSetProvider(config.absolutePathString()), true) + fixAndCompare(expected, test, DiktatRuleSetProvider(config.absolutePathString()), true) } } From 856576828af93549dca738c03b097c40b880c795 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 26 Oct 2022 17:57:58 +0300 Subject: [PATCH 05/39] diktatFix --- .../org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt index 771cd8b42f..ed3c541ab9 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt @@ -20,7 +20,6 @@ import org.cqfn.diktat.ruleset.constants.Warnings.MISSING_KDOC_CLASS_ELEMENTS import org.cqfn.diktat.ruleset.constants.Warnings.MISSING_KDOC_ON_FUNCTION import org.cqfn.diktat.ruleset.constants.Warnings.MISSING_KDOC_TOP_LEVEL import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_INDENTATION -import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider import org.cqfn.diktat.ruleset.rules.chapter1.FileNaming import org.cqfn.diktat.ruleset.rules.chapter2.comments.CommentsRule import org.cqfn.diktat.ruleset.rules.chapter2.comments.HeaderCommentRule @@ -39,7 +38,6 @@ import org.cqfn.diktat.util.assertEquals import com.charleskorn.kaml.Yaml import com.charleskorn.kaml.YamlConfiguration import com.pinterest.ktlint.core.LintError -import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Tag @@ -47,11 +45,9 @@ import org.junit.jupiter.api.Test import org.junit.jupiter.api.Timeout import java.io.File import java.nio.file.Path -import java.nio.file.Paths import java.time.LocalDate import java.util.concurrent.TimeUnit.SECONDS -import kotlin.io.path.absolutePathString import kotlin.io.path.createTempFile import kotlin.io.path.writeText From 3ae8383b505477d3f6682f3bb3ed39d3244a88e5 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 27 Oct 2022 11:21:50 +0300 Subject: [PATCH 06/39] removed shade plugin --- diktat-rules/pom.xml | 46 -------------------------------------------- pom.xml | 5 ----- 2 files changed, 51 deletions(-) diff --git a/diktat-rules/pom.xml b/diktat-rules/pom.xml index c0762a8a59..b117d8c995 100644 --- a/diktat-rules/pom.xml +++ b/diktat-rules/pom.xml @@ -260,52 +260,6 @@ - - - org.apache.maven.plugins - maven-shade-plugin - - - false - - false - - ${project.name}-${project.version}-fat-jar-for-smoke-tests - - - com.squareup:kotlinpoet - net.java.dev.jna:jna - org.jetbrains.intellij.deps:trove4j - org.jetbrains.kotlin:kotlin-compiler-embeddable - org.jetbrains.kotlin:kotlin-daemon-embeddable - org.jetbrains.kotlin:kotlin-reflect - org.jetbrains.kotlin:kotlin-script-runtime - org.jetbrains.kotlin:kotlin-stdlib-common - org.jetbrains.kotlin:kotlin-stdlib-jdk7 - org.jetbrains.kotlin:kotlin-stdlib-jdk8 - org.jetbrains.kotlin:kotlin-stdlib - org.jetbrains:annotations - - - - - - fat-jar-for-smoke-tests - package - - shade - - - - - - org.apache.maven.plugins maven-jar-plugin diff --git a/pom.xml b/pom.xml index 4dc73d8651..facd190290 100644 --- a/pom.xml +++ b/pom.xml @@ -284,11 +284,6 @@ maven-failsafe-plugin 3.0.0-M7 - - org.apache.maven.plugins - maven-shade-plugin - 3.3.0 - org.codehaus.mojo versions-maven-plugin From 9c73968b6fd335d6890fa322e25968e87464bb60 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 27 Oct 2022 11:25:56 +0300 Subject: [PATCH 07/39] fixed default value for rulesConfig --- .../src/test/kotlin/org/cqfn/diktat/util/FixTestBase.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 61160464b9..452dcd1be4 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 @@ -28,7 +28,7 @@ open class FixTestBase( protected fun fixAndCompare( expectedPath: String, testPath: String, - overrideRulesConfigList: List = emptyList(), + overrideRulesConfigList: List? = null, trimLastEmptyLine: Boolean = false, ) = super.fixAndCompare(expectedPath, testPath, ruleSetProviderRef(overrideRulesConfigList), trimLastEmptyLine) From a5a048edecdf216887b6f1f94b2d0ca8a98680cc Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 27 Oct 2022 12:00:51 +0300 Subject: [PATCH 08/39] fixed default value for rulesConfig --- .../src/test/kotlin/org/cqfn/diktat/util/FixTestBase.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 452dcd1be4..18beca6a1c 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 @@ -11,7 +11,7 @@ import java.nio.file.Path open class FixTestBase( resourceFilePath: String, ruleSupplier: (rulesConfigList: List) -> Rule, - private val defaultRulesConfigList: List = emptyList(), + private val defaultRulesConfigList: List? = null, ) : FixTestBaseCommon( resourceFilePath = resourceFilePath, ) { From 55d6f7d5669d20c57e0de57d075678ff2c1ecf6e Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 27 Oct 2022 12:55:32 +0300 Subject: [PATCH 09/39] unlink smoke tests --- .../org/cqfn/diktat/util/FixTestBaseCommon.kt | 9 +-- .../ruleset/smoke/DiktatSaveSmokeTest.kt | 7 +- .../diktat/ruleset/smoke/DiktatSmokeTest.kt | 28 ++++++- .../ruleset/smoke/DiktatSmokeTestBase.kt | 81 +++++++++---------- .../processing/TestComparatorUnit.kt | 19 +++++ .../diktat/test/framework/util/TestUtils.kt | 45 +++++++++++ 6 files changed, 135 insertions(+), 54 deletions(-) create mode 100644 diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBaseCommon.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBaseCommon.kt index fdc441d779..858eec8163 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBaseCommon.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBaseCommon.kt @@ -31,13 +31,6 @@ open class FixTestBaseCommon( ruleSetProvider: RuleSetProvider, trimLastEmptyLine: Boolean = false, ) { - val expectedPathResource = requireNotNull(javaClass.classLoader.getResource("$resourceFilePath/$expectedPath")) { - "Not able to find a file for running test: $expectedPath" - } - val testPathResource = requireNotNull(javaClass.classLoader.getResource("$resourceFilePath/$testPath")) { - "Not able to find a file for running test: $testPath" - } - val testComparatorUnit = TestComparatorUnit(resourceFilePath) { text, fileName -> format( ruleSetProviderRef = { ruleSetProvider }, @@ -48,7 +41,7 @@ open class FixTestBaseCommon( } Assertions.assertTrue( testComparatorUnit - .compareFilesFromFileSystem(Paths.get(expectedPathResource.toURI()), Paths.get(testPathResource.toURI()), trimLastEmptyLine) + .compareFilesFromResources(expectedPath, testPath, trimLastEmptyLine) .isSuccessful ) } diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt index f173f2db68..cfd1f031b4 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt @@ -5,6 +5,7 @@ import org.cqfn.diktat.util.deleteIfExistsSilently import org.cqfn.diktat.util.isSameJavaHomeAs import org.cqfn.diktat.util.prependPath import org.cqfn.diktat.util.retry +import com.pinterest.ktlint.core.LintError import mu.KotlinLogging import org.assertj.core.api.Assertions.assertThat @@ -31,15 +32,19 @@ import kotlin.system.measureNanoTime @DisabledOnOs(OS.MAC) class DiktatSaveSmokeTest : DiktatSmokeTestBase() { - override val isLintErrors = false override fun fixAndCompare( config: Path, expected: String, test: String, + trimLastEmptyLine: Boolean, ) { saveSmokeTest(config, test) } + override fun assertUnfixedLintErrors(lintErrorsConsumer: (List) -> Unit) { + // do nothing, we can't check unfixed lint errors here + } + /** * @param testPath path to file with code that will be transformed by formatter, relative to [resourceFilePath] * @param configFilePath path of diktat-analysis file diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt index 6de98eadf5..3b54891bfa 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt @@ -1,6 +1,10 @@ package org.cqfn.diktat.ruleset.smoke import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider +import org.cqfn.diktat.test.framework.processing.TestComparatorUnit +import com.pinterest.ktlint.core.LintError +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.BeforeEach import java.nio.file.Path import kotlin.io.path.absolutePathString @@ -10,13 +14,33 @@ import kotlin.io.path.absolutePathString * may change after some changes to text or other rules. */ class DiktatSmokeTest : DiktatSmokeTestBase() { - override val isLintErrors = true + private val unfixedLintErrors: MutableList = mutableListOf() override fun fixAndCompare( config: Path, expected: String, test: String, + trimLastEmptyLine: Boolean, ) { - fixAndCompare(expected, test, DiktatRuleSetProvider(config.absolutePathString()), true) + Assertions.assertTrue( + getTestComparatorUnit(config) + .compareFilesFromResources(expected, test, trimLastEmptyLine) + .isSuccessful + ) } + + @BeforeEach + internal fun setUp() { + unfixedLintErrors.clear() + } + + override fun assertUnfixedLintErrors(lintErrorsConsumer: (List) -> Unit) { + lintErrorsConsumer(unfixedLintErrors) + } + + private fun getTestComparatorUnit(config: Path) = TestComparatorUnit( + resourceFilePath = RESOURCE_FILE_PATH, + ruleSetProviderSupplier = { DiktatRuleSetProvider(config.absolutePathString()) }, + cb = { lintError, _ -> unfixedLintErrors.add(lintError) } + ) } diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt index ed3c541ab9..aafbf9e14e 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt @@ -32,14 +32,12 @@ import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_AFTER_OPERATORS import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_BEFORE_DOT import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_FOR_EXPRESSION_BODIES -import org.cqfn.diktat.util.FixTestBaseCommon import org.cqfn.diktat.util.assertEquals import com.charleskorn.kaml.Yaml import com.charleskorn.kaml.YamlConfiguration import com.pinterest.ktlint.core.LintError import org.junit.jupiter.api.Assertions -import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Tag import org.junit.jupiter.api.Test import org.junit.jupiter.api.Timeout @@ -57,20 +55,8 @@ typealias RuleToConfig = Map> /** * Base class for smoke test classes - * - * @property unfixedLintErrors */ -abstract class DiktatSmokeTestBase( - private val unfixedLintErrors: MutableList = mutableListOf(), -) : FixTestBaseCommon( - "test/smoke/src/main/kotlin", - { lintError, _ -> unfixedLintErrors.add(lintError) } -) { - /** - * Flag to check LintError in smoke tests - */ - abstract val isLintErrors: Boolean - +abstract class DiktatSmokeTestBase { /** * Disable some of the rules. * @@ -101,11 +87,6 @@ abstract class DiktatSmokeTestBase( } } - @BeforeEach - fun setUp() { - unfixedLintErrors.clear() - } - @Test @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) @@ -170,7 +151,7 @@ abstract class DiktatSmokeTestBase( ) fixAndCompare(configFilePath, "Example5Expected.kt", "Example5Test.kt") - if (isLintErrors) { + assertUnfixedLintErrors { unfixedLintErrors -> Assertions.assertFalse( unfixedLintErrors.contains(LintError(line = 1, col = 1, ruleId = "diktat-ruleset:${CommentsRule.NAME_ID}", detail = "${Warnings.COMMENTED_OUT_CODE.warnText()} /*")) ) @@ -216,7 +197,7 @@ abstract class DiktatSmokeTestBase( ) ) fixAndCompare(configFilePath, "Example2Expected.kt", "Example2Test.kt") - if (isLintErrors) { + assertUnfixedLintErrors { unfixedLintErrors -> unfixedLintErrors.assertEquals( LintError(1, 1, "$DIKTAT_RULE_SET_ID:${HeaderCommentRule.NAME_ID}", "${HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE.warnText()} there are 2 declared classes and/or objects", false), @@ -242,7 +223,7 @@ abstract class DiktatSmokeTestBase( ) ) fixAndCompare(configFilePath, "Example1Expected.kt", "Example1Test.kt") - if (isLintErrors) { + assertUnfixedLintErrors { unfixedLintErrors -> unfixedLintErrors.assertEquals( LintError(1, 1, "$DIKTAT_RULE_SET_ID:${FileNaming.NAME_ID}", "${FILE_NAME_MATCH_CLASS.warnText()} Example1Test.kt vs Example", true), LintError(1, 1, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false), @@ -326,7 +307,7 @@ abstract class DiktatSmokeTestBase( ) // so that trailing newline isn't checked, because it's incorrectly read in tests and we are comparing file with itself // file name is `gradle_` so that IDE doesn't suggest to import gradle project val tmpTestFile = javaClass.classLoader - .getResource("$resourceFilePath/../../../build.gradle_.kts")!! + .getResource("$RESOURCE_FILE_PATH/../../../build.gradle_.kts")!! .toURI() .let { val tmpTestFile = File(it).parentFile.resolve("build.gradle.kts") @@ -334,8 +315,10 @@ abstract class DiktatSmokeTestBase( tmpTestFile } val tmpFilePath = "../../../build.gradle.kts" - fixAndCompare(configFilePath, tmpFilePath, tmpFilePath) - Assertions.assertTrue(unfixedLintErrors.isEmpty()) + fixAndCompare(configFilePath, tmpFilePath, tmpFilePath, false) + assertUnfixedLintErrors { unfixedLintErrors -> + Assertions.assertTrue(unfixedLintErrors.isEmpty()) + } tmpTestFile.delete() } @@ -343,11 +326,15 @@ abstract class DiktatSmokeTestBase( @Tag("DiktatRuleSetProvider") fun `smoke test with gradle script plugin`() { fixAndCompare(prepareOverriddenRulesConfig(), "kotlin-library-expected.gradle.kts", "kotlin-library.gradle.kts") - Assertions.assertEquals( - LintError(2, 1, "$DIKTAT_RULE_SET_ID:${CommentsRule.NAME_ID}", "[COMMENTED_OUT_CODE] you should not comment out code, " + - "use VCS to save it in history and delete this block: import org.jetbrains.kotlin.gradle.dsl.jvm", false), - unfixedLintErrors.single() - ) + assertUnfixedLintErrors { unfixedLintErrors -> + Assertions.assertEquals( + LintError( + 2, 1, "$DIKTAT_RULE_SET_ID:${CommentsRule.NAME_ID}", "[COMMENTED_OUT_CODE] you should not comment out code, " + + "use VCS to save it in history and delete this block: import org.jetbrains.kotlin.gradle.dsl.jvm", false + ), + unfixedLintErrors.single() + ) + } } @Test @@ -363,27 +350,35 @@ abstract class DiktatSmokeTestBase( ) ) fixAndCompare(configFilePath, "Example1-2Expected.kt", "Example1Test.kt") - unfixedLintErrors.assertEquals( - LintError(1, 1, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false), - LintError(3, 1, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_TOP_LEVEL.warnText()} example", false), - LintError(3, 16, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} isValid", false), - LintError(6, 5, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} foo", false), - LintError(6, 5, "$DIKTAT_RULE_SET_ID:${KdocMethods.NAME_ID}", "${MISSING_KDOC_ON_FUNCTION.warnText()} foo", false), - LintError(8, 5, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} foo", false), - LintError(10, 4, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false), - LintError(13, 9, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false), - LintError(18, 40, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false) - ) + assertUnfixedLintErrors { unfixedLintErrors -> + unfixedLintErrors.assertEquals( + LintError(1, 1, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false), + LintError(3, 1, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_TOP_LEVEL.warnText()} example", false), + LintError(3, 16, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} isValid", false), + LintError(6, 5, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} foo", false), + LintError(6, 5, "$DIKTAT_RULE_SET_ID:${KdocMethods.NAME_ID}", "${MISSING_KDOC_ON_FUNCTION.warnText()} foo", false), + LintError(8, 5, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} foo", false), + LintError(10, 4, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false), + LintError(13, 9, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false), + LintError(18, 40, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false) + ) + } } + abstract fun fixAndCompare( config: Path, expected: String, test: String, + trimLastEmptyLine: Boolean = true, ) + abstract fun assertUnfixedLintErrors(lintErrorsConsumer: (List) -> Unit) + companion object { - const val DEFAULT_CONFIG_PATH = "../diktat-analysis.yml" + private const val DEFAULT_CONFIG_PATH = "../diktat-analysis.yml" + const val RESOURCE_FILE_PATH = "test/smoke/src/main/kotlin" private const val TEST_TIMEOUT_SECONDS = 20L } + } diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestComparatorUnit.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestComparatorUnit.kt index ac3e85750c..b6bfe975e2 100644 --- a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestComparatorUnit.kt +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestComparatorUnit.kt @@ -1,5 +1,8 @@ package org.cqfn.diktat.test.framework.processing +import org.cqfn.diktat.test.framework.util.LintErrorCallback +import org.cqfn.diktat.test.framework.util.format +import com.pinterest.ktlint.core.RuleSetProvider import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -22,6 +25,22 @@ import kotlin.io.path.readLines @Suppress("ForbiddenComment", "TYPE_ALIAS") class TestComparatorUnit(private val resourceFilePath: String, private val function: (expectedText: String, testFilePath: String) -> String) { + constructor( + resourceFilePath: String, + ruleSetProviderSupplier: () -> RuleSetProvider, + cb: LintErrorCallback, + ) : this( + resourceFilePath = resourceFilePath, + function = { text, fileName -> + format( + ruleSetProviderRef = ruleSetProviderSupplier, + text = text, + fileName = fileName, + cb = cb + ) + } + ) + /** * @param expectedResult the name of the resource which has the expected * content. The trailing newline, if any, **won't be read** as a separate diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt new file mode 100644 index 0000000000..25c501590d --- /dev/null +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt @@ -0,0 +1,45 @@ +package org.cqfn.diktat.test.framework.util + +import org.cqfn.diktat.common.utils.loggerWithKtlintConfig +import com.pinterest.ktlint.core.KtLint +import com.pinterest.ktlint.core.LintError +import com.pinterest.ktlint.core.RuleSetProvider +import mu.KotlinLogging +import org.intellij.lang.annotations.Language + +@Suppress("EMPTY_BLOCK_STRUCTURE_ERROR") +private val log = KotlinLogging.loggerWithKtlintConfig {} + +@Suppress("TYPE_ALIAS") +internal val defaultCallback: (lintError: LintError, corrected: Boolean) -> Unit = { lintError, _ -> + log.warn("Received linting error: $lintError") +} + +typealias LintErrorCallback = (LintError, Boolean) -> Unit + +/** + * @param ruleSetProviderRef + * @param text + * @param fileName + * @param cb callback to be called on unhandled [LintError]s + * @return formatted code + */ +@Suppress("LAMBDA_IS_NOT_LAST_PARAMETER") +fun format( + ruleSetProviderRef: () -> RuleSetProvider, + @Language("kotlin") text: String, + fileName: String, + cb: LintErrorCallback = defaultCallback +): String { + val ruleSets = listOf(ruleSetProviderRef().get()) + return KtLint.format( + KtLint.ExperimentalParams( + text = text, + ruleSets = ruleSets, + fileName = fileName.removeSuffix("_copy"), + script = fileName.removeSuffix("_copy").endsWith("kts"), + cb = cb, + debug = true, + ) + ) +} From d3319c5d6233a00ca5eac68dc687363cff9762b7 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 27 Oct 2022 13:07:18 +0300 Subject: [PATCH 10/39] removed test jar for diktat-rules --- diktat-rules/pom.xml | 13 ------ .../org/cqfn/diktat/util/FixTestBaseCommon.kt | 21 ++-------- .../kotlin/org/cqfn/diktat/util/TestUtils.kt | 42 ------------------- diktat-ruleset/pom.xml | 7 ---- .../processing/TestComparatorUnit.kt | 31 +++++--------- .../diktat/test/framework/util/TestUtils.kt | 2 +- 6 files changed, 15 insertions(+), 101 deletions(-) diff --git a/diktat-rules/pom.xml b/diktat-rules/pom.xml index b117d8c995..63fcbf0c3b 100644 --- a/diktat-rules/pom.xml +++ b/diktat-rules/pom.xml @@ -259,19 +259,6 @@ - - - org.apache.maven.plugins - maven-jar-plugin - - - test-jar - - test-jar - - - - diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBaseCommon.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBaseCommon.kt index 858eec8163..bac14010a3 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBaseCommon.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBaseCommon.kt @@ -2,11 +2,12 @@ package org.cqfn.diktat.util import org.cqfn.diktat.test.framework.processing.FileComparisonResult import org.cqfn.diktat.test.framework.processing.TestComparatorUnit +import org.cqfn.diktat.test.framework.util.LintErrorCallback +import org.cqfn.diktat.test.framework.util.defaultCallback import com.pinterest.ktlint.core.RuleSetProvider import org.intellij.lang.annotations.Language import org.junit.jupiter.api.Assertions import java.nio.file.Path -import java.nio.file.Paths import kotlin.io.path.bufferedWriter import kotlin.io.path.div @@ -31,14 +32,7 @@ open class FixTestBaseCommon( ruleSetProvider: RuleSetProvider, trimLastEmptyLine: Boolean = false, ) { - val testComparatorUnit = TestComparatorUnit(resourceFilePath) { text, fileName -> - format( - ruleSetProviderRef = { ruleSetProvider }, - text = text, - fileName = fileName, - cb = cb - ) - } + val testComparatorUnit = TestComparatorUnit(resourceFilePath, { ruleSetProvider }, cb) Assertions.assertTrue( testComparatorUnit .compareFilesFromResources(expectedPath, testPath, trimLastEmptyLine) @@ -73,14 +67,7 @@ open class FixTestBaseCommon( out.write(expectedContent) } - val testComparatorUnit = TestComparatorUnit(resourceFilePath) { text, fileName -> - format( - ruleSetProviderRef = { ruleSetProvider }, - text = text, - fileName = fileName, - cb = cb - ) - } + val testComparatorUnit = TestComparatorUnit(resourceFilePath, { ruleSetProvider }, cb) return testComparatorUnit .compareFilesFromFileSystem(expected, actual, false) } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt index 927137fc5a..4504dc60ab 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt @@ -4,35 +4,20 @@ package org.cqfn.diktat.util -import org.cqfn.diktat.common.utils.loggerWithKtlintConfig import org.cqfn.diktat.ruleset.constants.EmitType import com.pinterest.ktlint.core.KtLint -import com.pinterest.ktlint.core.LintError import com.pinterest.ktlint.core.Rule import com.pinterest.ktlint.core.RuleSet -import com.pinterest.ktlint.core.RuleSetProvider import com.pinterest.ktlint.core.api.FeatureInAlphaState -import mu.KotlinLogging import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.fail -import org.intellij.lang.annotations.Language import org.jetbrains.kotlin.com.intellij.lang.ASTNode import java.util.concurrent.atomic.AtomicInteger internal const val TEST_FILE_NAME = "TestFileName.kt" -@Suppress("EMPTY_BLOCK_STRUCTURE_ERROR") -private val log = KotlinLogging.loggerWithKtlintConfig {} - -@Suppress("TYPE_ALIAS") -internal val defaultCallback: (lintError: LintError, corrected: Boolean) -> Unit = { lintError, _ -> - log.warn("Received linting error: $lintError") -} - -typealias LintErrorCallback = (LintError, Boolean) -> Unit - /** * Casts a nullable value to a non-`null` one, similarly to the `!!` * operator. @@ -43,33 +28,6 @@ typealias LintErrorCallback = (LintError, Boolean) -> Unit internal fun T?.assertNotNull(lazyFailureMessage: () -> String = { "Expecting actual not to be null" }): T = this ?: fail(lazyFailureMessage()) -/** - * @param ruleSetProviderRef - * @param text - * @param fileName - * @param cb callback to be called on unhandled [LintError]s - * @return formatted code - */ -@Suppress("LAMBDA_IS_NOT_LAST_PARAMETER") -internal fun format( - ruleSetProviderRef: () -> RuleSetProvider, - @Language("kotlin") text: String, - fileName: String, - cb: LintErrorCallback = defaultCallback -): String { - val ruleSets = listOf(ruleSetProviderRef().get()) - return KtLint.format( - KtLint.ExperimentalParams( - text = text, - ruleSets = ruleSets, - fileName = fileName.removeSuffix("_copy"), - script = fileName.removeSuffix("_copy").endsWith("kts"), - cb = cb, - debug = true, - ) - ) -} - /** * This utility function lets you run arbitrary code on every node of given [code]. * It also provides you with counter which can be incremented inside [applyToNode] and then will be compared to [expectedAsserts]. diff --git a/diktat-ruleset/pom.xml b/diktat-ruleset/pom.xml index dcddd8e8e9..6413e5534b 100644 --- a/diktat-ruleset/pom.xml +++ b/diktat-ruleset/pom.xml @@ -38,13 +38,6 @@ - - org.cqfn.diktat - diktat-rules - ${project.version} - test-jar - test - org.cqfn.diktat diktat-test-framework diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestComparatorUnit.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestComparatorUnit.kt index b6bfe975e2..40fd00bcd8 100644 --- a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestComparatorUnit.kt +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestComparatorUnit.kt @@ -20,27 +20,14 @@ import kotlin.io.path.readLines * * @property resourceFilePath only used when the files are loaded as resources, * via [compareFilesFromResources]. - * @property function a transformation that will be applied to the file + * @property ruleSetProviderSupplier a supplier for [RuleSetProvider] + * @property cb a callback for [com.pinterest.ktlint.core.LintError] */ @Suppress("ForbiddenComment", "TYPE_ALIAS") class TestComparatorUnit(private val resourceFilePath: String, - private val function: (expectedText: String, testFilePath: String) -> String) { - constructor( - resourceFilePath: String, - ruleSetProviderSupplier: () -> RuleSetProvider, - cb: LintErrorCallback, - ) : this( - resourceFilePath = resourceFilePath, - function = { text, fileName -> - format( - ruleSetProviderRef = ruleSetProviderSupplier, - text = text, - fileName = fileName, - cb = cb - ) - } - ) - + private val ruleSetProviderSupplier: () -> RuleSetProvider, + private val cb: LintErrorCallback, +) { /** * @param expectedResult the name of the resource which has the expected * content. The trailing newline, if any, **won't be read** as a separate @@ -105,9 +92,11 @@ class TestComparatorUnit(private val resourceFilePath: String, val copyTestFile = Path("${testFile.absolutePathString()}_copy") testFile.copyTo(copyTestFile, overwrite = true) - val actualResult = function( - readFile(copyTestFile).joinToString("\n"), - copyTestFile.absolutePathString() + val actualResult = format( + ruleSetProviderRef = ruleSetProviderSupplier, + text = readFile(copyTestFile).joinToString("\n"), + fileName = copyTestFile.absolutePathString(), + cb = cb ) val actualFileContent = if (trimLastEmptyLine) { diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt index 25c501590d..e42a76aa7d 100644 --- a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt @@ -11,7 +11,7 @@ import org.intellij.lang.annotations.Language private val log = KotlinLogging.loggerWithKtlintConfig {} @Suppress("TYPE_ALIAS") -internal val defaultCallback: (lintError: LintError, corrected: Boolean) -> Unit = { lintError, _ -> +val defaultCallback: (lintError: LintError, corrected: Boolean) -> Unit = { lintError, _ -> log.warn("Received linting error: $lintError") } From c7c0e2d4ad1146c0eac05cf19976ec1ff774a8e6 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 27 Oct 2022 13:23:54 +0300 Subject: [PATCH 11/39] reverted changes in FixTestBase --- .../org/cqfn/diktat/util/FixTestBase.kt | 45 +++++++++-- .../org/cqfn/diktat/util/FixTestBaseCommon.kt | 74 ------------------- 2 files changed, 40 insertions(+), 79 deletions(-) delete mode 100644 diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBaseCommon.kt 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 18beca6a1c..ab88909ac4 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 @@ -1,19 +1,25 @@ package org.cqfn.diktat.util import org.cqfn.diktat.common.config.rules.RulesConfig +import org.cqfn.diktat.test.framework.processing.FileComparisonResult +import org.cqfn.diktat.test.framework.processing.TestComparatorUnit +import org.cqfn.diktat.test.framework.util.LintErrorCallback +import org.cqfn.diktat.test.framework.util.defaultCallback import com.pinterest.ktlint.core.Rule import org.intellij.lang.annotations.Language +import org.junit.jupiter.api.Assertions import java.nio.file.Path +import kotlin.io.path.bufferedWriter +import kotlin.io.path.div /** * @property resourceFilePath path to files which will be compared in tests */ open class FixTestBase( - resourceFilePath: String, + private val resourceFilePath: String, ruleSupplier: (rulesConfigList: List) -> Rule, private val defaultRulesConfigList: List? = null, -) : FixTestBaseCommon( - resourceFilePath = resourceFilePath, + private val cb: LintErrorCallback = defaultCallback, ) { private val ruleSetProviderRef = { rulesConfigList: List? -> DiktatRuleSetProvider4Test(ruleSupplier, rulesConfigList ?: defaultRulesConfigList) } @@ -30,7 +36,18 @@ open class FixTestBase( testPath: String, overrideRulesConfigList: List? = null, trimLastEmptyLine: Boolean = false, - ) = super.fixAndCompare(expectedPath, testPath, ruleSetProviderRef(overrideRulesConfigList), trimLastEmptyLine) + ) { + val testComparatorUnit = TestComparatorUnit( + resourceFilePath = resourceFilePath, + ruleSetProviderSupplier = { ruleSetProviderRef(overrideRulesConfigList) }, + cb = cb + ) + Assertions.assertTrue( + testComparatorUnit + .compareFilesFromResources(expectedPath, testPath, trimLastEmptyLine) + .isSuccessful + ) + } /** * Unlike [fixAndCompare], this method doesn't perform any assertions. @@ -51,5 +68,23 @@ open class FixTestBase( @Language("kotlin") expectedContent: String = actualContent, tempDir: Path, overrideRulesConfigList: List? = null - ) = super.fixAndCompareContent(actualContent, expectedContent, tempDir, ruleSetProviderRef(overrideRulesConfigList)) + ): FileComparisonResult { + val actual = tempDir / "actual.kt" + actual.bufferedWriter().use { out -> + out.write(actualContent) + } + + val expected = tempDir / "expected.kt" + expected.bufferedWriter().use { out -> + out.write(expectedContent) + } + + val testComparatorUnit = TestComparatorUnit( + resourceFilePath = resourceFilePath, + ruleSetProviderSupplier = { ruleSetProviderRef(overrideRulesConfigList) }, + cb = cb + ) + return testComparatorUnit + .compareFilesFromFileSystem(expected, actual, false) + } } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBaseCommon.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBaseCommon.kt deleted file mode 100644 index bac14010a3..0000000000 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBaseCommon.kt +++ /dev/null @@ -1,74 +0,0 @@ -package org.cqfn.diktat.util - -import org.cqfn.diktat.test.framework.processing.FileComparisonResult -import org.cqfn.diktat.test.framework.processing.TestComparatorUnit -import org.cqfn.diktat.test.framework.util.LintErrorCallback -import org.cqfn.diktat.test.framework.util.defaultCallback -import com.pinterest.ktlint.core.RuleSetProvider -import org.intellij.lang.annotations.Language -import org.junit.jupiter.api.Assertions -import java.nio.file.Path -import kotlin.io.path.bufferedWriter -import kotlin.io.path.div - -/** - * @property resourceFilePath path to files which will be compared in tests - */ -open class FixTestBaseCommon( - protected val resourceFilePath: String, - private val cb: LintErrorCallback = defaultCallback, -) { - /** - * @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 ruleSetProvider provider for rules which should be applied - * @param trimLastEmptyLine whether the last (empty) line should be - * discarded when reading the content of [testPath]. - * @see fixAndCompareContent - */ - protected fun fixAndCompare( - expectedPath: String, - testPath: String, - ruleSetProvider: RuleSetProvider, - trimLastEmptyLine: Boolean = false, - ) { - val testComparatorUnit = TestComparatorUnit(resourceFilePath, { ruleSetProvider }, cb) - Assertions.assertTrue( - testComparatorUnit - .compareFilesFromResources(expectedPath, testPath, trimLastEmptyLine) - .isSuccessful - ) - } - - /** - * @param actualContent the original file content (may well be modified as - * fixes are applied). - * @param expectedContent the content the file is expected to have after the - * fixes are applied. - * @param tempDir the temporary directory (usually injected by _JUnit_). - * @param ruleSetProvider provider for rules which should be applied - * @return the result of file content comparison. - * @see fixAndCompare - */ - @Suppress("FUNCTION_BOOLEAN_PREFIX") - protected fun fixAndCompareContent( - @Language("kotlin") actualContent: String, - @Language("kotlin") expectedContent: String = actualContent, - tempDir: Path, - ruleSetProvider: RuleSetProvider, - ): FileComparisonResult { - val actual = tempDir / "actual.kt" - actual.bufferedWriter().use { out -> - out.write(actualContent) - } - - val expected = tempDir / "expected.kt" - expected.bufferedWriter().use { out -> - out.write(expectedContent) - } - - val testComparatorUnit = TestComparatorUnit(resourceFilePath, { ruleSetProvider }, cb) - return testComparatorUnit - .compareFilesFromFileSystem(expected, actual, false) - } -} From a26f39ae770c652743ccaecaf4c0a3257ecd7610 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 27 Oct 2022 13:32:57 +0300 Subject: [PATCH 12/39] refactored FixTestBase --- .../org/cqfn/diktat/util/FixTestBase.kt | 31 ++++++++++--------- .../diktat/test/framework/util/TestUtils.kt | 4 +++ 2 files changed, 20 insertions(+), 15 deletions(-) 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 ab88909ac4..9e8426c0d6 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 @@ -13,15 +13,24 @@ import kotlin.io.path.bufferedWriter import kotlin.io.path.div /** - * @property resourceFilePath path to files which will be compared in tests + * Base class for FixTest */ open class FixTestBase( - private val resourceFilePath: String, + resourceFilePath: String, ruleSupplier: (rulesConfigList: List) -> Rule, - private val defaultRulesConfigList: List? = null, - private val cb: LintErrorCallback = defaultCallback, + defaultRulesConfigList: List? = null, + cb: LintErrorCallback = defaultCallback, ) { - private val ruleSetProviderRef = { rulesConfigList: List? -> DiktatRuleSetProvider4Test(ruleSupplier, rulesConfigList ?: defaultRulesConfigList) } + /** + * testComparatorUnit + */ + private val testComparatorUnitSupplier = { overrideRulesConfigList: List? -> + TestComparatorUnit( + resourceFilePath = resourceFilePath, + ruleSetProviderSupplier = { DiktatRuleSetProvider4Test(ruleSupplier, overrideRulesConfigList ?: defaultRulesConfigList) }, + cb = cb + ) + } /** * @param expectedPath path to file with expected result, relative to [resourceFilePath] @@ -37,11 +46,7 @@ open class FixTestBase( overrideRulesConfigList: List? = null, trimLastEmptyLine: Boolean = false, ) { - val testComparatorUnit = TestComparatorUnit( - resourceFilePath = resourceFilePath, - ruleSetProviderSupplier = { ruleSetProviderRef(overrideRulesConfigList) }, - cb = cb - ) + val testComparatorUnit = testComparatorUnitSupplier(overrideRulesConfigList) Assertions.assertTrue( testComparatorUnit .compareFilesFromResources(expectedPath, testPath, trimLastEmptyLine) @@ -79,11 +84,7 @@ open class FixTestBase( out.write(expectedContent) } - val testComparatorUnit = TestComparatorUnit( - resourceFilePath = resourceFilePath, - ruleSetProviderSupplier = { ruleSetProviderRef(overrideRulesConfigList) }, - cb = cb - ) + val testComparatorUnit = testComparatorUnitSupplier(overrideRulesConfigList) return testComparatorUnit .compareFilesFromFileSystem(expected, actual, false) } diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt index e42a76aa7d..ae94fb5e23 100644 --- a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt @@ -1,3 +1,7 @@ +/** + * Utility classes and methods for tests + */ + package org.cqfn.diktat.test.framework.util import org.cqfn.diktat.common.utils.loggerWithKtlintConfig From 0cb6c92780371f2cf3a1cbb31b2913be2fbc13e5 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 27 Oct 2022 13:41:50 +0300 Subject: [PATCH 13/39] diktatFix --- .../src/test/kotlin/org/cqfn/diktat/util/FixTestBase.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 9e8426c0d6..b1556703fc 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 @@ -45,7 +45,7 @@ open class FixTestBase( testPath: String, overrideRulesConfigList: List? = null, trimLastEmptyLine: Boolean = false, - ) { + ) { val testComparatorUnit = testComparatorUnitSupplier(overrideRulesConfigList) Assertions.assertTrue( testComparatorUnit From d994cf1faddfc9b0c351ccb54140f89f19ebbbef Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 27 Oct 2022 13:46:24 +0300 Subject: [PATCH 14/39] diktatFix #2 --- .../kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt index aafbf9e14e..abed684061 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt @@ -365,7 +365,6 @@ abstract class DiktatSmokeTestBase { } } - abstract fun fixAndCompare( config: Path, expected: String, @@ -380,5 +379,4 @@ abstract class DiktatSmokeTestBase { const val RESOURCE_FILE_PATH = "test/smoke/src/main/kotlin" private const val TEST_TIMEOUT_SECONDS = 20L } - } From 1e73aa050db95eb6a0874f5f5aba25b225f278c2 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 27 Oct 2022 14:33:19 +0300 Subject: [PATCH 15/39] added logging in save-cli for windows too --- .../kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt index cfd1f031b4..6c05b9b4ed 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt @@ -112,7 +112,7 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { val result = when { systemName.startsWith("Linux", ignoreCase = true) || systemName.startsWith("Mac", ignoreCase = true) -> ProcessBuilder("sh", "-c", "chmod 777 $savePath ; ./$savePath $filesDir/src/main/kotlin $testPath --log all") - else -> ProcessBuilder(savePath, "$filesDir/src/main/kotlin", testPath) + else -> ProcessBuilder(savePath, "--log", "all", "$filesDir/src/main/kotlin", testPath) } return result } From 6e8e64a8c338d160141b64c784a021c258e8bf9d Mon Sep 17 00:00:00 2001 From: Alexander Frolov Date: Thu, 27 Oct 2022 14:29:50 +0300 Subject: [PATCH 16/39] Bugfix for fix mode of `AVOID_NULL_CHECK` (#1535) * Bugfix for fix mode of AVOID_NULL_CHECK * Added a check for assignment operator when deciding if to put run block or not * Added tests (#1293) --- .../ruleset/rules/chapter4/NullChecksRule.kt | 43 +++++++++++++------ .../IfConditionNullCheckExpected.kt | 22 ++++++++++ .../null_checks/IfConditionNullCheckTest.kt | 28 ++++++++++++ .../ruleset/smoke/DiktatSmokeTestBase.kt | 2 +- 4 files changed, 81 insertions(+), 14 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt index 8a2409c8bb..03554fee1d 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt @@ -24,6 +24,7 @@ import org.jetbrains.kotlin.com.intellij.psi.tree.IElementType import org.jetbrains.kotlin.psi.KtBinaryExpression import org.jetbrains.kotlin.psi.KtBlockExpression import org.jetbrains.kotlin.psi.KtIfExpression +import org.jetbrains.kotlin.psi.KtPsiUtil import org.jetbrains.kotlin.psi.psiUtil.blockExpressionsOrSingle /** @@ -119,9 +120,10 @@ class NullChecksRule(configRules: List) : DiktatRule( } @Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION") - private fun fixNullInIfCondition(condition: ASTNode, - binaryExpression: KtBinaryExpression, - isEqualToNull: Boolean + private fun fixNullInIfCondition( + condition: ASTNode, + binaryExpression: KtBinaryExpression, + isEqualToNull: Boolean ) { val variableName = binaryExpression.left!!.text val thenFromExistingCode = condition.extractLinesFromBlock(THEN) @@ -138,26 +140,41 @@ class NullChecksRule(configRules: List) : DiktatRule( } else { elseFromExistingCode } - val numberOfStatementsInElseBlock = if (isEqualToNull) { - (condition.treeParent.psi as KtIfExpression).then?.blockExpressionsOrSingle()?.count() ?: 0 - } else { - (condition.treeParent.psi as KtIfExpression).`else`?.blockExpressionsOrSingle()?.count() ?: 0 - } - val elseEditedCodeLines = getEditedElseCodeLines(elseCodeLines, numberOfStatementsInElseBlock) + val (numberOfStatementsInElseBlock, isAssignmentInNewElseBlock) = (condition.treeParent.psi as KtIfExpression) + .let { + if (isEqualToNull) { + it.then + } else { + it.`else` + } + } + ?.blockExpressionsOrSingle() + ?.let { elements -> + elements.count() to elements.any { element -> + KtPsiUtil.isAssignment(element) + } + } + ?: Pair(0, false) + + val elseEditedCodeLines = getEditedElseCodeLines(elseCodeLines, numberOfStatementsInElseBlock, isAssignmentInNewElseBlock) val thenEditedCodeLines = getEditedThenCodeLines(variableName, thenCodeLines, elseEditedCodeLines) val text = "$thenEditedCodeLines $elseEditedCodeLines" val tree = KotlinParser().createNode(text) - condition.treeParent.treeParent.addChild(tree, condition.treeParent) - condition.treeParent.treeParent.removeChild(condition.treeParent) + val ifNode = condition.treeParent + ifNode.treeParent.replaceChild(ifNode, tree) } - private fun getEditedElseCodeLines(elseCodeLines: List?, numberOfStatementsInElseBlock: Int): String = when { + private fun getEditedElseCodeLines( + elseCodeLines: List?, + numberOfStatementsInElseBlock: Int, + isAssignment: Boolean, + ): String = when { // else { "null"/empty } -> "" elseCodeLines == null || elseCodeLines.singleOrNull() == "null" -> "" // else { bar() } -> ?: bar() - numberOfStatementsInElseBlock == 1 -> "?: ${elseCodeLines.joinToString(postfix = "\n", separator = "\n")}" + numberOfStatementsInElseBlock == 1 && !isAssignment -> "?: ${elseCodeLines.joinToString(postfix = "\n", separator = "\n")}" // else { ... } -> ?: run { ... } else -> getDefaultCaseElseCodeLines(elseCodeLines) } diff --git a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt index 47af964fc1..dc816f349c 100644 --- a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt @@ -79,3 +79,25 @@ foo() } ?: boo() } +fun nullCheckWithAssumption() { + val a: Int? = 5 + a?.let { +foo() +} ?: run { +a = 5 +} + a?.let { +foo() +} ?: run { +a = 5 +} + a?.let { +a = 5 +} ?: foo() + a?.let { +a = 5 +} ?: foo() + a?.let { +foo() +} ?: a == 5 +} diff --git a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt index 135c389548..08e6d8e319 100644 --- a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt @@ -111,3 +111,31 @@ fun reversedCheckSmartCases() { } } +fun nullCheckWithAssumption() { + val a: Int? = 5 + if (a != null) { + foo() + } else { + a = 5 + } + if (a == null) { + a = 5 + } else { + foo() + } + if (a != null) { + a = 5 + } else { + foo() + } + if (a == null) { + foo() + } else { + a = 5 + } + if (a != null) { + foo() + } else { + a == 5 + } +} diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt index abed684061..3608c2c3ea 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt @@ -377,6 +377,6 @@ abstract class DiktatSmokeTestBase { companion object { private const val DEFAULT_CONFIG_PATH = "../diktat-analysis.yml" const val RESOURCE_FILE_PATH = "test/smoke/src/main/kotlin" - private const val TEST_TIMEOUT_SECONDS = 20L + private const val TEST_TIMEOUT_SECONDS = 25L } } From 52572873a03e0daebf677fd2eb53fe001eaf9c85 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 27 Oct 2022 19:31:50 +0300 Subject: [PATCH 17/39] WIP --- diktat-rules/pom.xml | 61 -------------- diktat-ruleset/pom.xml | 80 +++++++++++++++++++ .../ruleset/smoke/DiktatSaveSmokeTest.kt | 1 + 3 files changed, 81 insertions(+), 61 deletions(-) diff --git a/diktat-rules/pom.xml b/diktat-rules/pom.xml index 63fcbf0c3b..ed551f7991 100644 --- a/diktat-rules/pom.xml +++ b/diktat-rules/pom.xml @@ -198,67 +198,6 @@ - - - org.apache.maven.plugins - maven-failsafe-plugin - - - **/DiktatSaveSmokeTest.* - - - - - - integration-test - verify - - - - - - - org.jacoco - jacoco-maven-plugin - - - - default-prepare-agent-integration - - prepare-agent-integration - - - - default-merge - post-integration-test - - merge - - - - - ${project.build.directory} - - jacoco.exec - jacoco-it.exec - - - - ${project.build.directory}/jacoco-merged.exec - - - - default-report-merged - - report-integration - - - ${project.reporting.outputDirectory}/jacoco - ${project.build.directory}/jacoco-merged.exec - - - - diff --git a/diktat-ruleset/pom.xml b/diktat-ruleset/pom.xml index 6413e5534b..f434098efb 100644 --- a/diktat-ruleset/pom.xml +++ b/diktat-ruleset/pom.xml @@ -102,7 +102,30 @@ + ${project.basedir}/src/test/kotlin + + org.jetbrains.kotlin + kotlin-maven-plugin + + + test-compile + process-test-sources + + test-compile + + + + + src/main/kotlin + src/test/kotlin + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/test/kotlin + + + + + maven-jar-plugin @@ -139,6 +162,63 @@ + + + org.apache.maven.plugins + maven-failsafe-plugin + + + + integration-test + verify + + integration-test + + + + + + org.jacoco + jacoco-maven-plugin + + + + default-prepare-agent-integration + + prepare-agent-integration + + + + default-merge + post-integration-test + + merge + + + + + ${project.build.directory} + + jacoco.exec + jacoco-it.exec + + + + ${project.build.directory}/jacoco-merged.exec + + + + default-report-merged + + report-integration + + + ${project.reporting.outputDirectory}/jacoco + ${project.build.directory}/jacoco-merged.exec + + + + diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt index 6c05b9b4ed..bff0d52c08 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt @@ -87,6 +87,7 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { softly.assertThat(saveLog).isRegularFile val saveOutput = saveLog.readText() + println(saveOutput) val saveCommandLine = processBuilder.command().joinToString(separator = " ") softly.assertThat(saveOutput) From 0b96349443b5c1a1ee40337e7d9e5bca89fda1cb Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 28 Oct 2022 12:04:34 +0300 Subject: [PATCH 18/39] removed debug logging --- .../kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt index bff0d52c08..6c05b9b4ed 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt @@ -87,7 +87,6 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { softly.assertThat(saveLog).isRegularFile val saveOutput = saveLog.readText() - println(saveOutput) val saveCommandLine = processBuilder.command().joinToString(separator = " ") softly.assertThat(saveOutput) From 49b7d84d7412308ce8ef3b3dd63e504f5007243c Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 28 Oct 2022 12:16:15 +0300 Subject: [PATCH 19/39] moved test run in maven --- diktat-rules/pom.xml | 32 -------------------------------- diktat-ruleset/pom.xml | 10 ++++++++++ pom.xml | 29 +++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 32 deletions(-) diff --git a/diktat-rules/pom.xml b/diktat-rules/pom.xml index ed551f7991..376b963aa1 100644 --- a/diktat-rules/pom.xml +++ b/diktat-rules/pom.xml @@ -165,38 +165,6 @@ org.apache.maven.plugins maven-surefire-plugin - - - me.fabriciorby - maven-surefire-junit5-tree-reporter - 1.1.0 - - - - - **/DiktatSaveSmokeTest.* - - - - true - true - true - true - - - plain - - true - - - - true - true - - false - - diff --git a/diktat-ruleset/pom.xml b/diktat-ruleset/pom.xml index f434098efb..ceb28e8d14 100644 --- a/diktat-ruleset/pom.xml +++ b/diktat-ruleset/pom.xml @@ -163,6 +163,16 @@ + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/DiktatSaveSmokeTest.* + + + org.apache.maven.plugins maven-failsafe-plugin diff --git a/pom.xml b/pom.xml index facd190290..aadf11b963 100644 --- a/pom.xml +++ b/pom.xml @@ -273,6 +273,35 @@ org.apache.maven.plugins maven-surefire-plugin 3.0.0-M7 + + + me.fabriciorby + maven-surefire-junit5-tree-reporter + 1.1.0 + + + + + + true + true + true + true + + + plain + + true + + + + true + true + + false + + org.apache.maven.plugins From 0be5071ae7c447fe8e44cd40cab524607f14eecd Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 28 Oct 2022 14:23:40 +0300 Subject: [PATCH 20/39] added **/DiktatSaveSmokeTest.* as include to failsafe --- diktat-ruleset/pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/diktat-ruleset/pom.xml b/diktat-ruleset/pom.xml index ceb28e8d14..9edd86b9b9 100644 --- a/diktat-ruleset/pom.xml +++ b/diktat-ruleset/pom.xml @@ -176,6 +176,11 @@ org.apache.maven.plugins maven-failsafe-plugin + + + **/DiktatSaveSmokeTest.* + + From 61da8b2aa477cedbdbb931e531882c174293b7f5 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 28 Oct 2022 17:51:35 +0300 Subject: [PATCH 21/39] fixed file name for save-cli tests --- .../ruleset/smoke/DiktatSmokeTestBase.kt | 44 ++++++++++++++----- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt index 3608c2c3ea..768e5d5b20 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt @@ -33,11 +33,14 @@ import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXT import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_BEFORE_DOT import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_FOR_EXPRESSION_BODIES import org.cqfn.diktat.util.assertEquals +import org.cqfn.diktat.util.deleteIfExistsSilently import com.charleskorn.kaml.Yaml import com.charleskorn.kaml.YamlConfiguration import com.pinterest.ktlint.core.LintError +import org.junit.jupiter.api.AfterAll import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Tag import org.junit.jupiter.api.Test import org.junit.jupiter.api.Timeout @@ -306,20 +309,11 @@ abstract class DiktatSmokeTestBase { ) ) // so that trailing newline isn't checked, because it's incorrectly read in tests and we are comparing file with itself // file name is `gradle_` so that IDE doesn't suggest to import gradle project - val tmpTestFile = javaClass.classLoader - .getResource("$RESOURCE_FILE_PATH/../../../build.gradle_.kts")!! - .toURI() - .let { - val tmpTestFile = File(it).parentFile.resolve("build.gradle.kts") - File(it).copyTo(tmpTestFile, true) - tmpTestFile - } val tmpFilePath = "../../../build.gradle.kts" fixAndCompare(configFilePath, tmpFilePath, tmpFilePath, false) assertUnfixedLintErrors { unfixedLintErrors -> Assertions.assertTrue(unfixedLintErrors.isEmpty()) } - tmpTestFile.delete() } @Test @@ -349,7 +343,7 @@ abstract class DiktatSmokeTestBase { ) ) ) - fixAndCompare(configFilePath, "Example1-2Expected.kt", "Example1Test.kt") + fixAndCompare(configFilePath, "Example1-2Expected.kt", "Example1-2Test.kt") assertUnfixedLintErrors { unfixedLintErrors -> unfixedLintErrors.assertEquals( LintError(1, 1, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false), @@ -378,5 +372,35 @@ abstract class DiktatSmokeTestBase { private const val DEFAULT_CONFIG_PATH = "../diktat-analysis.yml" const val RESOURCE_FILE_PATH = "test/smoke/src/main/kotlin" private const val TEST_TIMEOUT_SECONDS = 25L + private val tmpFiles: MutableList = mutableListOf() + + @BeforeAll + @JvmStatic + @Suppress("AVOID_NULL_CHECKS") + internal fun createTmpFiles() { + listOf( + "$RESOURCE_FILE_PATH/../../../build.gradle_.kts" to "build.gradle.kts", + "$RESOURCE_FILE_PATH/Example1Test.kt" to "Example1-2Test.kt", + ) + .map { (resource, targetFileName) -> + DiktatSmokeTestBase::class.java + .classLoader + .getResource(resource)!! + .toURI() + .let { + val tmpTestFile = File(it).parentFile.resolve(targetFileName) + File(it).copyTo(tmpTestFile, true) + } + .let { tmpFiles.add(it) } + } + } + + @AfterAll + @JvmStatic + internal fun deleteTmpFiles() { + tmpFiles.forEach { + it.toPath().deleteIfExistsSilently() + } + } } } From 3c3c70624c3586971ea138dcb18f52a85841dc54 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 28 Oct 2022 18:07:18 +0300 Subject: [PATCH 22/39] timeout for save-cli smoke tests --- .../org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt | 5 +++-- .../org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt index 6c05b9b4ed..e7a73b8153 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt @@ -18,6 +18,7 @@ import org.junit.jupiter.api.condition.OS import java.net.URL import java.nio.file.Path +import java.util.concurrent.TimeUnit import kotlin.io.path.Path import kotlin.io.path.absolute import kotlin.io.path.copyTo @@ -81,8 +82,8 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { } val saveProcess = processBuilder.start() - val saveExitCode = saveProcess.waitFor() - softly.assertThat(saveExitCode).describedAs("The exit code of SAVE").isZero + val saveProcessResult = saveProcess.waitFor(TEST_TIMEOUT_SECONDS, TimeUnit.SECONDS) + softly.assertThat(saveProcessResult).describedAs("The exit code of SAVE").isTrue softly.assertThat(saveLog).isRegularFile diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt index 768e5d5b20..34d70969ee 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt @@ -371,7 +371,7 @@ abstract class DiktatSmokeTestBase { companion object { private const val DEFAULT_CONFIG_PATH = "../diktat-analysis.yml" const val RESOURCE_FILE_PATH = "test/smoke/src/main/kotlin" - private const val TEST_TIMEOUT_SECONDS = 25L + const val TEST_TIMEOUT_SECONDS = 25L private val tmpFiles: MutableList = mutableListOf() @BeforeAll From 3883dbe5ecbbd236a285ceac469be73ceb3046b5 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 28 Oct 2022 18:18:52 +0300 Subject: [PATCH 23/39] added tree reporter for failsafe too --- pom.xml | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index aadf11b963..08d77d5a26 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,7 @@ 0.46.1 5.9.0 1.9.0 + 1.1.0 31.1-jre 1.7.36 1.5.0 @@ -277,7 +278,7 @@ me.fabriciorby maven-surefire-junit5-tree-reporter - 1.1.0 + ${surefire.junit5.tree-reporter.version} @@ -312,6 +313,35 @@ org.apache.maven.plugins maven-failsafe-plugin 3.0.0-M7 + + + me.fabriciorby + maven-surefire-junit5-tree-reporter + ${surefire.junit5.tree-reporter.version} + + + + + + true + true + true + true + + + plain + + true + + + + true + true + + false + + org.codehaus.mojo From 82df88b144ce5b299b675addafc19ab7166becc5 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 28 Oct 2022 18:33:13 +0300 Subject: [PATCH 24/39] set big timeout for save-cli process --- .../org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt | 3 ++- .../org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt index e7a73b8153..3f81d62ae7 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt @@ -82,7 +82,8 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { } val saveProcess = processBuilder.start() - val saveProcessResult = saveProcess.waitFor(TEST_TIMEOUT_SECONDS, TimeUnit.SECONDS) + // set some big timeout to wait more that JUnit can be configured to wait + val saveProcessResult = saveProcess.waitFor(1, TimeUnit.HOURS) softly.assertThat(saveProcessResult).describedAs("The exit code of SAVE").isTrue softly.assertThat(saveLog).isRegularFile diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt index 34d70969ee..768e5d5b20 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt @@ -371,7 +371,7 @@ abstract class DiktatSmokeTestBase { companion object { private const val DEFAULT_CONFIG_PATH = "../diktat-analysis.yml" const val RESOURCE_FILE_PATH = "test/smoke/src/main/kotlin" - const val TEST_TIMEOUT_SECONDS = 25L + private const val TEST_TIMEOUT_SECONDS = 25L private val tmpFiles: MutableList = mutableListOf() @BeforeAll From 845b327a7854e92715b1e04ae75acf255331faba Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 28 Oct 2022 18:49:20 +0300 Subject: [PATCH 25/39] reverted to waitFor for further investigation --- .../org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt index 3f81d62ae7..6c05b9b4ed 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt @@ -18,7 +18,6 @@ import org.junit.jupiter.api.condition.OS import java.net.URL import java.nio.file.Path -import java.util.concurrent.TimeUnit import kotlin.io.path.Path import kotlin.io.path.absolute import kotlin.io.path.copyTo @@ -82,9 +81,8 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { } val saveProcess = processBuilder.start() - // set some big timeout to wait more that JUnit can be configured to wait - val saveProcessResult = saveProcess.waitFor(1, TimeUnit.HOURS) - softly.assertThat(saveProcessResult).describedAs("The exit code of SAVE").isTrue + val saveExitCode = saveProcess.waitFor() + softly.assertThat(saveExitCode).describedAs("The exit code of SAVE").isZero softly.assertThat(saveLog).isRegularFile From 92b7330860a9c557441a728d4982767915f1582d Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 28 Oct 2022 21:24:15 +0300 Subject: [PATCH 26/39] set maven.compiler.release only for JDK 11 and more --- diktat-rules/pom.xml | 5 ----- pom.xml | 10 +++++++++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/diktat-rules/pom.xml b/diktat-rules/pom.xml index 376b963aa1..f9a42ec34e 100644 --- a/diktat-rules/pom.xml +++ b/diktat-rules/pom.xml @@ -12,11 +12,6 @@ 1.2.4-SNAPSHOT - - 1.8 - 1.8 - - diff --git a/pom.xml b/pom.xml index 08d77d5a26..18963e1d32 100644 --- a/pom.xml +++ b/pom.xml @@ -41,7 +41,6 @@ 1.8 1.8 - 8 UTF-8 1.7.20 true @@ -603,5 +602,14 @@ + + jdk11 + + [11, + + + 8 + + From cab2eedc61dfb7d1faa3e436635281d226009be3 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Sat, 29 Oct 2022 00:41:55 +0300 Subject: [PATCH 27/39] fixed after merge resolving --- diktat-maven-plugin/pom.xml | 56 ------- .../cqfn/diktat/ruleset/utils/KtlintUtils.kt | 47 +++++- .../smoke/RulesConfigValidationTest.kt | 2 +- .../org/cqfn/diktat/util/FixTestBase.kt | 15 +- .../ruleset/smoke/DiktatSaveSmokeTest.kt | 8 +- .../diktat/ruleset/smoke/DiktatSmokeTest.kt | 11 +- .../ruleset/smoke/DiktatSmokeTestBase.kt | 10 +- .../org/cqfn/diktat/util/SmokeTestUtils.kt | 69 --------- .../processing/TestComparatorUnit.kt | 20 +-- .../diktat/test/framework/util/TestUtils.kt | 141 ++++++++++++++---- pom.xml | 1 - 11 files changed, 192 insertions(+), 188 deletions(-) diff --git a/diktat-maven-plugin/pom.xml b/diktat-maven-plugin/pom.xml index 1172a07260..8f296767d5 100644 --- a/diktat-maven-plugin/pom.xml +++ b/diktat-maven-plugin/pom.xml @@ -142,37 +142,10 @@ org.apache.maven.plugins maven-surefire-plugin - - - me.fabriciorby - maven-surefire-junit5-tree-reporter - ${junit-tree-reporter.version} - - *IntegrationTest* - - - true - true - true - true - - - plain - - true - - - - true - true - - false - @@ -192,13 +165,6 @@ org.apache.maven.plugins maven-failsafe-plugin - - - me.fabriciorby - maven-surefire-junit5-tree-reporter - ${junit-tree-reporter.version} - - ${maven.version} @@ -207,28 +173,6 @@ **/*IntegrationTest* - ${project.build.testSourceDirectory} - ${project.build.testOutputDirectory} - - - true - true - true - true - - - plain - - true - - - - true - true - - false - diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/KtlintUtils.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/KtlintUtils.kt index 3c2fc353f4..63ac4d8a3e 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/KtlintUtils.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/KtlintUtils.kt @@ -1,10 +1,27 @@ -@file:Suppress("HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE") +@file:Suppress( + "HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE", + "Deprecation", +) package org.cqfn.diktat.ruleset.utils +import org.cqfn.diktat.common.utils.loggerWithKtlintConfig import com.pinterest.ktlint.core.KtLint import com.pinterest.ktlint.core.KtLint.ExperimentalParams import com.pinterest.ktlint.core.LintError +import com.pinterest.ktlint.core.RuleSetProvider +import mu.KotlinLogging +import org.intellij.lang.annotations.Language + +@Suppress("EMPTY_BLOCK_STRUCTURE_ERROR") +private val log = KotlinLogging.loggerWithKtlintConfig {} + +@Suppress("TYPE_ALIAS") +val defaultCallback: (lintError: LintError, corrected: Boolean) -> Unit = { lintError, _ -> + log.warn("Received linting error: $lintError") +} + +typealias LintErrorCallback = (LintError, Boolean) -> Unit /** * Enables ignoring autocorrected errors when in "fix" mode (i.e. when @@ -27,3 +44,31 @@ fun ExperimentalParams.ignoreCorrectedErrors(): ExperimentalParams = cb(error, false) } }) + + +/** + * @param ruleSetProviderRef + * @param text + * @param fileName + * @param cb callback to be called on unhandled [LintError]s + * @return formatted code + */ +@Suppress("LAMBDA_IS_NOT_LAST_PARAMETER") +fun format( + ruleSetProviderRef: () -> RuleSetProvider, + @Language("kotlin") text: String, + fileName: String, + cb: LintErrorCallback = defaultCallback +): String { + val ruleSets = listOf(ruleSetProviderRef().get()) + return KtLint.format( + ExperimentalParams( + text = text, + ruleSets = ruleSets, + fileName = fileName.removeSuffix("_copy"), + script = fileName.removeSuffix("_copy").endsWith("kts"), + cb = cb, + debug = true, + ).ignoreCorrectedErrors() + ) +} diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/RulesConfigValidationTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/RulesConfigValidationTest.kt index 586eaf20d6..07e2135a97 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/RulesConfigValidationTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/RulesConfigValidationTest.kt @@ -1,7 +1,7 @@ package org.cqfn.diktat.ruleset.smoke import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider -import org.cqfn.diktat.util.deleteIfExistsSilently +import org.cqfn.diktat.test.framework.util.deleteIfExistsSilently import com.charleskorn.kaml.InvalidPropertyValueException import org.junit.jupiter.api.AfterEach 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 40c21b12c9..a3950e109f 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,11 @@ package org.cqfn.diktat.util import org.cqfn.diktat.common.config.rules.RulesConfig +import org.cqfn.diktat.ruleset.utils.LintErrorCallback +import org.cqfn.diktat.ruleset.utils.defaultCallback +import org.cqfn.diktat.ruleset.utils.format import org.cqfn.diktat.test.framework.processing.FileComparisonResult import org.cqfn.diktat.test.framework.processing.TestComparatorUnit -import org.cqfn.diktat.test.framework.util.LintErrorCallback -import org.cqfn.diktat.test.framework.util.defaultCallback import com.pinterest.ktlint.core.Rule import org.intellij.lang.annotations.Language import org.junit.jupiter.api.Assertions @@ -31,8 +32,14 @@ open class FixTestBase( private val testComparatorUnitSupplier = { overrideRulesConfigList: List? -> TestComparatorUnit( resourceFilePath = resourceFilePath, - ruleSetProviderSupplier = { DiktatRuleSetProvider4Test(ruleSupplier, overrideRulesConfigList ?: defaultRulesConfigList) }, - cb = cb + function = { expectedText, testFilePath -> + format( + ruleSetProviderRef = { DiktatRuleSetProvider4Test(ruleSupplier, overrideRulesConfigList ?: defaultRulesConfigList) }, + text = expectedText, + fileName = testFilePath, + cb = cb, + ) + }, ) } diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt index 79701e9bd0..8e4d6cce19 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt @@ -1,11 +1,11 @@ package org.cqfn.diktat.ruleset.smoke import org.cqfn.diktat.common.utils.loggerWithKtlintConfig -import org.cqfn.diktat.util.deleteIfExistsRecursively -import org.cqfn.diktat.util.deleteIfExistsSilently +import org.cqfn.diktat.test.framework.util.deleteIfExistsRecursively +import org.cqfn.diktat.test.framework.util.deleteIfExistsSilently +import org.cqfn.diktat.test.framework.util.retry import org.cqfn.diktat.util.isSameJavaHomeAs import org.cqfn.diktat.util.prependPath -import org.cqfn.diktat.util.retry import com.pinterest.ktlint.core.LintError import mu.KotlinLogging @@ -129,7 +129,7 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { val result = when { systemName.startsWith("Linux", ignoreCase = true) || systemName.startsWith("Mac", ignoreCase = true) -> ProcessBuilder("sh", "-c", "chmod 777 $savePath ; ./$savePath $filesDir/src/main/kotlin $testPath --log all") - else -> ProcessBuilder(savePath, "--log", "all", "$filesDir/src/main/kotlin", testPath) + else -> ProcessBuilder(savePath, "$filesDir/src/main/kotlin", testPath, "--log", "all") } return result } diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt index 3b54891bfa..62c70c6a76 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt @@ -1,6 +1,7 @@ package org.cqfn.diktat.ruleset.smoke import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider +import org.cqfn.diktat.ruleset.utils.format import org.cqfn.diktat.test.framework.processing.TestComparatorUnit import com.pinterest.ktlint.core.LintError import org.junit.jupiter.api.Assertions @@ -40,7 +41,13 @@ class DiktatSmokeTest : DiktatSmokeTestBase() { private fun getTestComparatorUnit(config: Path) = TestComparatorUnit( resourceFilePath = RESOURCE_FILE_PATH, - ruleSetProviderSupplier = { DiktatRuleSetProvider(config.absolutePathString()) }, - cb = { lintError, _ -> unfixedLintErrors.add(lintError) } + function = { expectedText, testFilePath -> + format( + ruleSetProviderRef = { DiktatRuleSetProvider(config.absolutePathString()) }, + text = expectedText, + fileName = testFilePath, + cb = { lintError, _ -> unfixedLintErrors.add(lintError) }, + ) + }, ) } diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt index a854244987..e7199ae6f2 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTestBase.kt @@ -32,8 +32,7 @@ import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_AFTER_OPERATORS import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_BEFORE_DOT import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_FOR_EXPRESSION_BODIES -import org.cqfn.diktat.util.assertEquals -import org.cqfn.diktat.util.deleteIfExistsSilently +import org.cqfn.diktat.test.framework.util.deleteIfExistsSilently import com.charleskorn.kaml.Yaml import com.charleskorn.kaml.YamlConfiguration @@ -327,12 +326,11 @@ abstract class DiktatSmokeTestBase { fun `smoke test with gradle script plugin`() { fixAndCompare(prepareOverriddenRulesConfig(), "kotlin-library-expected.gradle.kts", "kotlin-library.gradle.kts") assertUnfixedLintErrors { unfixedLintErrors -> - Assertions.assertEquals( + assertThat(unfixedLintErrors).containsExactly( LintError( 2, 1, "$DIKTAT_RULE_SET_ID:${CommentsRule.NAME_ID}", "[COMMENTED_OUT_CODE] you should not comment out code, " + "use VCS to save it in history and delete this block: import org.jetbrains.kotlin.gradle.dsl.jvm", false - ), - unfixedLintErrors.single() + ) ) } } @@ -351,7 +349,7 @@ abstract class DiktatSmokeTestBase { ) fixAndCompare(configFilePath, "Example1-2Expected.kt", "Example1-2Test.kt") assertUnfixedLintErrors { unfixedLintErrors -> - unfixedLintErrors.assertEquals( + assertThat(unfixedLintErrors).containsExactlyInAnyOrder( LintError(1, 1, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false), LintError(3, 1, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_TOP_LEVEL.warnText()} example", false), LintError(3, 16, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} isValid", false), diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt index 4843e06f55..0e7d3cf7f8 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt @@ -70,37 +70,6 @@ internal fun List.assertEquals(vararg expectedLintErrors: LintError) } } -/** - * Deletes the file if it exists, retrying as necessary if the file is - * blocked by another process (on Windows). - * - * @receiver the file or empty directory. - * @see Path.deleteIfExists - */ -@Suppress( - "EMPTY_BLOCK_STRUCTURE_ERROR", - "MAGIC_NUMBER", -) -internal fun Path.deleteIfExistsSilently() { - val attempts = 10 - - val deleted = retry(attempts, delayMillis = 100L, lazyDefault = { false }) { - deleteIfExists() - - /* - * Ignore the return code of `deleteIfExists()` (will be `false` - * if the file doesn't exist). - */ - true - } - - if (!deleted) { - log.warn { - "File \"${absolute()}\" not deleted after $attempts attempt(s)." - } - } -} - /** * @receiver the 1st operand. * @param other the 2nd operand. @@ -167,41 +136,3 @@ internal fun ProcessBuilder.prependPath(pathEntry: Path) { environment[pathKey] = newPath } -/** - * Retries the execution of the [block]. - * - * @param attempts the number of attempts (must be positive). - * @param delayMillis the timeout (in milliseconds) between the consecutive - * attempts. The default is 0. Ignored if [attempts] is 1. - * @param lazyDefault allows to override the return value if none of the - * attempts succeeds. By default, the last exception is thrown. - * @param block the block to execute. - * @return the result of the execution of the [block], or whatever [lazyDefault] - * evaluates to if none of the attempts is successful. - */ -internal fun retry( - attempts: Int, - delayMillis: Long = 0L, - lazyDefault: (Throwable) -> T = { error -> throw error }, - block: () -> T -): T { - require(attempts > 0) { - "The number of attempts should be positive: $attempts" - } - - var lastError: Throwable? = null - - for (i in 1..attempts) { - try { - return block() - } catch (error: Throwable) { - lastError = error - } - - if (delayMillis > 0L) { - Thread.sleep(delayMillis) - } - } - - return lazyDefault(lastError ?: Exception("The block was never executed")) -} diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestComparatorUnit.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestComparatorUnit.kt index 40fd00bcd8..3d77f1419f 100644 --- a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestComparatorUnit.kt +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestComparatorUnit.kt @@ -1,8 +1,5 @@ package org.cqfn.diktat.test.framework.processing -import org.cqfn.diktat.test.framework.util.LintErrorCallback -import org.cqfn.diktat.test.framework.util.format -import com.pinterest.ktlint.core.RuleSetProvider import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -20,13 +17,12 @@ import kotlin.io.path.readLines * * @property resourceFilePath only used when the files are loaded as resources, * via [compareFilesFromResources]. - * @property ruleSetProviderSupplier a supplier for [RuleSetProvider] - * @property cb a callback for [com.pinterest.ktlint.core.LintError] + * @property function a transformation that will be applied to the file */ @Suppress("ForbiddenComment", "TYPE_ALIAS") -class TestComparatorUnit(private val resourceFilePath: String, - private val ruleSetProviderSupplier: () -> RuleSetProvider, - private val cb: LintErrorCallback, +class TestComparatorUnit( + private val resourceFilePath: String, + private val function: (expectedText: String, testFilePath: String) -> String, ) { /** * @param expectedResult the name of the resource which has the expected @@ -92,11 +88,9 @@ class TestComparatorUnit(private val resourceFilePath: String, val copyTestFile = Path("${testFile.absolutePathString()}_copy") testFile.copyTo(copyTestFile, overwrite = true) - val actualResult = format( - ruleSetProviderRef = ruleSetProviderSupplier, - text = readFile(copyTestFile).joinToString("\n"), - fileName = copyTestFile.absolutePathString(), - cb = cb + val actualResult = function( + readFile(copyTestFile).joinToString("\n"), + copyTestFile.absolutePathString() ) val actualFileContent = if (trimLastEmptyLine) { diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt index ae94fb5e23..a0df6696a0 100644 --- a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt @@ -5,45 +5,124 @@ package org.cqfn.diktat.test.framework.util import org.cqfn.diktat.common.utils.loggerWithKtlintConfig -import com.pinterest.ktlint.core.KtLint -import com.pinterest.ktlint.core.LintError -import com.pinterest.ktlint.core.RuleSetProvider import mu.KotlinLogging -import org.intellij.lang.annotations.Language +import java.io.IOException +import java.nio.file.FileVisitResult +import java.nio.file.FileVisitResult.CONTINUE +import java.nio.file.Files.walkFileTree +import java.nio.file.Path +import java.nio.file.SimpleFileVisitor +import java.nio.file.attribute.BasicFileAttributes +import kotlin.io.path.absolute +import kotlin.io.path.deleteExisting +import kotlin.io.path.deleteIfExists @Suppress("EMPTY_BLOCK_STRUCTURE_ERROR") private val log = KotlinLogging.loggerWithKtlintConfig {} -@Suppress("TYPE_ALIAS") -val defaultCallback: (lintError: LintError, corrected: Boolean) -> Unit = { lintError, _ -> - log.warn("Received linting error: $lintError") +/** + * Deletes the file if it exists, retrying as necessary if the file is + * blocked by another process (on Windows). + * + * @receiver the file or empty directory. + * @see Path.deleteIfExists + */ +@Suppress( + "EMPTY_BLOCK_STRUCTURE_ERROR", + "MAGIC_NUMBER", +) +fun Path.deleteIfExistsSilently() { + val attempts = 10 + + val deleted = retry(attempts, delayMillis = 100L, lazyDefault = { false }) { + deleteIfExists() + + /* + * Ignore the return code of `deleteIfExists()` (will be `false` + * if the file doesn't exist). + */ + true + } + + if (!deleted) { + log.warn { + "File \"${absolute()}\" not deleted after $attempts attempt(s)." + } + } } -typealias LintErrorCallback = (LintError, Boolean) -> Unit /** - * @param ruleSetProviderRef - * @param text - * @param fileName - * @param cb callback to be called on unhandled [LintError]s - * @return formatted code + * Deletes this directory recursively. + * + * @see Path.deleteIfExistsRecursively */ -@Suppress("LAMBDA_IS_NOT_LAST_PARAMETER") -fun format( - ruleSetProviderRef: () -> RuleSetProvider, - @Language("kotlin") text: String, - fileName: String, - cb: LintErrorCallback = defaultCallback -): String { - val ruleSets = listOf(ruleSetProviderRef().get()) - return KtLint.format( - KtLint.ExperimentalParams( - text = text, - ruleSets = ruleSets, - fileName = fileName.removeSuffix("_copy"), - script = fileName.removeSuffix("_copy").endsWith("kts"), - cb = cb, - debug = true, - ) - ) +fun Path.deleteRecursively() { + walkFileTree(this, object : SimpleFileVisitor() { + override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult { + file.deleteIfExistsSilently() + return CONTINUE + } + + override fun postVisitDirectory(dir: Path, exc: IOException?): FileVisitResult { + dir.deleteExisting() + return CONTINUE + } + }) +} + +/** + * Deletes this directory recursively if it exists. + * + * @return `true` if the existing directory was successfully deleted, `false` if + * the directory doesn't exist. + * @see Files.deleteIfExists + * @see Path.deleteRecursively + */ +@Suppress("FUNCTION_BOOLEAN_PREFIX") +fun Path.deleteIfExistsRecursively(): Boolean = + try { + deleteRecursively() + true + } catch (_: NoSuchFileException) { + false + } + +/** + * Retries the execution of the [block]. + * + * @param attempts the number of attempts (must be positive). + * @param delayMillis the timeout (in milliseconds) between the consecutive + * attempts. The default is 0. Ignored if [attempts] is 1. + * @param lazyDefault allows to override the return value if none of the + * attempts succeeds. By default, the last exception is thrown. + * @param block the block to execute. + * @return the result of the execution of the [block], or whatever [lazyDefault] + * evaluates to if none of the attempts is successful. + */ +fun retry( + attempts: Int, + delayMillis: Long = 0L, + lazyDefault: (Throwable) -> T = { error -> throw error }, + block: () -> T +): T { + require(attempts > 0) { + "The number of attempts should be positive: $attempts" + } + + var lastError: Throwable? = null + + for (i in 1..attempts) { + try { + return block() + } catch (error: Throwable) { + lastError = error + } + + if (delayMillis > 0L) { + Thread.sleep(delayMillis) + } + } + + return lazyDefault(lastError ?: Exception("The block was never executed")) } diff --git a/pom.xml b/pom.xml index a60408e8ba..fe5327fbbb 100644 --- a/pom.xml +++ b/pom.xml @@ -61,7 +61,6 @@ 3.6.4 1.24 2.18.0 - 1.1.0 From e4fa18b9bde44c0166b4171484721c2270da6521 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Sat, 29 Oct 2022 01:00:08 +0300 Subject: [PATCH 28/39] diktatFix + review note --- .../org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt | 5 ++--- .../kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt index 8e4d6cce19..18ca71bb65 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt @@ -43,9 +43,8 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { saveSmokeTest(config, test) } - override fun assertUnfixedLintErrors(lintErrorsConsumer: (List) -> Unit) { - // do nothing, we can't check unfixed lint errors here - } + // do nothing, we can't check unfixed lint errors here + override fun assertUnfixedLintErrors(lintErrorsConsumer: (List) -> Unit) = Unit /** * @param testPath path to file with code that will be transformed by formatter, relative to [resourceFilePath] diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt index a0df6696a0..066dfd63df 100644 --- a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt @@ -51,7 +51,6 @@ fun Path.deleteIfExistsSilently() { } } - /** * Deletes this directory recursively. * From 29d8f511a63209704ab3f563842be5b26923b0e2 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Sat, 29 Oct 2022 01:02:43 +0300 Subject: [PATCH 29/39] upgraded save-cli version to 0.3.4 --- .../kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt index 18ca71bb65..912e358621 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt @@ -139,7 +139,7 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { private const val BUILD_DIRECTORY = "target" private const val FAT_JAR_GLOB = "diktat-*.jar" private const val KTLINT_VERSION = "0.47.1" - private const val SAVE_VERSION: String = "0.3.2" + private const val SAVE_VERSION: String = "0.3.4" private val baseDirectory = Path("src/test/resources/test/smoke").absolute() private fun getSaveForCurrentOs(): String { From caee1acc5834a356bf845ac3e9d01ae834e47d5a Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Sat, 29 Oct 2022 01:07:46 +0300 Subject: [PATCH 30/39] removed unused methods --- .../org/cqfn/diktat/util/SmokeTestUtils.kt | 53 ------------------- 1 file changed, 53 deletions(-) diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt index 0e7d3cf7f8..afb3182fe4 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt @@ -6,70 +6,17 @@ package org.cqfn.diktat.util import org.cqfn.diktat.common.utils.loggerWithKtlintConfig -import com.pinterest.ktlint.core.LintError import mu.KotlinLogging -import org.assertj.core.api.Assertions.assertThat -import org.assertj.core.api.SoftAssertions.assertSoftly import java.io.File import java.nio.file.NoSuchFileException import java.nio.file.Path -import java.util.function.Consumer -import kotlin.io.path.absolute -import kotlin.io.path.deleteIfExists import kotlin.io.path.isDirectory import kotlin.io.path.isSameFileAs -internal const val TEST_FILE_NAME = "TestFileName.kt" - @Suppress("EMPTY_BLOCK_STRUCTURE_ERROR") private val log = KotlinLogging.loggerWithKtlintConfig {} -@Suppress("TYPE_ALIAS") -internal val defaultCallback: (lintError: LintError, corrected: Boolean) -> Unit = { lintError, _ -> - log.warn("Received linting error: $lintError") -} - -typealias LintErrorCallback = (LintError, Boolean) -> Unit - -/** - * Compare [LintError]s from [this] with [expectedLintErrors] - * - * @param expectedLintErrors expected [LintError]s - */ -internal fun List.assertEquals(vararg expectedLintErrors: LintError) { - if (size == expectedLintErrors.size) { - assertThat(this) - .allSatisfy(Consumer { actual -> - val expected = expectedLintErrors[this@assertEquals.indexOf(actual)] - assertSoftly { sa -> - sa - .assertThat(actual.line) - .`as`("Line") - .isEqualTo(expected.line) - sa - .assertThat(actual.col) - .`as`("Column") - .isEqualTo(expected.col) - sa - .assertThat(actual.ruleId) - .`as`("Rule id") - .isEqualTo(expected.ruleId) - sa - .assertThat(actual.detail) - .`as`("Detailed message") - .isEqualTo(expected.detail) - sa - .assertThat(actual.canBeAutoCorrected) - .`as`("Can be autocorrected") - .isEqualTo(expected.canBeAutoCorrected) - } - }) - } else { - assertThat(this).containsExactly(*expectedLintErrors) - } -} - /** * @receiver the 1st operand. * @param other the 2nd operand. From 044ada44f875f6ec80996e14b80719a88fbabc12 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Sat, 29 Oct 2022 01:08:58 +0300 Subject: [PATCH 31/39] removed unused methods #2 --- .../src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt | 7 ------- 1 file changed, 7 deletions(-) diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt index afb3182fe4..0dd197ba6d 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt @@ -4,19 +4,12 @@ package org.cqfn.diktat.util -import org.cqfn.diktat.common.utils.loggerWithKtlintConfig - -import mu.KotlinLogging - import java.io.File import java.nio.file.NoSuchFileException import java.nio.file.Path import kotlin.io.path.isDirectory import kotlin.io.path.isSameFileAs -@Suppress("EMPTY_BLOCK_STRUCTURE_ERROR") -private val log = KotlinLogging.loggerWithKtlintConfig {} - /** * @receiver the 1st operand. * @param other the 2nd operand. From 7f36ea102e77229eb1960cd1444d41fec4326671 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Sat, 29 Oct 2022 01:18:37 +0300 Subject: [PATCH 32/39] diktatFix --- .../org/cqfn/diktat/ruleset/utils/KtlintUtils.kt | 1 - .../test/kotlin/org/cqfn/diktat/util/FixTestBase.kt | 12 ++++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/KtlintUtils.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/KtlintUtils.kt index 63ac4d8a3e..6d773bd279 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/KtlintUtils.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/KtlintUtils.kt @@ -45,7 +45,6 @@ fun ExperimentalParams.ignoreCorrectedErrors(): ExperimentalParams = } }) - /** * @param ruleSetProviderRef * @param text 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 a3950e109f..925c531ae8 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 @@ -33,12 +33,12 @@ open class FixTestBase( TestComparatorUnit( resourceFilePath = resourceFilePath, function = { expectedText, testFilePath -> - format( - ruleSetProviderRef = { DiktatRuleSetProvider4Test(ruleSupplier, overrideRulesConfigList ?: defaultRulesConfigList) }, - text = expectedText, - fileName = testFilePath, - cb = cb, - ) + format( + ruleSetProviderRef = { DiktatRuleSetProvider4Test(ruleSupplier, overrideRulesConfigList ?: defaultRulesConfigList) }, + text = expectedText, + fileName = testFilePath, + cb = cb, + ) }, ) } From e1af67899310b6724f39bfc38fd885321e1dcb2c Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Sat, 29 Oct 2022 01:22:02 +0300 Subject: [PATCH 33/39] diktatFix #2 --- .../org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt | 12 ++++++------ .../kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt | 1 - 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt index 62c70c6a76..46cf775a42 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt @@ -42,12 +42,12 @@ class DiktatSmokeTest : DiktatSmokeTestBase() { private fun getTestComparatorUnit(config: Path) = TestComparatorUnit( resourceFilePath = RESOURCE_FILE_PATH, function = { expectedText, testFilePath -> - format( - ruleSetProviderRef = { DiktatRuleSetProvider(config.absolutePathString()) }, - text = expectedText, - fileName = testFilePath, - cb = { lintError, _ -> unfixedLintErrors.add(lintError) }, - ) + format( + ruleSetProviderRef = { DiktatRuleSetProvider(config.absolutePathString()) }, + text = expectedText, + fileName = testFilePath, + cb = { lintError, _ -> unfixedLintErrors.add(lintError) }, + ) }, ) } diff --git a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt index 0dd197ba6d..8b69310762 100644 --- a/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt +++ b/diktat-ruleset/src/test/kotlin/org/cqfn/diktat/util/SmokeTestUtils.kt @@ -75,4 +75,3 @@ internal fun ProcessBuilder.prependPath(pathEntry: Path) { environment[pathKey] = newPath } - From 058e8b90e00eef582e15d847629c1c25224bc3eb Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Sat, 29 Oct 2022 01:39:12 +0300 Subject: [PATCH 34/39] java exception --- .../main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt index 066dfd63df..c25c6967e9 100644 --- a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt @@ -12,6 +12,7 @@ import java.nio.file.FileVisitResult.CONTINUE import java.nio.file.Files.walkFileTree import java.nio.file.Path import java.nio.file.SimpleFileVisitor +import java.nio.file.NoSuchFileException import java.nio.file.attribute.BasicFileAttributes import kotlin.io.path.absolute import kotlin.io.path.deleteExisting From 15486d1efcac11f4fe4db8a211638ce7b42ce65e Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Sat, 29 Oct 2022 01:45:44 +0300 Subject: [PATCH 35/39] diktatFix --- .../kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt index c25c6967e9..4529b6745d 100644 --- a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt @@ -6,6 +6,7 @@ package org.cqfn.diktat.test.framework.util import org.cqfn.diktat.common.utils.loggerWithKtlintConfig import mu.KotlinLogging + import java.io.IOException import java.nio.file.FileVisitResult import java.nio.file.FileVisitResult.CONTINUE @@ -14,6 +15,7 @@ import java.nio.file.Path import java.nio.file.SimpleFileVisitor import java.nio.file.NoSuchFileException import java.nio.file.attribute.BasicFileAttributes + import kotlin.io.path.absolute import kotlin.io.path.deleteExisting import kotlin.io.path.deleteIfExists From ac8932fd3b7235d8572f427163e78e063c1dd93d Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Sat, 29 Oct 2022 01:48:14 +0300 Subject: [PATCH 36/39] diktatFix #2 --- .../kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt index 4529b6745d..f7067e79d9 100644 --- a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt @@ -4,8 +4,8 @@ package org.cqfn.diktat.test.framework.util -import org.cqfn.diktat.common.utils.loggerWithKtlintConfig import mu.KotlinLogging +import org.cqfn.diktat.common.utils.loggerWithKtlintConfig import java.io.IOException import java.nio.file.FileVisitResult From 6748f91fa2caa6150aa07518467458f69d0c0858 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Sat, 29 Oct 2022 01:50:54 +0300 Subject: [PATCH 37/39] diktatFix #3 --- .../main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt index f7067e79d9..8640068101 100644 --- a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt @@ -15,7 +15,6 @@ import java.nio.file.Path import java.nio.file.SimpleFileVisitor import java.nio.file.NoSuchFileException import java.nio.file.attribute.BasicFileAttributes - import kotlin.io.path.absolute import kotlin.io.path.deleteExisting import kotlin.io.path.deleteIfExists From fe3696b0e25e684d23336e86e57d2db706cd5a5f Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Sat, 29 Oct 2022 01:54:26 +0300 Subject: [PATCH 38/39] diktatFix #4 --- .../kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt index 8640068101..564297cf38 100644 --- a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/util/TestUtils.kt @@ -4,17 +4,19 @@ package org.cqfn.diktat.test.framework.util -import mu.KotlinLogging import org.cqfn.diktat.common.utils.loggerWithKtlintConfig +import mu.KotlinLogging + import java.io.IOException import java.nio.file.FileVisitResult import java.nio.file.FileVisitResult.CONTINUE import java.nio.file.Files.walkFileTree +import java.nio.file.NoSuchFileException import java.nio.file.Path import java.nio.file.SimpleFileVisitor -import java.nio.file.NoSuchFileException import java.nio.file.attribute.BasicFileAttributes + import kotlin.io.path.absolute import kotlin.io.path.deleteExisting import kotlin.io.path.deleteIfExists From a2575474c92c8823e803199b494942f559ea1f9d Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Sat, 29 Oct 2022 02:41:35 +0300 Subject: [PATCH 39/39] set big timeout for save-cli process --- diktat-ruleset/src/test/resources/test/smoke/save.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/diktat-ruleset/src/test/resources/test/smoke/save.toml b/diktat-ruleset/src/test/resources/test/smoke/save.toml index 54611e9109..bebb752e9d 100644 --- a/diktat-ruleset/src/test/resources/test/smoke/save.toml +++ b/diktat-ruleset/src/test/resources/test/smoke/save.toml @@ -5,6 +5,7 @@ description = "SmokeTest" suiteName = "SmokeTest" language = "Kotlin" expectedWarningsPattern = "// ;warn:?(.*):(\\d*): (.+)" +timeOutMillis = 3600000 ["fix and warn"] ["fix and warn".fix]