Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate diktat smoke tests to SAVE-cli mechanism #1388

Merged
merged 58 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
3c2a4e4
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jun 21, 2022
d40ac6b
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jun 22, 2022
96abde1
Merge branch 'master' into feature/migrate_tests_to_save
Cheshiriks Jun 22, 2022
660a7d1
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jun 23, 2022
4e6f391
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jun 23, 2022
963c456
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jun 23, 2022
71a9fd4
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jun 23, 2022
8287e61
Merge branch 'master' into feature/migrate_tests_to_save
Cheshiriks Jun 23, 2022
9a27748
Merge branch 'master' into feature/migrate_tests_to_save
Cheshiriks Jun 24, 2022
f045ee9
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jun 28, 2022
81b11b8
Merge branch 'master' into feature/migrate_tests_to_save
Cheshiriks Jun 28, 2022
345bbb4
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jun 29, 2022
2e8f33c
Merge branch 'master' into feature/migrate_tests_to_save
Cheshiriks Jun 29, 2022
355fd2b
Merge branch 'master' into feature/migrate_tests_to_save
Cheshiriks Jun 30, 2022
e29f5d9
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 4, 2022
1764a4b
Merge branch 'master' into feature/migrate_tests_to_save
Cheshiriks Jul 4, 2022
00628f7
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 5, 2022
e3e410e
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 5, 2022
33c5187
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 5, 2022
143d79e
Merge branch 'master' into feature/migrate_tests_to_save
Cheshiriks Jul 5, 2022
5e610f3
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 5, 2022
ac59eca
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 5, 2022
b3456af
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 6, 2022
394d292
Merge branch 'master' into feature/migrate_tests_to_save
Cheshiriks Jul 6, 2022
98dc4a2
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 6, 2022
c9ee5f4
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 7, 2022
7f5184a
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 11, 2022
703a37d
Merge branch 'master' into feature/migrate_tests_to_save
Cheshiriks Jul 11, 2022
9f23ae5
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 11, 2022
289e135
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 11, 2022
abcdb8f
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 11, 2022
fa10731
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 11, 2022
3785fe8
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 11, 2022
f5127c0
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 11, 2022
756aa5e
Merge branch 'master' into feature/migrate_tests_to_save
Cheshiriks Jul 11, 2022
ea5268c
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 11, 2022
80c375c
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 11, 2022
5ae5676
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 11, 2022
c1fb33a
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 11, 2022
d8a4e25
Fixing execution cmd
orchestr7 Jul 11, 2022
5122d82
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 11, 2022
95ba657
Fixing execution cmd
orchestr7 Jul 11, 2022
2464246
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 11, 2022
bb5a101
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 11, 2022
fe8f8eb
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 12, 2022
dd5e6be
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 12, 2022
18e08ff
Merge branch 'master' into feature/migrate_tests_to_save
Cheshiriks Jul 12, 2022
4c8c223
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 12, 2022
3258081
Merge branch 'master' into feature/migrate_tests_to_save
Cheshiriks Jul 12, 2022
ff780e4
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 12, 2022
f1e3878
Merge branch 'master' into feature/migrate_tests_to_save
Cheshiriks Jul 14, 2022
91f3fd8
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 14, 2022
8e51f1e
Merge branch 'master' into feature/migrate_tests_to_save
Cheshiriks Jul 14, 2022
43a4de9
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 14, 2022
b6c3401
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 14, 2022
7fec3f3
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 14, 2022
04dc7ed
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 14, 2022
23ec13e
Migrate diktat smoke tests to SAVE-cli mechanism
Cheshiriks Jul 14, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ jobs:
restore-keys: |
maven-build-
- name: Maven Install
run: mvn -B clean install
run: mvn -B clean install -DskipTests
- name: Maven run test
run: mvn test
- name: Code coverage report
uses: codecov/codecov-action@v3
with:
Expand Down Expand Up @@ -141,13 +143,25 @@ jobs:
- name: Maven Install
if: ${{ runner.os == 'Linux' || runner.os == 'macOS' }}
run: |
mvn -B -T1C clean install
mvn -B -T1C clean install -DskipTests
shell: bash

- name: Maven run test
if: ${{ runner.os == 'Linux' || runner.os == 'macOS' }}
run: |
mvn test
shell: bash

- name: Maven Install on windows
if: runner.os == 'Windows'
run: |
mvn -B -T1C clean install
mvn -B -T1C clean install -DskipTests
shell: cmd

- name: Maven run test
if: runner.os == 'Windows'
run: |
mvn test
shell: cmd

# This step needs a Git repository, so it's impossible to extract it
Expand Down
6 changes: 6 additions & 0 deletions diktat-rules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<apache.httpclient.version>4.5.13</apache.httpclient.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -93,6 +94,11 @@
<groupId>com.bpodgursky</groupId>
<artifactId>jbool_expressions</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${apache.httpclient.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.cqfn.diktat.ruleset.smoke

class DiktatSaveSmokeTest : DiktatSmokeTestBase() {
override fun fixAndCompareBase(
config: String,
test: String,
expected: String
) {
saveSmokeTest(config, test, expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,37 @@ 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.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.RulesConfigReader
import org.cqfn.diktat.ruleset.constants.Warnings
import org.cqfn.diktat.ruleset.constants.Warnings.EMPTY_BLOCK_STRUCTURE_ERROR
import org.cqfn.diktat.ruleset.constants.Warnings.FILE_NAME_MATCH_CLASS
import org.cqfn.diktat.ruleset.constants.Warnings.HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE
import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_NO_EMPTY_TAGS
import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_WITHOUT_PARAM_TAG
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
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.rules.chapter3.EmptyBlock
import org.cqfn.diktat.ruleset.rules.chapter6.classes.InlineClassesRule
import org.cqfn.diktat.util.FixTestBase
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
import org.junit.jupiter.api.Test

import java.io.File
import java.time.LocalDate

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

typealias RuleToConfig = Map<String, Map<String, String>>

/**
* 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 : FixTestBase("test/smoke/src/main/kotlin",
{ DiktatRuleSetProvider(configFilePath) },
{ lintError, _ -> unfixedLintErrors.add(lintError) },
null
) {
/**
* Disable some of the rules.
*/
@Suppress("UnsafeCallOnNullableType")
private fun overrideRulesConfig(rulesToDisable: List<Warnings>, rulesToOverride: RuleToConfig = emptyMap()) {
val rulesConfig = RulesConfigReader(javaClass.classLoader).readResource(configFilePath)!!
.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)
)
}

@BeforeEach
fun setUp() {
unfixedLintErrors.clear()
}

@AfterEach
fun tearDown() {
configFilePath = DEFAULT_CONFIG_PATH
class DiktatSmokeTest : DiktatSmokeTestBase() {
override fun fixAndCompareBase(
config: String,
test: String,
expected: String
) {
fixAndCompareSmokeTest(test, expected)
}

@Test
Expand All @@ -98,152 +42,6 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
"../../jsMain/kotlin/org/cqfn/diktat/scripts/ScriptTest.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
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))
fixAndCompareSmokeTest("DefaultPackageExpected.kt", "DefaultPackageTest.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #8 - anonymous function`() {
fixAndCompareSmokeTest("Example8Expected.kt", "Example8Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #7`() {
fixAndCompareSmokeTest("Example7Expected.kt", "Example7Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #6`() {
overrideRulesConfig(
rulesToDisable = emptyList(),
rulesToOverride = mapOf(
WRONG_INDENTATION.name to mapOf(
"extendedIndentAfterOperators" to "true",
"extendedIndentBeforeDot" to "true",
)
)
)
fixAndCompareSmokeTest("Example6Expected.kt", "Example6Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #5`() {
overrideRulesConfig(emptyList(),
mapOf(
Warnings.HEADER_MISSING_OR_WRONG_COPYRIGHT.name to mapOf(
"isCopyrightMandatory" to "true",
"copyrightText" to """|Copyright 2018-${LocalDate.now().year} John Doe.
| Licensed under the Apache License, Version 2.0 (the "License");
| you may not use this file except in compliance with the License.
| You may obtain a copy of the License at
|
| http://www.apache.org/licenses/LICENSE-2.0
""".trimMargin()
),
DIKTAT_COMMON to mapOf(
"domainName" to "org.cqfn.diktat",
"kotlinVersion" to "1.3.7"
)
)
)
fixAndCompareSmokeTest("Example5Expected.kt", "Example5Test.kt")

Assertions.assertFalse(
unfixedLintErrors.contains(LintError(line = 1, col = 1, ruleId = "diktat-ruleset:${CommentsRule.NAME_ID}", detail = "${Warnings.COMMENTED_OUT_CODE.warnText()} /*"))
)

Assertions.assertTrue(
unfixedLintErrors.contains(LintError(1, 1, "diktat-ruleset:${InlineClassesRule.NAME_ID}", "${Warnings.INLINE_CLASS_CAN_BE_USED.warnText()} class Some"))
)
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #4`() {
overrideRulesConfig(
rulesToDisable = emptyList(),
rulesToOverride = mapOf(
WRONG_INDENTATION.name to mapOf(
"extendedIndentAfterOperators" to "true",
"extendedIndentBeforeDot" to "false",
)
)
)
fixAndCompareSmokeTest("Example4Expected.kt", "Example4Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #3`() {
fixAndCompareSmokeTest("Example3Expected.kt", "Example3Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `regression - shouldn't throw exception in cases similar to #371`() {
fixAndCompareSmokeTest("Bug1Expected.kt", "Bug1Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #2`() {
overrideRulesConfig(
rulesToDisable = emptyList(),
rulesToOverride = mapOf(
WRONG_INDENTATION.name to mapOf(
"extendedIndentAfterOperators" to "true",
"extendedIndentBeforeDot" to "true",
)
)
)
fixAndCompareSmokeTest("Example2Expected.kt", "Example2Test.kt")
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),
LintError(15, 23, "$DIKTAT_RULE_SET_ID:${KdocMethods.NAME_ID}",
"${KDOC_WITHOUT_PARAM_TAG.warnText()} createWithFile (containerName)", true),
LintError(31, 14, "$DIKTAT_RULE_SET_ID:${EmptyBlock.NAME_ID}",
"${EMPTY_BLOCK_STRUCTURE_ERROR.warnText()} empty blocks are forbidden unless it is function with override keyword", false)
)
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #1`() {
overrideRulesConfig(
rulesToDisable = emptyList(),
rulesToOverride = mapOf(
WRONG_INDENTATION.name to mapOf(
"extendedIndentAfterOperators" to "true",
"extendedIndentBeforeDot" to "false",
)
)
)
fixAndCompareSmokeTest("Example1Expected.kt", "Example1Test.kt")
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),
LintError(3, 6, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_TOP_LEVEL.warnText()} Example", false),
LintError(3, 26, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} isValid", false),
LintError(6, 9, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} foo", false),
LintError(8, 8, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} foo", false),
LintError(8, 8, "$DIKTAT_RULE_SET_ID:${KdocMethods.NAME_ID}", "${MISSING_KDOC_ON_FUNCTION.warnText()} foo", false),
LintError(9, 3, "$DIKTAT_RULE_SET_ID:${EmptyBlock.NAME_ID}", EMPTY_BLOCK_STRUCTURE_ERROR.warnText() +
" empty blocks are forbidden unless it is function with override keyword", false),
LintError(12, 10, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
LintError(14, 8, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
LintError(19, 20, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false)
)
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test with kts files`() {
Expand Down Expand Up @@ -282,20 +80,6 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
)
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test with kts files #2`() {
fixAndCompareSmokeTest("script/SimpleRunInScriptExpected.kts", "script/SimpleRunInScriptTest.kts")
Assertions.assertEquals(7, unfixedLintErrors.size)
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test with kts files with package name`() {
fixAndCompareSmokeTest("script/PackageInScriptExpected.kts", "script/PackageInScriptTest.kts")
Assertions.assertEquals(7, unfixedLintErrors.size)
}

@Test
@Tag("DiktatRuleSetProvider")
fun `disable charters`() {
Expand All @@ -321,38 +105,4 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
LintError(18, 40, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false)
)
}

@Test
@Tag("DiktatRuleSetProvider")
fun `regression - should correctly handle tags with empty lines`() {
fixAndCompareSmokeTest("KdocFormattingMultilineTagsExpected.kt", "KdocFormattingMultilineTagsTest.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `regression - FP of local variables rule`() {
fixAndCompareSmokeTest("LocalVariableWithOffsetExpected.kt", "LocalVariableWithOffsetTest.kt")
org.assertj
.core
.api
.Assertions
.assertThat(unfixedLintErrors)
.noneMatch {
it.ruleId == "diktat-ruleset:local-variables"
}
}

@Test
@Tag("DiktatRuleSetProvider")
fun `fix can cause long line`() {
fixAndCompareSmokeTest("ManyLineTransformInLongLineExpected.kt", "ManyLineTransformInLongLineTest.kt")
}

companion object {
private const val DEFAULT_CONFIG_PATH = "../diktat-analysis.yml"
private val unfixedLintErrors: MutableList<LintError> = mutableListOf()

// by default using same yml config as for diktat code style check, but allow to override
private var configFilePath = DEFAULT_CONFIG_PATH
}
}
Loading