Skip to content

Commit

Permalink
fix: apply configuration for source sets and targets that are added a…
Browse files Browse the repository at this point in the history
…fter the plugin is applied

Use `DomainObjectCollection.all` instead of `Iterable<T>.forEach`.
`all` executes the given function also against any objects subsequently added objects
  • Loading branch information
odzhychko authored and wakingrufus committed Dec 8, 2023
1 parent 7901898 commit 4474650
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

- fix: apply configuration for source sets and targets that are added after the plugin is
applied [#732](https://github.com/JLLeitschuh/ktlint-gradle/pull/732)

## [12.0.2] - 2023-12-01

- remove KtLintIdea Plugin [#726](https://github.com/JLLeitschuh/ktlint-gradle/pull/726).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ open class KtlintPlugin : Plugin<Project> {
private fun PluginHolder.applyKtlintMultiplatform(): (Plugin<in Any>) -> Unit = {
val multiplatformExtension = target.extensions.getByType(KotlinMultiplatformExtension::class.java)

multiplatformExtension.sourceSets.forEach { sourceSet ->
multiplatformExtension.sourceSets.all(fun(sourceSet) {
val checkTask = createCheckTask(
this,
sourceSet.name,
Expand Down Expand Up @@ -68,18 +68,18 @@ open class KtlintPlugin : Plugin<Project> {
)

addGenerateReportsTaskToProjectMetaFormatTask(generateReportsFormatTask)
}
})

multiplatformExtension.targets.forEach { kotlinTarget ->
multiplatformExtension.targets.all(fun(kotlinTarget) {
if (kotlinTarget.platformType == KotlinPlatformType.androidJvm) {
applyKtLintToAndroid()
}
}
})
}

private fun PluginHolder.applyKtLint(): (Plugin<in Any>) -> Unit = {
target.extensions.configure(KotlinProjectExtension::class.java) {
sourceSets.forEach { sourceSet ->
sourceSets.all(fun(sourceSet) {
val kotlinSourceDirectories = sourceSet.kotlin.sourceDirectories
val checkTask = createCheckTask(
this@applyKtLint,
Expand Down Expand Up @@ -109,7 +109,7 @@ open class KtlintPlugin : Plugin<Project> {
)

addGenerateReportsTaskToProjectMetaFormatTask(generateReportsFormatTask)
}
})
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.jlleitschuh.gradle.ktlint

import org.assertj.core.api.Assertions.assertThat
import org.gradle.util.GradleVersion
import org.jlleitschuh.gradle.ktlint.tasks.GenerateReportsTask
import org.jlleitschuh.gradle.ktlint.testdsl.CommonTest
import org.jlleitschuh.gradle.ktlint.testdsl.GradleTestVersions
import org.jlleitschuh.gradle.ktlint.testdsl.build
import org.jlleitschuh.gradle.ktlint.testdsl.project
import org.jlleitschuh.gradle.ktlint.testdsl.projectSetup
import org.junit.jupiter.api.DisplayName
import java.io.File

/**
* Contains all tests related to "org.jetbrains.kotlin.multiplatform" plugin support.
*/
@GradleTestVersions
class KotlinMultiplatformPluginTests : AbstractPluginTest() {
private fun multiplatformProjectSetup(gradleVersion: GradleVersion): (File) -> Unit = {
projectSetup("multiplatform", gradleVersion).invoke(it)

//language=Groovy
it.resolve("build.gradle").appendText(
"""
|
|kotlin {
| js {
| browser()
| }
| jvm()
|}
""".trimMargin()
)
}

@DisplayName("Should add check on all sources")
@CommonTest
fun addCheckTasks(gradleVersion: GradleVersion) {
project(gradleVersion, projectSetup = multiplatformProjectSetup(gradleVersion)) {
build("-m", CHECK_PARENT_TASK_NAME) {
val ktlintTasks = output.lineSequence().toList()

assertThat(ktlintTasks).anySatisfy {
assertThat(it).contains(
GenerateReportsTask.generateNameForSourceSets(
"commonMain",
GenerateReportsTask.LintType.CHECK
)
)
}
assertThat(ktlintTasks).anySatisfy {
assertThat(it).contains(
GenerateReportsTask.generateNameForSourceSets(
"JsMain",
GenerateReportsTask.LintType.CHECK
)
)
}
assertThat(ktlintTasks).anySatisfy {
assertThat(it).contains(
GenerateReportsTask.generateNameForSourceSets(
"JvmMain",
GenerateReportsTask.LintType.CHECK
)
)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.jlleitschuh.gradle.ktlint.testdsl.TestProject.Companion.FAIL_SOURCE_F
import org.jlleitschuh.gradle.ktlint.testdsl.build
import org.jlleitschuh.gradle.ktlint.testdsl.buildAndFail
import org.jlleitschuh.gradle.ktlint.testdsl.project
import org.jlleitschuh.gradle.ktlint.testdsl.projectSetup
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.condition.EnabledOnOs
import org.junit.jupiter.api.condition.OS
Expand Down Expand Up @@ -689,4 +690,43 @@ class KtlintPluginTest : AbstractPluginTest() {
}
}
}

@DisplayName("Should add check on additional sources")
@CommonTest
fun checkAdditionalSources(gradleVersion: GradleVersion) {
fun projectSetup(file: File) {
projectSetup("jvm", gradleVersion).invoke(file)

//language=Groovy
file.resolve("build.gradle").appendText(
"""
|
|sourceSets {
| additionalSources {
| kotlin {
| srcDir 'src/additionalSources/kotlin'
| }
| }
|}
""".trimMargin()
)
}

val additionalSourcesSourceSetCheckTaskName = GenerateReportsTask.generateNameForSourceSets(
"additionalSources",
GenerateReportsTask.LintType.CHECK
)

project(gradleVersion, projectSetup = ::projectSetup) {
build("-m", CHECK_PARENT_TASK_NAME) {
val ktlintTasks = output.lineSequence().toList()
assertThat(ktlintTasks).anySatisfy {
assertThat(it).contains(mainSourceSetCheckTaskName)
}
assertThat(ktlintTasks).anySatisfy {
assertThat(it).contains(additionalSourcesSourceSetCheckTaskName)
}
}
}
}
}

0 comments on commit 4474650

Please sign in to comment.