Skip to content

Commit

Permalink
Add tests for LintTask being incremental
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski committed Jan 24, 2023
1 parent a05ebaf commit cf34af6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ abstract class FormatWorkerAction : WorkAction<FormatWorkerParameters> {
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<String>()
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ abstract class LintWorkerAction : WorkAction<LintWorkerParameters> {
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 }}")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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"}
}
}

Expand Down

0 comments on commit cf34af6

Please sign in to comment.