From 5421456fe8d7f77c935e26acdf94abcac9f60839 Mon Sep 17 00:00:00 2001 From: Jeremy Mailen Date: Wed, 27 Dec 2023 16:10:27 -0800 Subject: [PATCH] Fix Lint and Format Tasks not re-running after editorconfig changes (#358) Fixes #327 --- .../kotlinter/tasks/ConfigurableKtLintTask.kt | 5 +-- .../kotlinter/functional/EditorConfigTest.kt | 44 +++++++++++++++---- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt index 2322bafb..74c6d3bb 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt @@ -28,11 +28,8 @@ abstract class ConfigurableKtLintTask( from(projectLayout.findApplicableEditorConfigFiles().toList()) } - protected fun getChangedEditorconfigFiles(inputChanges: InputChanges) = if (inputChanges.isIncremental) { + protected fun getChangedEditorconfigFiles(inputChanges: InputChanges) = inputChanges.getFileChanges(editorconfigFiles).map(FileChange::getFile) - } else { - emptyList() - } } internal inline fun ObjectFactory.property(default: T? = null): Property = property(T::class.java).apply { diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt index 2b19e0d6..cec116bd 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt @@ -131,7 +131,7 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { } @Test - fun `editorconfig changes are taken into account on lint task re-runs`() { + fun `editorconfig changes are taken into account when adds lint issues`() { setup(KotlinterConfig.DEFAULT) projectRoot.resolve(".editorconfig") { writeText( @@ -173,7 +173,36 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { } @Test - fun `editorconfig changes are ignored for format task re-runs`() { + fun `editorconfig changes are taken into account when removes lint issues`() { + setup(KotlinterConfig.DEFAULT) + projectRoot.resolve(".editorconfig") { + writeText(editorConfig) + } + projectRoot.resolve("src/main/kotlin/FileName.kt") { + writeText(kotlinClass("DifferentClassName")) + } + buildAndFail("lintKotlin").apply { + assertEquals(TaskOutcome.FAILED, task(":lintKotlinMain")?.outcome) + assertTrue(output.contains("[standard:filename] File 'FileName.kt' contains a single top level declaration")) + } + + projectRoot.resolve(".editorconfig") { + writeText( + // language=editorconfig + """ + [*.{kt,kts}] + ktlint_standard_filename = disabled + """.trimIndent(), + ) + } + build("lintKotlin", "--info").apply { + assertEquals(TaskOutcome.SUCCESS, task(":lintKotlinMain")?.outcome) + assertTrue(output.contains("resetting KtLint caches")) + } + } + + @Test + fun `editorconfig changes are taken for format task re-runs`() { setup(KotlinterConfig.DEFAULT) projectRoot.resolve(".editorconfig") { writeText(editorConfig) @@ -200,13 +229,12 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { } build("formatKotlin", "--info").apply { assertEquals(TaskOutcome.SUCCESS, task(":formatKotlinMain")?.outcome) - assertTrue(output.contains("Format could not fix")) - assertFalse(output.contains("resetting KtLint caches")) + assertFalse(output.contains("Format could not fix")) } } @Test - fun `editorconfig changes are ignored for format task re-runs when failBuildWhenCannotAutoFormat enabled`() { + fun `editorconfig changes are taken for format task re-runs when failBuildWhenCannotAutoFormat configured`() { setup(KotlinterConfig.FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT) projectRoot.resolve(".editorconfig") { writeText(editorConfig) @@ -231,10 +259,8 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { """.trimIndent(), ) } - buildAndFail("formatKotlin", "--info").apply { - assertEquals(TaskOutcome.FAILED, task(":formatKotlinMain")?.outcome) - assertTrue(output.contains("Format could not fix")) - assertFalse(output.contains("resetting KtLint caches")) + build("formatKotlin", "--info").apply { + assertEquals(TaskOutcome.SUCCESS, task(":formatKotlinMain")?.outcome) } } }