diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 981a3fae74..bdb1df0d75 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -107,7 +107,7 @@ jobs: - name: Run diKTat from cli continue-on-error: true run: | - java -jar ktlint -R ${{ env.DIKTAT_JAR }} --disabled_rules=standard 'examples/maven/src/main/kotlin/Test.kt' &>out.txt + java -jar ktlint -R ${{ env.DIKTAT_JAR }} --disabled_rules=standard,experimental,test,custom 'examples/maven/src/main/kotlin/Test.kt' &>out.txt shell: bash - name: Check output @@ -121,14 +121,14 @@ jobs: continue-on-error: true if: ${{ runner.os == 'Linux' || runner.os == 'macOS' }} run: | - java -jar ktlint -R ${{ env.DIKTAT_JAR }} --disabled_rules=standard "$PWD/examples/maven/src/main/kotlin/Test.kt" &>out.txt + java -jar ktlint -R ${{ env.DIKTAT_JAR }} --disabled_rules=standard,experimental,test,custom "$PWD/examples/maven/src/main/kotlin/Test.kt" &>out.txt shell: bash - name: Run diKTat from cli on windows (absolute paths) continue-on-error: true if: runner.os == 'Windows' run: | - java -jar ktlint -R ${{ env.DIKTAT_JAR }} --disabled_rules=standard "%cd%/examples/maven/src/main/kotlin/Test.kt" > out.txt 2>&1 + java -jar ktlint -R ${{ env.DIKTAT_JAR }} --disabled_rules=standard,experimental,test,custom "%cd%/examples/maven/src/main/kotlin/Test.kt" > out.txt 2>&1 shell: cmd - name: Check output (absolute paths) @@ -141,7 +141,7 @@ jobs: - name: Run diKTat from cli (glob paths, 1 of 4) continue-on-error: true run: | - java -jar ktlint -R ${{ env.DIKTAT_JAR }} --disabled_rules=standard 'examples/maven/src/main/kotlin/*.kt' &>out.txt + java -jar ktlint -R ${{ env.DIKTAT_JAR }} --disabled_rules=standard,experimental,test,custom 'examples/maven/src/main/kotlin/*.kt' &>out.txt shell: bash - name: Check output (glob paths, 1 of 4) @@ -154,7 +154,7 @@ jobs: - name: Run diKTat from cli (glob paths, 2 of 4) continue-on-error: true run: | - java -jar ktlint -R ${{ env.DIKTAT_JAR }} --disabled_rules=standard 'examples/**/main/kotlin/*.kt' &>out.txt + java -jar ktlint -R ${{ env.DIKTAT_JAR }} --disabled_rules=standard,experimental,test,custom 'examples/**/main/kotlin/*.kt' &>out.txt shell: bash - name: Check output (glob paths, 2 of 4) @@ -167,7 +167,7 @@ jobs: - name: Run diKTat from cli (glob paths, 3 of 4) continue-on-error: true run: | - java -jar ktlint -R ${{ env.DIKTAT_JAR }} --disabled_rules=standard 'examples/**/*.kt' &>out.txt + java -jar ktlint -R ${{ env.DIKTAT_JAR }} --disabled_rules=standard,experimental,test,custom 'examples/**/*.kt' &>out.txt shell: bash - name: Check output (glob paths, 3 of 4) @@ -180,7 +180,7 @@ jobs: - name: Run diKTat from cli (glob paths, 4 of 4) continue-on-error: true run: | - java -jar ktlint -R ${{ env.DIKTAT_JAR }} --disabled_rules=standard '**/*.kt' &>out.txt + java -jar ktlint -R ${{ env.DIKTAT_JAR }} --disabled_rules=standard,experimental,test,custom '**/*.kt' &>out.txt shell: bash - name: Check output (glob paths, 4 of 4) diff --git a/README.md b/README.md index 21927380cb..3663361147 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ Main features of diktat are the following: Finally, run KTlint (with diKTat injected) to check your '*.kt' files in 'dir/your/dir': ```console -$ ./ktlint -R diktat.jar --disabled_rules=standard "dir/your/dir/**/*.kt" +$ ./ktlint -R diktat.jar --disabled_rules=standard,experimental,test,custom "dir/your/dir/**/*.kt" ``` To **autofix** all code style violations, use `-F` option. diff --git a/diktat-common/src/main/kotlin/org/cqfn/diktat/common/ktlint/KtLintUtils.kt b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/ktlint/KtLintUtils.kt new file mode 100644 index 0000000000..4e45c72d02 --- /dev/null +++ b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/ktlint/KtLintUtils.kt @@ -0,0 +1,22 @@ +@file:Suppress("HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE") + +package org.cqfn.diktat.common.ktlint + +private val ktlintRuleSetIds: List by lazy { + listOf( + "standard", + "experimental", + "test", + "custom" + ) +} + +/** + * Contains the necessary value of the `--disabled_rules` _KtLint_ argument. + */ +val ktlintDisabledRulesArgument: String by lazy { + ktlintRuleSetIds.joinToString( + prefix = "--disabled_rules=", + separator = "," + ) +} diff --git a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatExtension.kt b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatExtension.kt index 5b939f8f3e..e25eb42468 100644 --- a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatExtension.kt +++ b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatExtension.kt @@ -58,6 +58,6 @@ open class DiktatExtension( * @param action configuration lambda for `PatternFilterable` */ fun inputs(action: PatternFilterable.() -> Unit) { - action.invoke(patternSet) + action(patternSet) } } diff --git a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt index f0a0acbc36..e444c458b6 100644 --- a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt +++ b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt @@ -17,6 +17,7 @@ class DiktatGradlePlugin : Plugin { /** * @param project a gradle [Project] that the plugin is applied to */ + @Suppress("TOO_LONG_FUNCTION") override fun apply(project: Project) { val patternSet = PatternSet() val diktatExtension = project.extensions.create( @@ -31,12 +32,25 @@ class DiktatGradlePlugin : Plugin { val diktatConfiguration = project.configurations.create(DIKTAT_CONFIGURATION) { configuration -> configuration.isVisible = false configuration.dependencies.add(project.dependencies.create("com.pinterest:ktlint:$KTLINT_VERSION", closureOf { - exclude( - mutableMapOf( - "group" to "com.pinterest.ktlint", - "module" to "ktlint-ruleset-standard" - ) + /* + * Prevent the discovery of standard rules by excluding them as + * dependencies. + */ + val ktlintRuleSets = sequenceOf( + "standard", + "experimental", + "test", + "template" ) + ktlintRuleSets.forEach { ktlintRuleSet -> + exclude( + mutableMapOf( + "group" to "com.pinterest.ktlint", + "module" to "ktlint-ruleset-$ktlintRuleSet" + ) + ) + } + attributes { it.attribute(Bundling.BUNDLING_ATTRIBUTE, project.objects.named(Bundling::class.java, Bundling.EXTERNAL)) } diff --git a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskBase.kt b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskBase.kt index 7c11bd46fc..12a148b7a5 100644 --- a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskBase.kt +++ b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskBase.kt @@ -93,6 +93,20 @@ open class DiktatJavaExecTaskBase @Inject constructor( project.logger.info("Setting system property for diktat config to $it") }) args = additionalFlags.toMutableList().apply { + /* + * Disable the standard rules via the command line. + * + * Classpath exclusion (see `DiktatGradlePlugin`) is enough, but + * this is better left enabled as a safety net. + */ + run { + val ktlintRuleSetIds = sequenceOf("standard", "experimental", "test", "custom") + ktlintRuleSetIds.joinToString( + prefix = "--disabled_rules=", + separator = "," + ) + }.let(::add) + if (diktatExtension.debug) { add("--debug") } diff --git a/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt b/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt index a4d79fb08d..82a9043b7f 100644 --- a/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt @@ -33,7 +33,12 @@ class DiktatJavaExecTaskTest { @Test fun `check command line for various inputs`() { assertCommandLineEquals( - listOf(null, combinePathParts("src", "main", "kotlin", "Test.kt"), "--reporter=plain") + listOf( + null, + DISABLED_RULES, + combinePathParts("src", "main", "kotlin", "Test.kt"), + "--reporter=plain" + ) ) { inputs { include("src/**/*.kt") } } @@ -42,7 +47,13 @@ class DiktatJavaExecTaskTest { @Test fun `check command line in debug mode`() { assertCommandLineEquals( - listOf(null, "--debug", combinePathParts("src", "main", "kotlin", "Test.kt"), "--reporter=plain") + listOf( + null, + DISABLED_RULES, + "--debug", + combinePathParts("src", "main", "kotlin", "Test.kt"), + "--reporter=plain" + ) ) { inputs { include("src/**/*.kt") } debug = true @@ -54,7 +65,12 @@ class DiktatJavaExecTaskTest { project.file("src/main/kotlin/generated").mkdirs() project.file("src/main/kotlin/generated/Generated.kt").createNewFile() assertCommandLineEquals( - listOf(null, combinePathParts("src", "main", "kotlin", "Test.kt"), "--reporter=plain") + listOf( + null, + DISABLED_RULES, + combinePathParts("src", "main", "kotlin", "Test.kt"), + "--reporter=plain" + ) ) { inputs { include("src/**/*.kt") @@ -91,7 +107,11 @@ class DiktatJavaExecTaskTest { @Test fun `check command line has reporter type and output`() { assertCommandLineEquals( - listOf(null, "--reporter=json,output=${project.projectDir.resolve("some.txt")}") + listOf( + null, + DISABLED_RULES, + "--reporter=json,output=${project.projectDir.resolve("some.txt")}" + ) ) { inputs { exclude("*") } diktatConfigFile = project.file("../diktat-analysis.yml") @@ -103,7 +123,11 @@ class DiktatJavaExecTaskTest { @Test fun `check command line has reporter type without output`() { assertCommandLineEquals( - listOf(null, "--reporter=json") + listOf( + null, + DISABLED_RULES, + "--reporter=json" + ) ) { inputs { exclude("*") } diktatConfigFile = project.file("../diktat-analysis.yml") @@ -115,7 +139,11 @@ class DiktatJavaExecTaskTest { fun `check command line in githubActions mode`() { val path = project.file("${project.buildDir}/reports/diktat/diktat.sarif") assertCommandLineEquals( - listOf(null, "--reporter=sarif,output=$path") + listOf( + null, + DISABLED_RULES, + "--reporter=sarif,output=$path" + ) ) { inputs { exclude("*") } diktatConfigFile = project.file("../diktat-analysis.yml") @@ -132,7 +160,11 @@ class DiktatJavaExecTaskTest { fun `githubActions mode should have higher precedence over explicit reporter`() { val path = project.file("${project.buildDir}/reports/diktat/diktat.sarif") assertCommandLineEquals( - listOf(null, "--reporter=sarif,output=$path") + listOf( + null, + DISABLED_RULES, + "--reporter=sarif,output=$path" + ) ) { inputs { exclude("*") } diktatConfigFile = project.file("../diktat-analysis.yml") @@ -145,7 +177,11 @@ class DiktatJavaExecTaskTest { @Test fun `should set system property with SARIF reporter`() { assertCommandLineEquals( - listOf(null, "--reporter=sarif") + listOf( + null, + DISABLED_RULES, + "--reporter=sarif" + ) ) { inputs { exclude("*") } diktatConfigFile = project.file("../diktat-analysis.yml") @@ -232,5 +268,6 @@ class DiktatJavaExecTaskTest { companion object { private const val DIKTAT_CHECK_TASK = "diktatCheck" + private const val DISABLED_RULES = "--disabled_rules=standard,experimental,test,custom" } } diff --git a/diktat-ruleset/src/test/resources/test/smoke/save.toml b/diktat-ruleset/src/test/resources/test/smoke/save.toml index bebb752e9d..ba0e3acee4 100644 --- a/diktat-ruleset/src/test/resources/test/smoke/save.toml +++ b/diktat-ruleset/src/test/resources/test/smoke/save.toml @@ -9,7 +9,7 @@ timeOutMillis = 3600000 ["fix and warn"] ["fix and warn".fix] - execFlags="--disabled_rules=standard -F" + execFlags="--disabled_rules=standard,experimental,test,custom -F" ["fix and warn".warn] lineCaptureGroup = 1 columnCaptureGroup = 2 diff --git a/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/save.toml b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/save.toml index 29c5a68b62..f4d8eb501e 100644 --- a/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/save.toml +++ b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/save.toml @@ -8,7 +8,7 @@ expectedWarningsPattern = "// ;warn:?(.*):(\\d*): (.+)" ["fix and warn"] ["fix and warn".fix] - execFlags="--disabled_rules=standard -F" + execFlags="--disabled_rules=standard,experimental,test,custom -F" ["fix and warn".warn] actualWarningsPattern = "(\\w+\\..+):(\\d+):(\\d+): (\\[.*\\].*)$" exactWarningsMatch = false