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

Fixed smoke tests #1769

Merged
merged 27 commits into from
Nov 7, 2023
Merged

Fixed smoke tests #1769

merged 27 commits into from
Nov 7, 2023

Conversation

nulls
Copy link
Member

@nulls nulls commented Oct 27, 2023

What's done:

  • fixed rule PACKAGE_NAME_MISSING

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

@github-actions
Copy link
Contributor

github-actions bot commented Oct 27, 2023

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

   164 files  ±0     164 suites  ±0   5m 14s ⏱️ - 2m 40s
1 389 tests +1  1 372 ✔️ +15  17 💤  - 14  0 ±0 
2 768 runs  +1  2 751 ✔️ +15  17 💤  - 14  0 ±0 

Results for commit a1a32ce. ± Comparison against base commit c1c3cab.

♻️ This comment has been updated with latest results.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 27, 2023

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

   164 files  ±0     164 suites  ±0   7m 4s ⏱️ +47s
1 389 tests +1  1 353 ✔️ +8  36 💤  - 7  0 ±0 
2 768 runs  +1  2 732 ✔️ +8  36 💤  - 7  0 ±0 

Results for commit a1a32ce. ± Comparison against base commit c1c3cab.

♻️ This comment has been updated with latest results.

@nulls
Copy link
Member Author

nulls commented Nov 2, 2023

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())
+        }
     }
 }

@nulls nulls self-assigned this Nov 3, 2023
Copy link

codecov bot commented Nov 3, 2023

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%) ⬆️
...ool/diktat/ruleset/rules/chapter1/PackageNaming.kt 87.40% <62.50%> (-1.03%) ⬇️
...urtool/diktat/ktlint/DiktatProcessorFactoryImpl.kt 0.00% <0.00%> (ø)
...otlin/com/pinterest/ktlint/rule/engine/api/Code.kt 36.58% <36.58%> (ø)

@nulls nulls marked this pull request as ready for review November 3, 2023 13:01
@nulls nulls enabled auto-merge (squash) November 3, 2023 13:01
@nulls nulls mentioned this pull request Nov 7, 2023
### What's done:
- added a duplicated Code from KTLint
- added fix before calling warn in warnAndFix scenario
Copy link
Member

@orchestr7 orchestr7 left a comment

Choose a reason for hiding this comment

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

tests passed, good

@nulls nulls merged commit d981125 into master Nov 7, 2023
32 of 33 checks passed
@nulls nulls deleted the feature/fix-smoke-test#1737 branch November 7, 2023 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants