From cf34af6eec9c6ec7362f628494da3948d45c9f32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Kwiecin=CC=81ski?= Date: Sun, 16 Oct 2022 21:30:16 +0200 Subject: [PATCH] Add tests for `LintTask` being incremental --- .../gradle/kotlinter/tasks/FormatTask.kt | 2 +- .../tasks/format/FormatWorkerAction.kt | 3 +- .../kotlinter/tasks/lint/LintWorkerAction.kt | 3 +- .../kotlinter/functional/KotlinProjectTest.kt | 77 +++++++++++++++++-- 4 files changed, 76 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt index b64cf97c..8182662d 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt @@ -40,7 +40,7 @@ open class FormatTask @Inject constructor( workQueue.submit(FormatWorkerAction::class.java) { p -> p.name.set(name) - p.files.from(getChangedSources(inputChanges)) + p.files.from(source) p.projectDirectory.set(projectLayout.projectDirectory.asFile) p.ktLintParams.set(getKtLintParams()) p.output.set(report) diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt index e39c6330..9418995a 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt @@ -27,7 +27,8 @@ abstract class FormatWorkerAction : WorkAction { changedEditorconfigFiles = parameters.changedEditorConfigFiles, logger = logger, ) - logger.info("Resolved ${ktLintEngine.ruleProviders.size} RuleProviders") + logger.info("$name - resolved ${ktLintEngine.ruleProviders.size} RuleProviders") + logger.info("$name - executing against ${files.size} file(s)") val fixes = mutableListOf() try { diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerAction.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerAction.kt index 53fcba58..e827051a 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerAction.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerAction.kt @@ -31,7 +31,8 @@ abstract class LintWorkerAction : WorkAction { changedEditorconfigFiles = parameters.changedEditorConfigFiles, logger = logger, ) - logger.info("Resolved ${ktLintEngine.ruleProviders.size} RuleProviders") + logger.info("$name - resolved ${ktLintEngine.ruleProviders.size} RuleProviders") + logger.info("$name - executing against ${files.size} file(s)") if (logger.isDebugEnabled) { logger.debug("Resolved RuleSetProviders = ${ktLintEngine.ruleProviders.joinToString { it.createNewRuleInstance().id }}") } diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt index 7811bfec..53b54518 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt @@ -1,6 +1,7 @@ package org.jmailen.gradle.kotlinter.functional import org.gradle.testkit.runner.TaskOutcome.FAILED +import org.gradle.testkit.runner.TaskOutcome.NO_SOURCE import org.gradle.testkit.runner.TaskOutcome.SUCCESS import org.gradle.testkit.runner.TaskOutcome.UP_TO_DATE import org.jmailen.gradle.kotlinter.functional.utils.editorConfig @@ -193,6 +194,66 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { } } + @Test + fun `lint task is incremental`() { + settingsFile() + buildFile() + editorConfig() + kotlinSourceFile( + "CustomObject.kt", + """ + object CustomObject + + """.trimIndent(), + ) + kotlinSourceFile( + "CustomClass.kt", + """ + data class CustomClass(val value: Int) + + """.trimIndent(), + ) + + build("lintKotlin", "--info").apply { + assertEquals(SUCCESS, task(":lintKotlin")?.outcome) + assertEquals(SUCCESS, task(":lintKotlinMain")?.outcome) + assertEquals(NO_SOURCE, task(":lintKotlinTest")?.outcome) + assertTrue(output.contains("lintKotlinMain - executing against 2 file(s)")) + } + + kotlinSourceFile( + "CustomClass.kt", + """ + data class CustomClass(val modified: Int) + + """.trimIndent(), + ) + build("lintKotlin", "--info").apply { + assertEquals(SUCCESS, task(":lintKotlin")?.outcome) + assertEquals(SUCCESS, task(":lintKotlinMain")?.outcome) + assertEquals(NO_SOURCE, task(":lintKotlinTest")?.outcome) + assertTrue(output.contains("lintKotlinMain - executing against 1 file(s)")) + } + + editorconfigFile.appendText("content=updated") + build("lintKotlin", "--info").apply { + assertEquals(SUCCESS, task(":lintKotlin")?.outcome) + assertTrue(output.contains("lintKotlinMain - executing against 2 file(s)")) + } + + kotlinSourceFile( + "CustomClass.kt", + """ + data class CustomClass(val modifiedEditorconfig: Int) + + """.trimIndent(), + ) + build("lintKotlin", "--info").apply { + assertEquals(SUCCESS, task(":lintKotlin")?.outcome) + assertTrue(output.contains("lintKotlinMain - executing against 1 file(s)")) + } + } + @Test fun `plugin is compatible with configuration cache`() { settingsFile() @@ -225,7 +286,7 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { } @Test - fun `plugin resolves dynamically loaded RulesetProviders`() { + fun `plugin resolves dynamically loaded RuleSetProviders`() { settingsFile() buildFile() editorConfig() @@ -237,18 +298,22 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { """.trimIndent(), ) - val matcher = "Resolved (\\d+) RuleProviders".toRegex() + fun String.findResolvedRuleProvidersCount(taskName: String): Int { + val matcher = "$taskName - resolved (\\d+) RuleProviders".toRegex() + + return matcher.find(this)?.groups?.get(1)?.value?.toIntOrNull() ?: 0 + } build("lintKotlin", "--info").apply { assertEquals(SUCCESS, task(":lintKotlin")?.outcome) - val resolvedRulesCount = matcher.find(output)?.groups?.get(1)?.value?.toIntOrNull() ?: 0 - assertTrue(resolvedRulesCount > 0) + val resolvedRulesCount = output.findResolvedRuleProvidersCount("lintKotlinMain") + assertTrue(resolvedRulesCount > 0) { "expected to find more than 0 resolved RuleProviders, was=$resolvedRulesCount"} } build("formatKotlin", "--info").apply { assertEquals(SUCCESS, task(":formatKotlin")?.outcome) - val resolvedRulesCount = matcher.find(output)?.groups?.get(1)?.value?.toIntOrNull() ?: 0 - assertTrue(resolvedRulesCount > 0) + val resolvedRulesCount = output.findResolvedRuleProvidersCount("formatKotlinMain") + assertTrue(resolvedRulesCount > 0) { "expected to find more than 0 resolved RuleProviders, was=$resolvedRulesCount"} } }