Skip to content

Commit

Permalink
Migrate diktat smoke tests to SAVE-cli mechanism
Browse files Browse the repository at this point in the history
### What's done:
* migrated diktat smoke tests to SAVE-cli mechanism
Closes #1383
  • Loading branch information
Cheshiriks committed Jul 6, 2022
1 parent 394d292 commit 98dc4a2
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 170 deletions.
Original file line number Diff line number Diff line change
@@ -1,121 +1,64 @@
package org.cqfn.diktat.ruleset.smoke

import org.cqfn.diktat.common.config.rules.DIKTAT_COMMON
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.RulesConfigReader
import org.cqfn.diktat.ruleset.constants.Warnings
import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider
import org.cqfn.diktat.util.FixTestBase

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.BeforeEach
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test

import java.time.LocalDate

import kotlin.io.path.createTempFile
import kotlinx.serialization.builtins.ListSerializer

class DiktatSaveSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
{ DiktatRuleSetProvider(configFilePath) },
{ lintError, _ -> unfixedLintErrors.add(lintError) },
) {
/**
* Disable some of the rules.
*/
@Suppress("UnsafeCallOnNullableType")
private fun overrideRulesConfig(rulesToDisable: List<Warnings>, rulesToOverride: RuleToConfig = emptyMap()) {
val rulesConfig = RulesConfigReader(javaClass.classLoader).readResource(DEFAULT_CONFIG_PATH)!!
.toMutableList()
.also { rulesConfig ->
rulesToDisable.forEach { warning ->
rulesConfig.removeIf { it.name == warning.name }
rulesConfig.add(RulesConfig(warning.name, enabled = false, configuration = emptyMap()))
}
rulesToOverride.forEach { (warning, configuration) ->
rulesConfig.removeIf { it.name == warning }
rulesConfig.add(RulesConfig(warning, enabled = true, configuration = configuration))
}
}
.toList()
createTempFile().toFile()
.also {
configFilePath = it.absolutePath
}
.writeText(
Yaml(configuration = YamlConfiguration(strictMode = true))
.encodeToString(ListSerializer(RulesConfig.serializer()), rulesConfig)
)
class DiktatSaveSmokeTest : DiktatSmokeTestBase() {
@BeforeEach
fun setUp() {
unfixedLintErrors.clear()
}

@Test
@Tag("DiktatRuleSetProvider")
fun `save smoke test #1`() {
saveSmokeTest(DEFAULT_CONFIG_PATH, "Bug1Expected.kt", "Bug1Test.kt")
@AfterEach
fun tearDown() {
configFilePath = DEFAULT_CONFIG_PATH
}

@Test
@Tag("DiktatRuleSetProvider")
fun `save smoke test #2`() {
saveSmokeTest(DEFAULT_CONFIG_PATH, "DefaultPackageExpected.kt", "DefaultPackageTest.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `save smoke test #3`() {
overrideRulesConfig(
rulesToDisable = emptyList(),
rulesToOverride = mapOf(
Warnings.WRONG_INDENTATION.name to mapOf(
"extendedIndentAfterOperators" to "true",
"extendedIndentBeforeDot" to "false",
)
)
)
saveSmokeTest(configFilePath, "Example1Expected.kt", "Example1Test.kt")
override fun `regression - should not fail if package is not set`() {
overrideRulesConfig(listOf(Warnings.PACKAGE_NAME_MISSING, Warnings.PACKAGE_NAME_INCORRECT_PATH,
Warnings.PACKAGE_NAME_INCORRECT_PREFIX))
saveSmokeTest(configFilePath, "DefaultPackageExpected.kt", "DefaultPackageTest.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `save smoke test #4`() {
overrideRulesConfig(
rulesToDisable = emptyList(),
rulesToOverride = mapOf(
Warnings.WRONG_INDENTATION.name to mapOf(
"extendedIndentAfterOperators" to "true",
"extendedIndentBeforeDot" to "true",
)
)
)
saveSmokeTest(configFilePath, "Example2Expected.kt", "Example2Test.kt")
override fun `smoke test #8 - anonymous function`() {
saveSmokeTest(configFilePath, "Example8Expected.kt", "Example8Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `save smoke test #5`() {
saveSmokeTest(DEFAULT_CONFIG_PATH, "Example3Expected.kt", "Example3Test.kt")
override fun `smoke test #7`() {
saveSmokeTest(configFilePath, "Example7Expected.kt", "Example7Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `save smoke test #6`() {
override fun `smoke test #6`() {
overrideRulesConfig(
rulesToDisable = emptyList(),
rulesToOverride = mapOf(
Warnings.WRONG_INDENTATION.name to mapOf(
"extendedIndentAfterOperators" to "true",
"extendedIndentBeforeDot" to "false",
"extendedIndentBeforeDot" to "true",
)
)
)
saveSmokeTest(configFilePath, "Example4Expected.kt", "Example4Test.kt")
saveSmokeTest(configFilePath, "Example6Expected.kt", "Example6Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `save smoke test #7`() {
override fun `smoke test #5`() {
overrideRulesConfig(emptyList(),
mapOf(
Warnings.HEADER_MISSING_OR_WRONG_COPYRIGHT.name to mapOf(
Expand All @@ -139,66 +82,103 @@ class DiktatSaveSmokeTest : FixTestBase("test/smoke/src/main/kotlin",

@Test
@Tag("DiktatRuleSetProvider")
fun `save smoke test #8`() {
override fun `smoke test #4`() {
overrideRulesConfig(
rulesToDisable = emptyList(),
rulesToOverride = mapOf(
Warnings.WRONG_INDENTATION.name to mapOf(
"extendedIndentAfterOperators" to "true",
"extendedIndentBeforeDot" to "true",
"extendedIndentBeforeDot" to "false",
)
)
)
saveSmokeTest(configFilePath, "Example6Expected.kt", "Example6Test.kt")
saveSmokeTest(configFilePath, "Example4Expected.kt", "Example4Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `save smoke test #9`() {
saveSmokeTest(DEFAULT_CONFIG_PATH, "Example7Expected.kt", "Example7Test.kt")
override fun `smoke test #3`() {
saveSmokeTest(configFilePath, "Example3Expected.kt", "Example3Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `save smoke test #10`() {
saveSmokeTest(DEFAULT_CONFIG_PATH, "Example8Expected.kt", "Example8Test.kt")
override fun `regression - shouldn't throw exception in cases similar to #371`() {
saveSmokeTest(configFilePath, "Bug1Expected.kt", "Bug1Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `save smoke test #11`() {
saveSmokeTest(DEFAULT_CONFIG_PATH, "KdocFormattingMultilineTagsExpected.kt", "KdocFormattingMultilineTagsTest.kt")
override fun `smoke test #2`() {
overrideRulesConfig(
rulesToDisable = emptyList(),
rulesToOverride = mapOf(
Warnings.WRONG_INDENTATION.name to mapOf(
"extendedIndentAfterOperators" to "true",
"extendedIndentBeforeDot" to "true",
)
)
)
saveSmokeTest(configFilePath, "Example2Expected.kt", "Example2Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `save smoke test #12`() {
saveSmokeTest(DEFAULT_CONFIG_PATH, "ManyLineTransformInLongLineExpected.kt", "ManyLineTransformInLongLineTest.kt")
override fun `smoke test #1`() {
overrideRulesConfig(
rulesToDisable = emptyList(),
rulesToOverride = mapOf(
Warnings.WRONG_INDENTATION.name to mapOf(
"extendedIndentAfterOperators" to "true",
"extendedIndentBeforeDot" to "false",
)
)
)
saveSmokeTest(configFilePath, "Example1Expected.kt", "Example1Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `save smoke test #13`() {
saveSmokeTest(DEFAULT_CONFIG_PATH, "LocalVariableWithOffsetExpected.kt", "LocalVariableWithOffsetTest.kt")
override fun `smoke test with kts files #2`() {
saveSmokeTest(configFilePath, "script/SimpleRunInScriptExpected.kts", "script/SimpleRunInScriptTest.kts")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `save smoke test with kts files #2`() {
saveSmokeTest(DEFAULT_CONFIG_PATH, "script/SimpleRunInScriptExpected.kts", "script/SimpleRunInScriptTest.kts")
override fun `smoke test with kts files with package name`() {
saveSmokeTest(configFilePath, "script/PackageInScriptExpected.kts", "script/PackageInScriptTest.kts")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `save smoke test with kts files with package name`() {
saveSmokeTest(DEFAULT_CONFIG_PATH, "script/PackageInScriptExpected.kts", "script/PackageInScriptTest.kts")
override fun `disable charters`() {
overrideRulesConfig(
emptyList(),
mapOf(
DIKTAT_COMMON to mapOf(
"domainName" to "org.cqfn.diktat",
"disabledChapters" to "Naming,3,4,5,Classes"
)
)
)
saveSmokeTest(configFilePath, "Example1-2Expected.kt", "Example1Test.kt")
}

companion object {
private const val DEFAULT_CONFIG_PATH = "../diktat-analysis.yml"
private val unfixedLintErrors: MutableList<LintError> = mutableListOf()
@Test
@Tag("DiktatRuleSetProvider")
override fun `regression - should correctly handle tags with empty lines`() {
saveSmokeTest(configFilePath, "KdocFormattingMultilineTagsExpected.kt", "KdocFormattingMultilineTagsTest.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
override fun `regression - FP of local variables rule`() {
saveSmokeTest(configFilePath, "LocalVariableWithOffsetExpected.kt", "LocalVariableWithOffsetTest.kt")
}

// by default using same yml config as for diktat code style check, but allow to override
private var configFilePath = DEFAULT_CONFIG_PATH
@Test
@Tag("DiktatRuleSetProvider")
override fun `fix can cause long line`() {
saveSmokeTest(configFilePath, "ManyLineTransformInLongLineExpected.kt", "ManyLineTransformInLongLineTest.kt")
}
}
Loading

0 comments on commit 98dc4a2

Please sign in to comment.