Skip to content

Commit f533039

Browse files
committed
Fixed smoke tests
### What's done: - fixed rule PACKAGE_NAME_MISSING It closes #1737
1 parent c7c539a commit f533039

File tree

7 files changed

+130
-18
lines changed

7 files changed

+130
-18
lines changed

diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter1/PackageNaming.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import org.jetbrains.kotlin.KtNodeTypes.DOT_QUALIFIED_EXPRESSION
1818
import org.jetbrains.kotlin.KtNodeTypes.FILE_ANNOTATION_LIST
1919
import org.jetbrains.kotlin.KtNodeTypes.PACKAGE_DIRECTIVE
2020
import org.jetbrains.kotlin.KtNodeTypes.REFERENCE_EXPRESSION
21+
import org.jetbrains.kotlin.com.intellij.lang.ASTFactory
2122
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
2223
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
2324
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
@@ -255,7 +256,7 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(
255256
packageDirectiveParent.removeChild(packageDirectiveNode)
256257
packageDirectiveParent.addChild(newPackageDirective, addBefore)
257258
if (newPackageDirective.treePrev.elementType != WHITE_SPACE) {
258-
packageDirectiveParent.addChild(PsiWhiteSpaceImpl("\n"), newPackageDirective)
259+
packageDirectiveParent.addChild(ASTFactory.whitespace("\n"), newPackageDirective)
259260
}
260261
} else {
261262
packageDirectiveParent.replaceChild(packageDirectiveNode, newPackageDirective)

diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter1/PackagePathFixTest.kt

+36
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.saveourtool.diktat.ruleset.chapter1
22

33
import com.saveourtool.diktat.common.config.rules.RulesConfig
4+
import com.saveourtool.diktat.ruleset.constants.Warnings
45
import com.saveourtool.diktat.ruleset.rules.chapter1.PackageNaming
56
import com.saveourtool.diktat.util.FixTestBase
67

78
import generated.WarningNames
9+
import org.junit.jupiter.api.Assertions
810
import org.junit.jupiter.api.Tag
911
import org.junit.jupiter.api.Test
12+
import org.junit.jupiter.api.io.TempDir
13+
import java.nio.file.Path
1014

1115
class PackagePathFixTest : FixTestBase(
1216
"test/paragraph1/naming/package/src/main/kotlin",
@@ -68,4 +72,36 @@ class PackagePathFixTest : FixTestBase(
6872
fun `fix missing package name with a proper location`() {
6973
fixAndCompare("com/saveourtool/diktat/some/name/FixMissingExpected.kt", "com/saveourtool/diktat/some/name/FixMissingTest.kt")
7074
}
75+
76+
@Test
77+
@Tag(WarningNames.PACKAGE_NAME_MISSING)
78+
fun `several empty lines after package`(@TempDir tempDir: Path) {
79+
fixAndCompareContent(
80+
"""
81+
/**
82+
* @param bar
83+
* @return something
84+
*/
85+
fun foo1(bar: Bar): Baz {
86+
// placeholder
87+
}
88+
""".trimIndent(),
89+
"""
90+
package com.saveourtool.diktat
91+
92+
/**
93+
* @param bar
94+
* @return something
95+
*/
96+
fun foo1(bar: Bar): Baz {
97+
// placeholder
98+
}
99+
""".trimIndent(),
100+
tempDir = tempDir,
101+
)
102+
.run {
103+
Assertions.assertEquals(expectedContentWithoutWarns, actualContent)
104+
}
105+
106+
}
71107
}

diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter2/KdocFormattingFixTest.kt

+48
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import com.saveourtool.diktat.ruleset.rules.chapter2.kdoc.KdocFormatting
44
import com.saveourtool.diktat.util.FixTestBase
55

66
import generated.WarningNames
7+
import org.junit.jupiter.api.Assertions
78
import org.junit.jupiter.api.Tag
89
import org.junit.jupiter.api.Tags
910
import org.junit.jupiter.api.Test
11+
import org.junit.jupiter.api.io.TempDir
12+
import java.nio.file.Path
1013

1114
class KdocFormattingFixTest : FixTestBase("test/paragraph2/kdoc/", ::KdocFormatting) {
1215
@Test
@@ -62,4 +65,49 @@ class KdocFormattingFixTest : FixTestBase("test/paragraph2/kdoc/", ::KdocFormatt
6265
fun `KdocFormatting - sort order`() {
6366
fixAndCompare("KdocFormattingOrderExpected.kt", "KdocFormattingOrderTest.kt")
6467
}
68+
69+
@Test
70+
@Tag(WarningNames.KDOC_NO_EMPTY_TAGS)
71+
fun `several empty lines after package`(@TempDir tempDir: Path) {
72+
fixAndCompareContent(
73+
actualContent = """
74+
/**
75+
* @param bar lorem ipsum
76+
*
77+
* dolor sit amet
78+
*/
79+
fun foo1(bar: Bar): Baz {
80+
// placeholder
81+
}
82+
83+
/**
84+
* @param bar lorem ipsum
85+
*
86+
* dolor sit amet
87+
*
88+
*/
89+
fun foo2(bar: Bar): Baz {
90+
// placeholder
91+
}
92+
""".trimIndent(),
93+
expectedContent = """
94+
package com.saveourtool.diktat
95+
96+
/**
97+
* @param bar
98+
* @return
99+
*/
100+
fun foo1(bar: Bar): Baz {
101+
// placeholder
102+
}
103+
""".trimIndent(),
104+
tempDir = tempDir,
105+
)
106+
.run {
107+
Assertions.assertEquals(expectedContentWithoutWarns, actualContent)
108+
}
109+
110+
}
111+
112+
65113
}

diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTest.kt

+28-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package com.saveourtool.diktat.ruleset.smoke
22

33
import com.saveourtool.diktat.api.DiktatError
44
import com.saveourtool.diktat.ktlint.format
5+
import com.saveourtool.diktat.ktlint.lint
56
import com.saveourtool.diktat.ruleset.rules.DiktatRuleConfigReaderImpl
67
import com.saveourtool.diktat.ruleset.rules.DiktatRuleSetFactoryImpl
78
import com.saveourtool.diktat.test.framework.processing.TestComparatorUnit
9+
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
810
import org.junit.jupiter.api.Assertions
911
import org.junit.jupiter.api.BeforeEach
1012
import java.nio.file.Path
1113
import kotlin.io.path.inputStream
14+
import kotlin.io.path.readText
1215

1316
/**
1417
* Test for [DiktatRuleSetFactoryImpl] in autocorrect mode as a whole. All rules are applied to a file.
@@ -23,11 +26,21 @@ class DiktatSmokeTest : DiktatSmokeTestBase() {
2326
expected: String,
2427
test: String,
2528
) {
26-
Assertions.assertTrue(
27-
getTestComparatorUnit(config)
28-
.compareFilesFromResources(expected, test)
29-
.isSuccessful
30-
)
29+
val result = getTestComparatorUnit(config)
30+
.compareFilesFromResources(expected, test)
31+
if (!result.isSuccessful) {
32+
Assertions.assertAll(
33+
{
34+
assertUnfixedLintErrors {
35+
org.assertj.core.api.Assertions.assertThat(unfixedLintErrors).isEmpty()
36+
}
37+
},
38+
{
39+
Assertions.assertEquals(result.expectedContentWithoutWarns, result.actualContent)
40+
}
41+
)
42+
}
43+
Assertions.assertTrue(result.isSuccessful)
3144
}
3245

3346
@BeforeEach
@@ -42,7 +55,7 @@ class DiktatSmokeTest : DiktatSmokeTestBase() {
4255
private fun getTestComparatorUnit(config: Path) = TestComparatorUnit(
4356
resourceFilePath = RESOURCE_FILE_PATH,
4457
function = { testFile ->
45-
format(
58+
lint(
4659
ruleSetSupplier = {
4760
val diktatRuleConfigReader = DiktatRuleConfigReaderImpl()
4861
val diktatRuleSetFactory = DiktatRuleSetFactoryImpl()
@@ -51,6 +64,15 @@ class DiktatSmokeTest : DiktatSmokeTestBase() {
5164
file = testFile,
5265
cb = { lintError, _ -> unfixedLintErrors.add(lintError) },
5366
)
67+
format(
68+
ruleSetSupplier = {
69+
val diktatRuleConfigReader = DiktatRuleConfigReaderImpl()
70+
val diktatRuleSetFactory = DiktatRuleSetFactoryImpl()
71+
diktatRuleSetFactory(diktatRuleConfigReader(config.inputStream()))
72+
},
73+
file = testFile,
74+
cb = { _, _ -> },
75+
)
5476
},
5577
)
5678
}

diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt

+12-8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import org.junit.jupiter.api.Disabled
4545
import org.junit.jupiter.api.Tag
4646
import org.junit.jupiter.api.Test
4747
import org.junit.jupiter.api.Timeout
48+
import org.junit.jupiter.api.io.TempDir
4849
import java.io.File
4950
import java.nio.file.Path
5051
import java.nio.file.Paths
@@ -136,7 +137,6 @@ abstract class DiktatSmokeTestBase {
136137
@Test
137138
@Tag("DiktatRuleSetProvider")
138139
@Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS)
139-
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
140140
fun `smoke test #5`() {
141141
val configFilePath = prepareOverriddenRulesConfig(emptyList(),
142142
mapOf(
@@ -200,7 +200,6 @@ abstract class DiktatSmokeTestBase {
200200
@Test
201201
@Tag("DiktatRuleSetProvider")
202202
@Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS)
203-
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
204203
fun `smoke test #2`() {
205204
val configFilePath = prepareOverriddenRulesConfig(
206205
rulesToDisable = emptyList(),
@@ -227,7 +226,6 @@ abstract class DiktatSmokeTestBase {
227226
@Test
228227
@Tag("DiktatRuleSetProvider")
229228
@Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS)
230-
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
231229
fun `smoke test #1`() {
232230
val configFilePath = prepareOverriddenRulesConfig(
233231
rulesToDisable = emptyList(),
@@ -279,9 +277,14 @@ abstract class DiktatSmokeTestBase {
279277
@Test
280278
@Tag("DiktatRuleSetProvider")
281279
@Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS)
282-
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
283280
fun `regression - should correctly handle tags with empty lines`() {
284-
fixAndCompare(prepareOverriddenRulesConfig(), "KdocFormattingMultilineTagsExpected.kt", "KdocFormattingMultilineTagsTest.kt")
281+
fixAndCompare(
282+
prepareOverriddenRulesConfig(
283+
// listOf(Warnings.HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE, Warnings.KDOC_WITHOUT_RETURN_TAG),
284+
),
285+
expected = "KdocFormattingMultilineTagsExpected.kt",
286+
test = "KdocFormattingMultilineTagsTest.kt",
287+
)
285288
}
286289

287290
@Test
@@ -294,7 +297,6 @@ abstract class DiktatSmokeTestBase {
294297
@Test
295298
@Tag("DiktatRuleSetProvider")
296299
@Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS)
297-
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
298300
fun `fix can cause long line`() {
299301
val configFilePath = prepareOverriddenRulesConfig(
300302
rulesToDisable = emptyList(),
@@ -338,7 +340,6 @@ abstract class DiktatSmokeTestBase {
338340

339341
@Test
340342
@Tag("DiktatRuleSetProvider")
341-
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
342343
fun `smoke test with gradle script plugin`() {
343344
fixAndCompare(prepareOverriddenRulesConfig(), "kotlin-library-expected.gradle.kts", "kotlin-library.gradle.kts")
344345
assertUnfixedLintErrors { unfixedLintErrors ->
@@ -353,7 +354,6 @@ abstract class DiktatSmokeTestBase {
353354

354355
@Test
355356
@Tag("DiktatRuleSetProvider")
356-
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
357357
fun `disable chapters`() {
358358
val configFilePath = prepareOverriddenRulesConfig(
359359
emptyList(),
@@ -394,6 +394,10 @@ abstract class DiktatSmokeTestBase {
394394
private const val TEST_TIMEOUT_SECONDS = 30L
395395
private val tmpFiles: MutableList<File> = mutableListOf()
396396

397+
@TempDir
398+
@JvmStatic
399+
internal var tempDir: Path? = null
400+
397401
@BeforeAll
398402
@JvmStatic
399403
@Suppress("AVOID_NULL_CHECKS")

diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/KdocFormattingMultilineTagsExpected.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package com.saveourtool.diktat
55
* @param bar lorem ipsum
66
*
77
* dolor sit amet
8-
// ;warn:4: [KDOC_NO_EMPTY_TAGS] no empty descriptions in tag blocks are allowed: @return (cannot be auto-corrected) (diktat-ruleset:kdoc-formatting)
8+
// ;warn:1: [KDOC_NO_EMPTY_TAGS] no empty descriptions in tag blocks are allowed: @return (cannot be auto-corrected) (diktat-ruleset:kdoc-formatting)
99
* @return
1010
*/
1111
fun foo1(bar: Bar): Baz {
@@ -16,7 +16,7 @@ fun foo1(bar: Bar): Baz {
1616
* @param bar lorem ipsum
1717
*
1818
* dolor sit amet
19-
// ;warn:4: [KDOC_NO_EMPTY_TAGS] no empty descriptions in tag blocks are allowed: @return (cannot be auto-corrected) (diktat-ruleset:kdoc-formatting)
19+
// ;warn:1: [KDOC_NO_EMPTY_TAGS] no empty descriptions in tag blocks are allowed: @return (cannot be auto-corrected) (diktat-ruleset:kdoc-formatting)
2020
* @return
2121
*/
2222
fun foo2(bar: Bar): Baz {

diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineExpected.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ fun foo() {
44
(1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or 12 or 13 or 14 or 15 or 16 or 17 or 18 or 19 or 20 or 21 or 22 or 23 or 24 or 25 or 26 or 27 or 28 or 29 or 30 or 31
55
?: 32 or 33 or 34 or 35 or 36 or 37 or 38 or 39 ?: 40 or 41 or 42 or 43 or 44 or 45 or 46 or 47 or 48 or 49 or 50 or 51 or 52 or 53 + 54 or 55 or 56 or 57 or 58 or 59
66
?: 60 or 61 or 62 or 63 or 64 or 65 or 66 - 67 or 68 or 69 or 70 or 1 + 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or 12 or 13 or 14 or 15 or 16 or 17 or 18 + 19 -
7-
20 or 21 or 22 or 23 or 24 or 25 or 26 or 27 or 28 or 29 or 30 or 31 or 32 or 33 or 34 or 35 or 36 or 37 or 38 or 39 or 40 or 41 || 42 or 43 or 44 or 45 or 46 or 47 && 48 or 49 ||
7+
20 or 21 or 22 or 23 or 24 or 25 or 26 or 27 or 28 or 29 or 30 or 31 or 32 or 33 or 34 or 35 or 36 or 37 or 38 or 39 or 40 or 41 || 42 or 43 or 44 or 45 or 46 or 47 &&
8+
48 or 49 ||
89
50 or 51 or 52 or 53 or 54 or 55 or 56 or 57 or 58 or 59 or 60 or 61 or 62 or 63 or 64 or 65 or 66 or 67 or 68 or 69 or 70 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or
910
12 or 13 or 14 or 15 or 16 or 17 or 18 or 19 or 20 or 21 or 22 or 23 or 24 or 25 or 26 or 27 or 28 or 29 or 30 or 31 or 32 or 33 or 34 or 35 or 36 or 37 or 38 or 39 or 40 or 41 or
1011
42 or 43 or 44 or 45 or 46 or 47 or 48 or 49 or 50 or 51 or 52 or 53 or 54 or 55 or 56 or 57 or 58 or 59 or 60 or 61 or 62 or 63 or 64 or 65 or 66 or 67 or 68 or 69 or 70)

0 commit comments

Comments
 (0)