Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feature/run-diktat-…
Browse files Browse the repository at this point in the history
…cli-smoke-test#1781
  • Loading branch information
nulls committed Nov 8, 2023
2 parents 67ac878 + d981125 commit e530245
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import com.charleskorn.kaml.YamlConfiguration
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Timeout
Expand Down Expand Up @@ -143,7 +142,6 @@ abstract class DiktatSmokeTestBase {
@Test
@Tag("DiktatRuleSetProvider")
@Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS)
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
fun `smoke test #5`() {
val configFilePath = prepareOverriddenRulesConfig(emptyList(),
mapOf(
Expand All @@ -166,20 +164,19 @@ abstract class DiktatSmokeTestBase {
fixAndCompare(configFilePath, "Example5Expected.kt", "Example5Test.kt")

assertUnfixedLintErrors { unfixedLintErrors ->
Assertions.assertFalse(
unfixedLintErrors.contains(
assertThat(unfixedLintErrors).let { errors ->
errors.doesNotContain(
DiktatError(
line = 1,
col = 1,
ruleId = "diktat-ruleset:${CommentsRule.NAME_ID}",
detail = "${Warnings.COMMENTED_OUT_CODE.warnText()} /*"
)
)
)

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

Expand Down Expand Up @@ -207,7 +204,6 @@ abstract class DiktatSmokeTestBase {
@Test
@Tag("DiktatRuleSetProvider")
@Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS)
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
fun `smoke test #2`() {
val configFilePath = prepareOverriddenRulesConfig(
rulesToDisable = emptyList(),
Expand All @@ -223,18 +219,15 @@ abstract class DiktatSmokeTestBase {
assertThat(unfixedLintErrors).containsExactlyInAnyOrder(
DiktatError(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),
DiktatError(15, 30, "$DIKTAT_RULE_SET_ID:${KdocMethods.NAME_ID}",
"${KDOC_WITHOUT_PARAM_TAG.warnText()} createWithFile (containerName)", true),
DiktatError(31, 21, "$DIKTAT_RULE_SET_ID:${EmptyBlock.NAME_ID}",
"${EMPTY_BLOCK_STRUCTURE_ERROR.warnText()} empty blocks are forbidden unless it is function with override keyword", false)
DiktatError(34, 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),
)
}
}

@Test
@Tag("DiktatRuleSetProvider")
@Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS)
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
fun `smoke test #1`() {
val configFilePath = prepareOverriddenRulesConfig(
rulesToDisable = emptyList(),
Expand All @@ -248,23 +241,18 @@ abstract class DiktatSmokeTestBase {
fixAndCompare(configFilePath, "Example1Expected.kt", "Example1Test.kt")
assertUnfixedLintErrors { unfixedLintErrors ->
assertThat(unfixedLintErrors).containsExactlyInAnyOrder(
DiktatError(1, 1, "$DIKTAT_RULE_SET_ID:${FileNaming.NAME_ID}", "${FILE_NAME_MATCH_CLASS.warnText()} Example1Test.kt vs Example", true),
DiktatError(1, 1, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
DiktatError(3, 13, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_TOP_LEVEL.warnText()} Example", false),
DiktatError(3, 33, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} isValid", false),
DiktatError(6, 16, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} foo", false),
DiktatError(8, 15, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} foo", false),
DiktatError(8, 15, "$DIKTAT_RULE_SET_ID:${KdocMethods.NAME_ID}", "${MISSING_KDOC_ON_FUNCTION.warnText()} foo", false),
/*
* This 2nd `MISSING_KDOC_ON_FUNCTION` is a duplicate caused by
* https://github.com/saveourtool/diktat/issues/1538.
*/
DiktatError(6, 5, "$DIKTAT_RULE_SET_ID:${KdocMethods.NAME_ID}", "${MISSING_KDOC_ON_FUNCTION.warnText()} foo", false),
DiktatError(9, 10, "$DIKTAT_RULE_SET_ID:${EmptyBlock.NAME_ID}", EMPTY_BLOCK_STRUCTURE_ERROR.warnText() +
" empty blocks are forbidden unless it is function with override keyword", false),
DiktatError(12, 17, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
DiktatError(14, 15, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
DiktatError(19, 27, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false)
DiktatError(1, 1, "$DIKTAT_RULE_SET_ID:${FileNaming.NAME_ID}", "${FILE_NAME_MATCH_CLASS.warnText()} Example1Test.kt vs Example", false),
DiktatError(3, 1, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_TOP_LEVEL.warnText()} Example", false),
DiktatError(4, 5, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} isValid", false),
DiktatError(6, 5, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} foo", false),
DiktatError(8, 5, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} foo", false),
DiktatError(8, 5, "$DIKTAT_RULE_SET_ID:${KdocMethods.NAME_ID}", "${MISSING_KDOC_ON_FUNCTION.warnText()} foo", false),
DiktatError(8, 19, "$DIKTAT_RULE_SET_ID:${EmptyBlock.NAME_ID}",
"${EMPTY_BLOCK_STRUCTURE_ERROR.warnText()} empty blocks are forbidden unless it is function with override keyword", false),
DiktatError(13, 8, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
DiktatError(19, 8, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
DiktatError(27, 8, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
DiktatError(36, 8, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
)
}
}
Expand All @@ -286,7 +274,6 @@ abstract class DiktatSmokeTestBase {
@Test
@Tag("DiktatRuleSetProvider")
@Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS)
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
fun `regression - should correctly handle tags with empty lines`() {
fixAndCompare(prepareOverriddenRulesConfig(), "KdocFormattingMultilineTagsExpected.kt", "KdocFormattingMultilineTagsTest.kt")
}
Expand All @@ -301,7 +288,6 @@ abstract class DiktatSmokeTestBase {
@Test
@Tag("DiktatRuleSetProvider")
@Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS)
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
fun `fix can cause long line`() {
val configFilePath = prepareOverriddenRulesConfig(
rulesToDisable = emptyList(),
Expand Down Expand Up @@ -345,7 +331,6 @@ abstract class DiktatSmokeTestBase {

@Test
@Tag("DiktatRuleSetProvider")
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
fun `smoke test with gradle script plugin`() {
fixAndCompare(prepareOverriddenRulesConfig(), "kotlin-library-expected.gradle.kts", "kotlin-library.gradle.kts")
assertUnfixedLintErrors { unfixedLintErrors ->
Expand All @@ -360,7 +345,6 @@ abstract class DiktatSmokeTestBase {

@Test
@Tag("DiktatRuleSetProvider")
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
fun `disable chapters`() {
val configFilePath = prepareOverriddenRulesConfig(
emptyList(),
Expand All @@ -374,15 +358,15 @@ abstract class DiktatSmokeTestBase {
fixAndCompare(configFilePath, "Example1-2Expected.kt", "Example1-2Test.kt")
assertUnfixedLintErrors { unfixedLintErrors ->
assertThat(unfixedLintErrors).containsExactlyInAnyOrder(
DiktatError(1, 1, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
DiktatError(3, 1, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_TOP_LEVEL.warnText()} example", false),
DiktatError(3, 16, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} isValid", false),
DiktatError(6, 5, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} foo", false),
DiktatError(6, 5, "$DIKTAT_RULE_SET_ID:${KdocMethods.NAME_ID}", "${MISSING_KDOC_ON_FUNCTION.warnText()} foo", false),
DiktatError(8, 5, "$DIKTAT_RULE_SET_ID:${KdocComments.NAME_ID}", "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} foo", false),
DiktatError(10, 4, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
DiktatError(13, 9, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
DiktatError(18, 40, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false)
DiktatError(13, 4, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
DiktatError(20, 4, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
DiktatError(28, 4, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
DiktatError(37, 4, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ fun foo() {
(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
?: 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
?: 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 -
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 ||
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 ||
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
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
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Copied from KtLint and open the constructor
*/

@file:Suppress(
"TOO_LONG_FUNCTION",
"PACKAGE_NAME_INCORRECT_PREFIX",
"PACKAGE_NAME_INCORRECT_PATH",
"KDOC_NO_CONSTRUCTOR_PROPERTY",
"MISSING_KDOC_CLASS_ELEMENTS",
"MISSING_KDOC_ON_FUNCTION",
"KDOC_WITHOUT_PARAM_TAG",
"KDOC_WITHOUT_RETURN_TAG",
)

package com.pinterest.ktlint.rule.engine.api

import com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine.Companion.STDIN_FILE
import org.jetbrains.kotlin.konan.file.file
import java.io.File
import java.nio.file.Path
import kotlin.io.path.pathString

/**
* A representation of a block of code. Use one of the factory methods [fromFile], [fromPath], [fromSnippet] or [fromStdin] to instantiate.
*/
public class Code(
public val content: String,
public val fileName: String?,
public val filePath: Path?,
public val script: Boolean,
public val isStdIn: Boolean,
) {
public fun fileNameOrStdin(): String =
if (isStdIn) {
STDIN_FILE
} else {
fileName.orEmpty()
}

public fun filePathOrStdin(): String =
if (isStdIn) {
STDIN_FILE
} else {
filePath?.pathString.orEmpty()
}

public companion object {
/**
* Create [Code] from a [file] containing valid Kotlin code or script. The '.editorconfig' files on the path to [file] are taken
* into account.
*/
public fun fromFile(file: File): Code =
Code(
content = file.readText(),
fileName = file.name,
filePath = file.toPath(),
script = file.name.endsWith(".kts", ignoreCase = true),
isStdIn = false,
)

/**
* Create [Code] from a [path] to a file containing valid Kotlin code or script. The '.editorconfig' files on the path to [file] are
* taken into account. This method is intended to be used in unit tests. In order to work with the Ktlint test file system it needs
* to make additional call to get the file system which makes it slower compared to [fromFile]. Prefer to use [fromFile].
*/
public fun fromPath(path: Path): Code {
// Resolve the file based on the file system of the original path given.
val file =
path
.fileSystem
.file(path.pathString)
return Code(
content = file.readStrings().joinToString(separator = "\n"),
fileName = file.name,
filePath = path,
script = file.name.endsWith(".kts", ignoreCase = true),
isStdIn = false,
)
}

/**
* The [content] represent a valid piece of Kotlin code or Kotlin script. The '.editorconfig' files on the filesystem are ignored as
* the snippet is not associated with a file path. Use [Code.fromFile] for scanning a file while at the same time respecting the
* '.editorconfig' files on the path to the file.
*/
public fun fromSnippet(
content: String,
script: Boolean = false,
): Code =
Code(
content = content,
filePath = null,
fileName = null,
script = script,
isStdIn = true,
)

/**
* Create [Code] by reading the snippet from 'stdin'. No '.editorconfig' are taken into account. The '.editorconfig' files on the
* filesystem are ignored as the snippet is not associated with a file path. Use [Code.fromFile] for scanning a file while at the
* same time respecting the '.editorconfig' files on the path to the file.
*/
public fun fromStdin(): Code = fromSnippet(String(System.`in`.readBytes()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine
import com.pinterest.ktlint.rule.engine.api.LintError
import java.nio.file.Path

private typealias FormatCallback = (LintError, Boolean) -> Unit
private typealias LintCallback = (LintError) -> Unit

/**
Expand All @@ -23,12 +22,12 @@ class DiktatProcessorFactoryImpl : DiktatProcessorFactory {
override fun fix(
file: Path,
callback: DiktatCallback,
): String = ktLintRuleEngine.format(file.toKtLint(), callback.toKtLintForFormat())
): String = ktLintRuleEngine.formatSilentlyThenLint(file.toKtLint(), callback.toKtLintForLint())
override fun fix(
code: String,
isScript: Boolean,
callback: DiktatCallback
): String = ktLintRuleEngine.format(code.toKtLint(isScript), callback.toKtLintForFormat())
): String = ktLintRuleEngine.formatSilentlyThenLint(code.toKtLint(isScript), callback.toKtLintForLint())
override fun check(
file: Path,
callback: DiktatCallback,
Expand All @@ -48,12 +47,26 @@ class DiktatProcessorFactoryImpl : DiktatProcessorFactory {

private fun String.toKtLint(isScript: Boolean): Code = Code.fromSnippet(this, isScript)

private fun DiktatCallback.toKtLintForFormat(): FormatCallback = { error, isCorrected ->
this(error.wrap(), isCorrected)
}

private fun DiktatCallback.toKtLintForLint(): LintCallback = { error ->
this(error.wrap(), false)
}

private fun KtLintRuleEngine.formatSilentlyThenLint(
code: Code,
callback: LintCallback,
): String {
val formatResult = format(code)
lint(
code = Code(
content = formatResult,
fileName = code.fileName,
filePath = code.filePath,
script = code.script,
isStdIn = code.isStdIn,
),
callback = callback,
)
return formatResult
}
}
}
Loading

0 comments on commit e530245

Please sign in to comment.