Skip to content

Commit

Permalink
Fix Lint and Format Tasks not re-running after editorconfig changes (#…
Browse files Browse the repository at this point in the history
…358)

Fixes #327
  • Loading branch information
jeremymailen authored Dec 28, 2023
1 parent 2023cbb commit 5421456
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <reified T> ObjectFactory.property(default: T? = null): Property<T> = property(T::class.java).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
}
}

0 comments on commit 5421456

Please sign in to comment.