Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Make KtLint tasks runnable even without specific plugins being present #426

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Available on the Gradle Plugins Portal: https://plugins.gradle.org/plugin/org.jm

```kotlin
plugins {
id("org.jmailen.kotlinter") version "5.0.1"
id("org.jmailen.kotlinter") version "5.0.2"
}
```

Expand All @@ -36,7 +36,7 @@ plugins {

```groovy
plugins {
id "org.jmailen.kotlinter" version "5.0.1"
id "org.jmailen.kotlinter" version "5.0.2"
}
```

Expand All @@ -50,7 +50,7 @@ Root `build.gradle.kts`

```kotlin
plugins {
id("org.jmailen.kotlinter") version "5.0.1" apply false
id("org.jmailen.kotlinter") version "5.0.2" apply false
}
```

Expand All @@ -70,7 +70,7 @@ Root `build.gradle`

```groovy
plugins {
id 'org.jmailen.kotlinter' version "5.0.1" apply false
id 'org.jmailen.kotlinter' version "5.0.2" apply false
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ val githubUrl = "https://github.com/jeremymailen/kotlinter-gradle"
val webUrl = "https://github.com/jeremymailen/kotlinter-gradle"
val projectDescription = "Lint and formatting for Kotlin using ktlint with configuration-free setup on JVM and Android projects"

version = "5.0.1"
version = "5.0.2"
group = "org.jmailen.gradle"
description = projectDescription

Expand Down
13 changes: 8 additions & 5 deletions src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.jmailen.gradle.kotlinter
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.attributes.Bundling
import org.gradle.api.artifacts.Configuration
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Provider
Expand Down Expand Up @@ -42,13 +43,13 @@ class KotlinterPlugin : Plugin<Project> {
val formatKotlin = registerParentFormatTask()

registerSourceSetTasks(kotlinterExtension, sourceResolver, lintKotlin, formatKotlin)

// Configure all tasks including custom user tasks
tasks.withType(ConfigurableKtLintTask::class.java).configureEach { task ->
task.ktlintClasspath.from(ktlintConfiguration)
}
}
}

// Configure all tasks including custom user tasks
tasks.withType(ConfigurableKtLintTask::class.java).configureEach { task ->
task.ktlintClasspath.from(ktlintConfiguration)
}
}

private fun Project.registerParentLintTask(): TaskProvider<Task> = tasks.register("lintKotlin") {
Expand All @@ -69,6 +70,8 @@ class KotlinterPlugin : Plugin<Project> {
isCanBeConsumed = false
isVisible = false

attributes.attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling::class.java, Bundling.EXTERNAL))

val dependencyProvider = provider {
// Even though we don't use CLI, it bundles all the runtime dependencies we need.
val ktlintVersion = kotlinterExtension.ktlintVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,27 @@ import org.jmailen.gradle.kotlinter.functional.utils.settingsFile
import org.junit.jupiter.api.Assertions.assertArrayEquals
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import java.io.File

class CustomTaskTest : WithGradleTest.Kotlin() {

lateinit var projectRoot: File

@BeforeEach
fun setUp() {
private fun setup(testType: String) {
projectRoot = testProjectDir.apply {
resolve("settings.gradle") { writeText(settingsFile) }
resolve("build.gradle") {
// language=groovy
val buildScript =
"""
plugins {
id 'kotlin'
id 'org.jmailen.kotlinter'
val buildScript = buildString {
appendLine("plugins {")
if (testType == WITH_KOTLIN_PLUGIN) {
appendLine("id 'kotlin'")
}
$repositories
""".trimIndent()
appendLine("id 'org.jmailen.kotlinter'")
appendLine("}")
appendLine(repositories)
}
writeText(buildScript)
}
}
Expand All @@ -39,8 +38,11 @@ class CustomTaskTest : WithGradleTest.Kotlin() {
}
}

@Test
fun `ktLint custom task succeeds when no lint errors detected`() {
@ParameterizedTest
@ValueSource(strings = [WITH_NO_PLUGINS, WITH_KOTLIN_PLUGIN])
fun `ktLint custom task succeeds when no lint errors detected`(testType: String) {
setup(testType)

projectRoot.resolve(".editorconfig") {
writeText(editorConfig)
}
Expand Down Expand Up @@ -77,8 +79,11 @@ class CustomTaskTest : WithGradleTest.Kotlin() {
}
}

@Test
fun `ktLint custom task succeeds with default configuration`() {
@ParameterizedTest
@ValueSource(strings = [WITH_NO_PLUGINS, WITH_KOTLIN_PLUGIN])
fun `ktLint custom task succeeds with default configuration`(testType: String) {
setup(testType)

projectRoot.resolve("build.gradle") {
// language=groovy
val buildScript =
Expand All @@ -98,8 +103,11 @@ class CustomTaskTest : WithGradleTest.Kotlin() {
}
}

@Test
fun `ktLint custom task succeeds with fully provided configuration`() {
@ParameterizedTest
@ValueSource(strings = [WITH_NO_PLUGINS, WITH_KOTLIN_PLUGIN])
fun `ktLint custom task succeeds with fully provided configuration`(testType: String) {
setup(testType)

projectRoot.resolve(".editorconfig") {
writeText(editorConfig)
}
Expand All @@ -124,8 +132,11 @@ class CustomTaskTest : WithGradleTest.Kotlin() {
}
}

@Test
fun `ktLintFormat custom task succeeds with fully provided configuration`() {
@ParameterizedTest
@ValueSource(strings = [WITH_NO_PLUGINS, WITH_KOTLIN_PLUGIN])
fun `ktLintFormat custom task succeeds with fully provided configuration`(testType: String) {
setup(testType)

projectRoot.resolve(".editorconfig") {
writeText(editorConfig)
}
Expand All @@ -148,8 +159,11 @@ class CustomTaskTest : WithGradleTest.Kotlin() {
}
}

@Test
fun `ktLint custom task skips reports generation if reports not configured`() {
@ParameterizedTest
@ValueSource(strings = [WITH_NO_PLUGINS, WITH_KOTLIN_PLUGIN])
fun `ktLint custom task skips reports generation if reports not configured`(testType: String) {
setup(testType)

projectRoot.resolve("src/main/kotlin/MissingNewLine.kt") {
// language=kotlin
val validClass =
Expand Down Expand Up @@ -189,8 +203,11 @@ class CustomTaskTest : WithGradleTest.Kotlin() {
}
}

@Test
fun `ktLint custom task became up-to-date on second run if reports not configured`() {
@ParameterizedTest
@ValueSource(strings = [WITH_NO_PLUGINS, WITH_KOTLIN_PLUGIN])
fun `ktLint custom task became up-to-date on second run if reports not configured`(testType: String) {
setup(testType)

projectRoot.resolve("build.gradle") {
// language=groovy
val buildScript =
Expand Down Expand Up @@ -224,8 +241,11 @@ class CustomTaskTest : WithGradleTest.Kotlin() {
}
}

@Test
fun `ktLint custom task treats reports as input parameter`() {
@ParameterizedTest
@ValueSource(strings = [WITH_NO_PLUGINS, WITH_KOTLIN_PLUGIN])
fun `ktLint custom task treats reports as input parameter`(testType: String) {
setup(testType)

projectRoot.resolve("build.gradle") {
// language=groovy
val buildScript =
Expand Down Expand Up @@ -256,4 +276,9 @@ class CustomTaskTest : WithGradleTest.Kotlin() {
assertTrue(projectRoot.resolve("build/lint-report.txt").exists())
}
}

companion object {
const val WITH_NO_PLUGINS = "with-no-plugins"
const val WITH_KOTLIN_PLUGIN = "with-kotlin-plugin"
}
}
Loading