saveourtool / diktat

Strict coding standard for Kotlin and a custom set of rules for detecting code smells, code style issues and bugs
https://diktat.saveourtool.com
MIT License
521 stars 39 forks source link

Fixed smoke tests #1769

Closed nulls closed 10 months ago

nulls commented 10 months ago

What's done:

It closes #1737 It closes #1538 It closes #1774

github-actions[bot] commented 10 months ago

JUnit Tests (Windows, EnricoMi/publish-unit-test-result-action@v2)

   164 files  ±0     164 suites  ±0   5m 14s :stopwatch: - 2m 40s 1 389 tests +1  1 372 :heavy_check_mark: +15  17 :zzz:  - 14  0 :x: ±0  2 768 runs  +1  2 751 :heavy_check_mark: +15  17 :zzz:  - 14  0 :x: ±0 

Results for commit a1a32ce9. ± Comparison against base commit c1c3cab8.

:recycle: This comment has been updated with latest results.

github-actions[bot] commented 10 months ago

JUnit Tests (macOS, EnricoMi/publish-unit-test-result-action@v2)

   164 files  ±0     164 suites  ±0   7m 4s :stopwatch: +47s 1 389 tests +1  1 353 :heavy_check_mark: +8  36 :zzz:  - 7  0 :x: ±0  2 768 runs  +1  2 732 :heavy_check_mark: +8  36 :zzz:  - 7  0 :x: ±0 

Results for commit a1a32ce9. ± Comparison against base commit c1c3cab8.

:recycle: This comment has been updated with latest results.

nulls commented 10 months ago

Can be useful for testing:

Index: diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/KtLintRuleWrapper.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/KtLintRuleWrapper.kt b/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/KtLintRuleWrapper.kt
--- a/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/KtLintRuleWrapper.kt   (revision 39de04c9bc487b601dd1081f70d1843c974fe558)
+++ b/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/KtLintRuleWrapper.kt   (date 1698919199521)
@@ -8,6 +8,13 @@
 import com.pinterest.ktlint.rule.engine.core.api.RuleId
 import com.pinterest.ktlint.rule.engine.core.api.RuleProvider
 import org.jetbrains.kotlin.com.intellij.lang.ASTNode
+import org.jetbrains.kotlin.psi.stubs.elements.KtFileElementType
+import java.nio.file.Paths
+import java.util.concurrent.atomic.AtomicInteger
+import kotlin.io.path.listDirectoryEntries
+import kotlin.io.path.name
+import kotlin.io.path.readText
+import kotlin.io.path.writeText

 private typealias EmitType = (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit

@@ -27,11 +34,37 @@
         node: ASTNode,
         autoCorrect: Boolean,
         emit: EmitType,
-    ) = rule.invoke(node, autoCorrect) { offset, errorMessage, canBeAutoCorrected ->
-        emit.invoke(offset, errorMessage.correctErrorDetail(canBeAutoCorrected), canBeAutoCorrected)
-    }
+    ) {
+        node.dumpNodeIfRequired(rule, true)
+        rule.invoke(node, autoCorrect) { offset, errorMessage, canBeAutoCorrected ->
+            emit.invoke(offset, errorMessage.correctErrorDetail(canBeAutoCorrected), canBeAutoCorrected)
+        }

+    }
+
+    override fun afterVisitChildNodes(node: ASTNode, autoCorrect: Boolean, emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) {
+        super.afterVisitChildNodes(node, autoCorrect, emit)
+        node.dumpNodeIfRequired(rule, false)
+    }
+
     companion object {
+        private val counter = AtomicInteger(1000)
+
+        private val folder = Paths.get("d:\\projects\\diktat\\test")
+
+        private fun ASTNode.dumpNodeIfRequired(rule: DiktatRule, isBefore: Boolean) {
+            if (elementType == KtFileElementType.INSTANCE) {
+                val newContent = prettyPrint()
+                val latestContent = folder.listDirectoryEntries()
+                    .find { it.name.startsWith("${counter.get()}") }
+                    ?.readText()
+                if (latestContent != newContent) {
+                    folder.resolve("${counter.incrementAndGet()}_${if (isBefore) "before" else "after"}_${rule.javaClass.simpleName}.txt")
+                        .writeText(newContent)
+                }
+            }
+        }
+
         private val about: About = About(
             maintainer = "Diktat",
             repositoryUrl = "https://github.com/saveourtool/diktat",
@@ -73,5 +106,29 @@
          * @return a rule to which a logic is delegated
          */
         internal fun Rule.unwrap(): DiktatRule = (this as? KtLintRuleWrapper)?.rule ?: error("Provided rule ${javaClass.simpleName} is not wrapped by diktat")
+
+
+        /**
+         * Converts this AST node and all its children to pretty string representation
+         */
+        @Suppress("AVOID_NESTED_FUNCTIONS")
+        fun ASTNode.prettyPrint(level: Int = 0, maxLevel: Int = -1): String {
+            /**
+             * AST operates with \n only, so we need to build the whole string representation and then change line separator
+             */
+            fun ASTNode.doPrettyPrint(level: Int, maxLevel: Int): String {
+                val result = StringBuilder("${this.elementType}: \"${this.text}\"").append('\n')
+                if (maxLevel != 0) {
+                    this.getChildren(null).forEach { child ->
+                        result.append(
+                            "${"-".repeat(level + 1)} " +
+                                    child.doPrettyPrint(level + 1, maxLevel - 1)
+                        )
+                    }
+                }
+                return result.toString()
+            }
+            return doPrettyPrint(level, maxLevel).replace("\n", System.lineSeparator())
+        }
     }
 }
codecov[bot] commented 10 months ago

Codecov Report

Merging #1769 (a1a32ce) into master (c1c3cab) will decrease coverage by 0.27%. The diff coverage is 40.57%.

@@             Coverage Diff              @@
##             master    #1769      +/-   ##
============================================
- Coverage     78.25%   77.99%   -0.27%     
- Complexity     2409     2415       +6     
============================================
  Files           126      127       +1     
  Lines          8453     8511      +58     
  Branches       2149     2152       +3     
============================================
+ Hits           6615     6638      +23     
- Misses          880      914      +34     
- Partials        958      959       +1     
Files Coverage Δ
.../diktat/ruleset/rules/chapter2/kdoc/KdocMethods.kt 88.09% <100.00%> (+0.29%) :arrow_up:
...ool/diktat/ruleset/rules/chapter1/PackageNaming.kt 87.40% <62.50%> (-1.03%) :arrow_down:
...urtool/diktat/ktlint/DiktatProcessorFactoryImpl.kt 0.00% <0.00%> (ø)
...otlin/com/pinterest/ktlint/rule/engine/api/Code.kt 36.58% <36.58%> (ø)