From af7ea9f3ab93f4ec869f90270b9c84ceb87cb9b1 Mon Sep 17 00:00:00 2001 From: Jeremy Mailen Date: Wed, 11 Dec 2024 23:24:09 -0800 Subject: [PATCH] Test adding custom rule sets (#421) * Test adding custom rule sets Rule sets can be configured by adding them to the `ktlint` dependency configuration * Don't fail test project build on lint * Fix test --- .github/workflows/default.yml | 4 ++-- test-project/build.gradle.kts | 3 +-- test-project/rules/build.gradle.kts | 7 ++++++ .../org/jmailen/ktlint/rule/NoVarRule.kt | 23 +++++++++++++++++++ .../jmailen/ktlint/rule/RuleSetProvider.kt | 16 +++++++++++++ ...int.cli.ruleset.core.api.RuleSetProviderV3 | 1 + test-project/service/build.gradle.kts | 13 +++++++++++ .../gradle/kotlinter/sample/CustomNoVar.kt | 5 ++++ .../gradle/kotlinter/sample/OpSpacing.kt | 0 test-project/settings.gradle.kts | 3 +++ .../kotlinter/sample/EmptyClassBodyClass.kt | 4 ---- 11 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 test-project/rules/build.gradle.kts create mode 100644 test-project/rules/src/main/kotlin/org/jmailen/ktlint/rule/NoVarRule.kt create mode 100644 test-project/rules/src/main/kotlin/org/jmailen/ktlint/rule/RuleSetProvider.kt create mode 100644 test-project/rules/src/main/resources/META-INF/services/com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3 create mode 100644 test-project/service/build.gradle.kts create mode 100644 test-project/service/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/CustomNoVar.kt rename test-project/{ => service}/src/test/kotlin/org/jmailen/gradle/kotlinter/sample/OpSpacing.kt (100%) delete mode 100644 test-project/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/EmptyClassBodyClass.kt diff --git a/.github/workflows/default.yml b/.github/workflows/default.yml index 8c18957d..a7b7f97d 100644 --- a/.github/workflows/default.yml +++ b/.github/workflows/default.yml @@ -72,8 +72,8 @@ jobs: - name: Assert fixes run: | - cd test-project/build/reports/ktlint - grep 'no-empty-class-body' main-format.txt | grep -q 'EmptyClassBodyClass.kt:3:27' + cd test-project/service/build/reports/ktlint + grep 'kotlinter-test-rules:no-var' main-lint.txt | grep -q 'CustomNoVar.kt:4:5' grep 'op-spacing' test-format.txt | grep -q 'OpSpacing.kt:5:16' - name: Upload reports diff --git a/test-project/build.gradle.kts b/test-project/build.gradle.kts index d5fbdfd6..2682285e 100644 --- a/test-project/build.gradle.kts +++ b/test-project/build.gradle.kts @@ -1,4 +1,3 @@ plugins { - kotlin("jvm") version "2.1.0" - id("org.jmailen.kotlinter") + kotlin("jvm") version "2.1.0" apply false } diff --git a/test-project/rules/build.gradle.kts b/test-project/rules/build.gradle.kts new file mode 100644 index 00000000..4f6f62dd --- /dev/null +++ b/test-project/rules/build.gradle.kts @@ -0,0 +1,7 @@ +plugins { + kotlin("jvm") +} + +dependencies { + compileOnly("com.pinterest.ktlint:ktlint-cli-ruleset-core:1.5.0") +} diff --git a/test-project/rules/src/main/kotlin/org/jmailen/ktlint/rule/NoVarRule.kt b/test-project/rules/src/main/kotlin/org/jmailen/ktlint/rule/NoVarRule.kt new file mode 100644 index 00000000..937951e9 --- /dev/null +++ b/test-project/rules/src/main/kotlin/org/jmailen/ktlint/rule/NoVarRule.kt @@ -0,0 +1,23 @@ +package org.jmailen.ktlint.rule + +import com.pinterest.ktlint.rule.engine.core.api.* +import org.jetbrains.kotlin.com.intellij.lang.ASTNode + +class NoVarRule : + Rule( + ruleId = RuleId("$CUSTOM_RULE_SET_ID:no-var"), + about = About( + maintainer = "jmailen", + repositoryUrl = "https://gtihub.com/jeremymailen/kotlinter-gradle" + ) + ), + RuleAutocorrectApproveHandler { + override fun beforeVisitChildNodes( + node: ASTNode, + emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> AutocorrectDecision + ) { + if (node.elementType == ElementType.VAR_KEYWORD) { + emit(node.startOffset, "Use val instead of var", false) + } + } +} diff --git a/test-project/rules/src/main/kotlin/org/jmailen/ktlint/rule/RuleSetProvider.kt b/test-project/rules/src/main/kotlin/org/jmailen/ktlint/rule/RuleSetProvider.kt new file mode 100644 index 00000000..f698e7e3 --- /dev/null +++ b/test-project/rules/src/main/kotlin/org/jmailen/ktlint/rule/RuleSetProvider.kt @@ -0,0 +1,16 @@ +package org.jmailen.ktlint.rule + +import com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3 +import com.pinterest.ktlint.rule.engine.core.api.RuleProvider +import com.pinterest.ktlint.rule.engine.core.api.RuleSetId + +internal val CUSTOM_RULE_SET_ID = "kotlinter-test-rules" + +class CustomRuleSetProvider : RuleSetProviderV3(RuleSetId(CUSTOM_RULE_SET_ID)) { + override fun getRuleProviders(): Set = + setOf( + RuleProvider { + NoVarRule() + }, + ) +} diff --git a/test-project/rules/src/main/resources/META-INF/services/com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3 b/test-project/rules/src/main/resources/META-INF/services/com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3 new file mode 100644 index 00000000..683baa14 --- /dev/null +++ b/test-project/rules/src/main/resources/META-INF/services/com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3 @@ -0,0 +1 @@ +org.jmailen.ktlint.rule.CustomRuleSetProvider diff --git a/test-project/service/build.gradle.kts b/test-project/service/build.gradle.kts new file mode 100644 index 00000000..f01fc325 --- /dev/null +++ b/test-project/service/build.gradle.kts @@ -0,0 +1,13 @@ +plugins { + kotlin("jvm") + id("org.jmailen.kotlinter") +} + +dependencies { + ktlint(project(":rules")) +} + +kotlinter { + reporters = arrayOf("plain", "checkstyle") + ignoreLintFailures = true +} diff --git a/test-project/service/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/CustomNoVar.kt b/test-project/service/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/CustomNoVar.kt new file mode 100644 index 00000000..5952ecef --- /dev/null +++ b/test-project/service/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/CustomNoVar.kt @@ -0,0 +1,5 @@ +package org.jmailen.gradle.kotlinter.sample + +class CustomNoVar { + var test: String = "test" +} diff --git a/test-project/src/test/kotlin/org/jmailen/gradle/kotlinter/sample/OpSpacing.kt b/test-project/service/src/test/kotlin/org/jmailen/gradle/kotlinter/sample/OpSpacing.kt similarity index 100% rename from test-project/src/test/kotlin/org/jmailen/gradle/kotlinter/sample/OpSpacing.kt rename to test-project/service/src/test/kotlin/org/jmailen/gradle/kotlinter/sample/OpSpacing.kt diff --git a/test-project/settings.gradle.kts b/test-project/settings.gradle.kts index e8bbe0f0..c5f7e923 100644 --- a/test-project/settings.gradle.kts +++ b/test-project/settings.gradle.kts @@ -10,3 +10,6 @@ dependencyResolutionManagement { plugins { id("org.gradle.toolchains.foojay-resolver-convention") version "0.7.0" } + +include(":service") +include(":rules") diff --git a/test-project/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/EmptyClassBodyClass.kt b/test-project/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/EmptyClassBodyClass.kt deleted file mode 100644 index cc7fbd3b..00000000 --- a/test-project/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/EmptyClassBodyClass.kt +++ /dev/null @@ -1,4 +0,0 @@ -package org.jmailen.gradle.kotlinter.sample - -class EmptyClassBodyClass { -}