Skip to content

Commit

Permalink
Disabled additional rules from KtLint (#1643)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrey Shcheglov <andrewbass+saveourtool@gmail.com>
  • Loading branch information
nulls and 0x6675636b796f75676974687562 authored Mar 27, 2023
1 parent e4e6465 commit d2a22f0
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 24 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@file:Suppress("HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE")

package org.cqfn.diktat.common.ktlint

private val ktlintRuleSetIds: List<String> 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 = ","
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ open class DiktatExtension(
* @param action configuration lambda for `PatternFilterable`
*/
fun inputs(action: PatternFilterable.() -> Unit) {
action.invoke(patternSet)
action(patternSet)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class DiktatGradlePlugin : Plugin<Project> {
/**
* @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(
Expand All @@ -31,12 +32,25 @@ class DiktatGradlePlugin : Plugin<Project> {
val diktatConfiguration = project.configurations.create(DIKTAT_CONFIGURATION) { configuration ->
configuration.isVisible = false
configuration.dependencies.add(project.dependencies.create("com.pinterest:ktlint:$KTLINT_VERSION", closureOf<ExternalModuleDependency> {
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))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cqfn.diktat.plugin.gradle

import org.cqfn.diktat.common.config.rules.DIKTAT_CONF_PROPERTY
import org.cqfn.diktat.common.ktlint.ktlintDisabledRulesArgument
import org.cqfn.diktat.plugin.gradle.DiktatGradlePlugin.Companion.DIKTAT_CHECK_TASK
import org.cqfn.diktat.plugin.gradle.DiktatGradlePlugin.Companion.DIKTAT_FIX_TASK
import org.cqfn.diktat.plugin.gradle.DiktatGradlePlugin.Companion.MIN_JVM_REQUIRES_ADD_OPENS
Expand Down Expand Up @@ -93,6 +94,14 @@ 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.
*/
add(ktlintDisabledRulesArgument)

if (diktatExtension.debug) {
add("--debug")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cqfn.diktat.plugin.gradle

import org.cqfn.diktat.common.config.rules.DIKTAT_CONF_PROPERTY
import org.cqfn.diktat.common.ktlint.ktlintDisabledRulesArgument

import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
Expand Down Expand Up @@ -33,7 +34,12 @@ class DiktatJavaExecTaskTest {
@Test
fun `check command line for various inputs`() {
assertCommandLineEquals(
listOf(null, combinePathParts("src", "main", "kotlin", "Test.kt"), "--reporter=plain")
listOf(
null,
ktlintDisabledRulesArgument,
combinePathParts("src", "main", "kotlin", "Test.kt"),
"--reporter=plain"
)
) {
inputs { include("src/**/*.kt") }
}
Expand All @@ -42,7 +48,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,
ktlintDisabledRulesArgument,
"--debug",
combinePathParts("src", "main", "kotlin", "Test.kt"),
"--reporter=plain"
)
) {
inputs { include("src/**/*.kt") }
debug = true
Expand All @@ -54,7 +66,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,
ktlintDisabledRulesArgument,
combinePathParts("src", "main", "kotlin", "Test.kt"),
"--reporter=plain"
)
) {
inputs {
include("src/**/*.kt")
Expand Down Expand Up @@ -91,7 +108,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,
ktlintDisabledRulesArgument,
"--reporter=json,output=${project.projectDir.resolve("some.txt")}"
)
) {
inputs { exclude("*") }
diktatConfigFile = project.file("../diktat-analysis.yml")
Expand All @@ -103,7 +124,11 @@ class DiktatJavaExecTaskTest {
@Test
fun `check command line has reporter type without output`() {
assertCommandLineEquals(
listOf(null, "--reporter=json")
listOf(
null,
ktlintDisabledRulesArgument,
"--reporter=json"
)
) {
inputs { exclude("*") }
diktatConfigFile = project.file("../diktat-analysis.yml")
Expand All @@ -115,7 +140,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,
ktlintDisabledRulesArgument,
"--reporter=sarif,output=$path"
)
) {
inputs { exclude("*") }
diktatConfigFile = project.file("../diktat-analysis.yml")
Expand All @@ -132,7 +161,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,
ktlintDisabledRulesArgument,
"--reporter=sarif,output=$path"
)
) {
inputs { exclude("*") }
diktatConfigFile = project.file("../diktat-analysis.yml")
Expand All @@ -145,7 +178,11 @@ class DiktatJavaExecTaskTest {
@Test
fun `should set system property with SARIF reporter`() {
assertCommandLineEquals(
listOf(null, "--reporter=sarif")
listOf(
null,
ktlintDisabledRulesArgument,
"--reporter=sarif"
)
) {
inputs { exclude("*") }
diktatConfigFile = project.file("../diktat-analysis.yml")
Expand Down
2 changes: 1 addition & 1 deletion diktat-ruleset/src/test/resources/test/smoke/save.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d2a22f0

Please sign in to comment.