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

Added Save in diktat smoke tests #1060

Merged
merged 6 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test with multiplatform project layout`() {
fixAndCompare("../../jsMain/kotlin/org/cqfn/diktat/scripts/ScriptExpected.kt",
fixAndCompareSmokeTest("../../jsMain/kotlin/org/cqfn/diktat/scripts/ScriptExpected.kt",
"../../jsMain/kotlin/org/cqfn/diktat/scripts/ScriptTest.kt")
}

Expand All @@ -95,19 +95,19 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
fun `regression - should not fail if package is not set`() {
overrideRulesConfig(listOf(Warnings.PACKAGE_NAME_MISSING, Warnings.PACKAGE_NAME_INCORRECT_PATH,
Warnings.PACKAGE_NAME_INCORRECT_PREFIX))
fixAndCompare("DefaultPackageExpected.kt", "DefaultPackageTest.kt")
fixAndCompareSmokeTest("DefaultPackageExpected.kt", "DefaultPackageTest.kt")
}

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

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #6`() {
fixAndCompare("Example6Expected.kt", "Example6Test.kt")
fixAndCompareSmokeTest("Example6Expected.kt", "Example6Test.kt")
}

@Test
Expand All @@ -131,7 +131,7 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
)
)
)
fixAndCompare("Example5Expected.kt", "Example5Test.kt")
fixAndCompareSmokeTest("Example5Expected.kt", "Example5Test.kt")

Assertions.assertFalse(
unfixedLintErrors.contains(LintError(line = 1, col = 1, ruleId = "diktat-ruleset:comments", detail = "${Warnings.COMMENTED_OUT_CODE.warnText()} /*"))
Expand All @@ -145,25 +145,25 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #4`() {
fixAndCompare("Example4Expected.kt", "Example4Test.kt")
fixAndCompareSmokeTest("Example4Expected.kt", "Example4Test.kt")
}

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

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

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #2`() {
fixAndCompare("Example2Expected.kt", "Example2Test.kt")
fixAndCompareSmokeTest("Example2Expected.kt", "Example2Test.kt")
unfixedLintErrors.assertEquals(
LintError(1, 1, "$DIKTAT_RULE_SET_ID:header-comment", "${HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE.warnText()} there are 2 declared classes and/or objects", false),
LintError(15, 23, "$DIKTAT_RULE_SET_ID:kdoc-methods",
Expand All @@ -176,7 +176,7 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #1`() {
fixAndCompare("Example1Expected.kt", "Example1Test.kt")
fixAndCompareSmokeTest("Example1Expected.kt", "Example1Test.kt")
unfixedLintErrors.assertEquals(
LintError(1, 1, "$DIKTAT_RULE_SET_ID:file-naming", "${FILE_NAME_MATCH_CLASS.warnText()} Example1Test.kt vs Example", true),
LintError(1, 1, "$DIKTAT_RULE_SET_ID:kdoc-formatting", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
Expand Down Expand Up @@ -220,7 +220,7 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test with gradle script plugin`() {
fixAndCompare("kotlin-library-expected.gradle.kts", "kotlin-library.gradle.kts")
fixAndCompareSmokeTest("kotlin-library-expected.gradle.kts", "kotlin-library.gradle.kts")
Assertions.assertEquals(
LintError(2, 1, "$DIKTAT_RULE_SET_ID:comments", "[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),
Expand All @@ -231,14 +231,14 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test with kts files #2`() {
fixAndCompare("script/SimpleRunInScriptExpected.kts", "script/SimpleRunInScriptTest.kts")
fixAndCompareSmokeTest("script/SimpleRunInScriptExpected.kts", "script/SimpleRunInScriptTest.kts")
Assertions.assertEquals(3, unfixedLintErrors.size)
}

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

Expand All @@ -254,7 +254,7 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
)
)
)
fixAndCompare("Example1-2Expected.kt", "Example1Test.kt")
fixAndCompareSmokeTest("Example1-2Expected.kt", "Example1Test.kt")
unfixedLintErrors.assertEquals(
LintError(1, 1, "$DIKTAT_RULE_SET_ID:kdoc-formatting", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
LintError(3, 1, "$DIKTAT_RULE_SET_ID:kdoc-comments", "${MISSING_KDOC_TOP_LEVEL.warnText()} example", false),
Expand All @@ -271,13 +271,13 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
@Test
@Tag("DiktatRuleSetProvider")
fun `regression - should correctly handle tags with empty lines`() {
fixAndCompare("KdocFormattingMultilineTagsExpected.kt", "KdocFormattingMultilineTagsTest.kt")
fixAndCompareSmokeTest("KdocFormattingMultilineTagsExpected.kt", "KdocFormattingMultilineTagsTest.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `regression - FP of local variables rule`() {
fixAndCompare("LocalVariableWithOffsetExpected.kt", "LocalVariableWithOffsetTest.kt")
fixAndCompareSmokeTest("LocalVariableWithOffsetExpected.kt", "LocalVariableWithOffsetTest.kt")
org.assertj.core.api.Assertions.assertThat(unfixedLintErrors).noneMatch {
it.ruleId == "diktat-ruleset:local-variables"
}
Expand Down
11 changes: 11 additions & 0 deletions diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ open class FixTestBase(protected val resourceFilePath: String,
)
}

/**
* @param expectedPath path to file with expected result, relative to [resourceFilePath]
* @param testPath path to file with code that will be transformed by formatter, relative to [resourceFilePath]
*/
protected fun fixAndCompareSmokeTest(expectedPath: String, testPath: String) {
Assertions.assertTrue(
testComparatorUnit
.compareFilesFromResources(expectedPath, testPath, true)
)
}

/**
* @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]
Expand Down
11 changes: 11 additions & 0 deletions diktat-rules/src/test/resources/test/smoke/save.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[general]
execCmd="./ktlint -R diktat-1.0.0-rc.2.jar"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we were planning to execute SAVE in CI too?

tags = ["smokeTest"]
description = "SmokeTest"
suiteName = "SmokeTest"

["fix and warn"]
["fix and warn".fix]
execFlags="-F"
["fix and warn".warn]
actualWarningsPattern="(\\w+\\..+):(\\d+):(\\d+): (\\[.*\\].*)$"
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ import kotlinx.browser.document
fun main() {
(document.getElementById("myId") as HTMLElement).click()
}

Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
// ;warn:1:1: [FILE_NAME_MATCH_CLASS] file name is incorrect - it should match with the class described in it if there is the only one class declared: Bug1Expected.kt vs D
package org.cqfn.diktat

// ;warn:4:1: [MISSING_KDOC_TOP_LEVEL] all public and internal top-level classes and functions should have Kdoc: D (cannot be auto-corrected)
// ;warn:7:7: [CLASS_NAME_INCORRECT] class/enum/interface name should be in PascalCase and should contain only latin (ASCII) letters or numbers: D
// ;warn:7:7: [IDENTIFIER_LENGTH] identifier's length is incorrect, it should be in range of [2, 64] symbols: D (cannot be auto-corrected)
class D {
// ;warn:8:5: [MISSING_KDOC_CLASS_ELEMENTS] all public, internal and protected classes, functions and variables inside the class should have Kdoc: x (cannot be auto-corrected)
val x = 0
// ;warn:13:8: [KDOC_NO_EMPTY_TAGS] no empty descriptions in tag blocks are allowed: @return (cannot be auto-corrected)

/**
* @return
*/
fun bar(): Bar {
// ;warn:17:19: [MAGIC_NUMBER] avoid using magic numbers, instead define constants with clear names describing what the magic number means: 42 (cannot be auto-corrected)
val qux = 42
return Bar(qux)
}
Expand All @@ -18,4 +25,3 @@ class D {
fun readFile(foo: Foo) {
var bar: Bar
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@file:Suppress(
"PACKAGE_NAME_MISSING",
"PACKAGE_NAME_INCORRECT_PATH",
"PACKAGE_NAME_INCORRECT_PREFIX"
)

/**
* Dolor sit amet
*/
class Example

Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

@file:Suppress(
"PACKAGE_NAME_MISSING",
"PACKAGE_NAME_INCORRECT_PATH",
"PACKAGE_NAME_INCORRECT_PREFIX"
)
/**
* Dolor sit amet
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,3 @@ fun foo(x : Int
)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@ class Example {
bar(x, y)
)
}

Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,3 @@ private fun foo(node: ASTNode) {

setOf<Object>(IOException(), Properties(), LoggerFactory())
}

Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,3 @@ fun mains() {
}
httpClient.doRequest()
}

Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,3 @@ fun foo() {
val y = "akgjsaujtmaksdkfasakgjsaujtmaksdkfasakgjsaujtmaksdkfasakgjsaujtm" +
" aksdkfasfasakgjsaujtmaksdfasafasakgjsaujtmaksdfasakgjsaujtmaksdfasakgjsaujtmaksdfasakgjsaujtmaksdfasakgjsaujtmaksdkgjsaujtmaksdfasakgjsaujtmaksd"
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ package org.cqfn.diktat
class Some {
val config = Config()
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ val dockerFileAsText =
COPY resources $resourcesPath
RUN /bin/bash
""".trimIndent() // RUN command shouldn't matter because it will be replaced on container creation

Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ fun foo() {
doSmth()
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ fun foo1(bar: Bar): Baz {
fun foo2(bar: Bar): Baz {
// placeholder
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ override fun boo() {
handleTestFile(chunk.map { it.single() }, warnPluginConfig, generalConfig)
}.forEach { listTestResult.addAll(it) }
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ import org.gradle.kotlin.dsl.plugins
plugins {
kotlin("jvm")
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ run {
also {
println("a")
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ run {
also {
println("a")
}

Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,22 @@ class FileComparator {
* @return true in case files are different
* false - in case they are equals
*/
@Suppress("ReturnCount", "FUNCTION_BOOLEAN_PREFIX")
@Suppress(
"ReturnCount",
"FUNCTION_BOOLEAN_PREFIX",
"TOO_LONG_FUNCTION")
fun compareFilesEqual(): Boolean {
try {
val expect = readFile(expectedResultFile.absolutePath)
if (expect.isEmpty()) {
return false
}
val patch = diff(expect, actualResultList)
val regex = (".*// ;warn:(\\d+):(\\d+): (.*)").toRegex()
val expectWithoutWarn = expect.filter { line ->
!line.contains(regex)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
val expectWithoutWarn = expect.filter { line ->
!line.contains(regex)
val expectWithoutWarn = expect.filterNot { line ->
line.contains(regex)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

}
val patch = diff(expectWithoutWarn, actualResultList)

if (patch.deltas.isEmpty()) {
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ class TestComparatorUnit(private val resourceFilePath: String,
/**
* @param expectedResult
* @param testFileStr
* @param trimLastEmptyLine
* @return true if transformed file equals expected result, false otherwise
*/
@Suppress("FUNCTION_BOOLEAN_PREFIX")
fun compareFilesFromResources(expectedResult: String, testFileStr: String): Boolean {
fun compareFilesFromResources(
expectedResult: String,
testFileStr: String,
trimLastEmptyLine: Boolean = false): Boolean {
val expectedPath = javaClass.classLoader.getResource("$resourceFilePath/$expectedResult")
val testPath = javaClass.classLoader.getResource("$resourceFilePath/$testFileStr")
if (testPath == null || expectedPath == null) {
Expand All @@ -43,6 +47,12 @@ class TestComparatorUnit(private val resourceFilePath: String,
copyTestFile.absolutePath
)

if (trimLastEmptyLine) {
val actual: MutableList<String> = mutableListOf()
actual.addAll(actualResult.split("\n").dropLast(1))
return FileComparator(expectedFile, actual).compareFilesEqual()
}

// fixme: actualResult is separated by KtLint#determineLneSeparator, should be split by it here too
return FileComparator(expectedFile, actualResult.split("\n")).compareFilesEqual()
}
Expand Down