Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Do not abort when applying the plugin if the Kotlin plugin was not ap…
Browse files Browse the repository at this point in the history
…plied
  • Loading branch information
hovinen committed Jul 15, 2020
1 parent c587431 commit 434f670
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.kotlin.formatter.plugin

import java.io.File
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
Expand All @@ -15,9 +16,9 @@ class KotlinFormatterPlugin : Plugin<Project> {
.register("formatKotlin", FormatKotlinTask::class.java) {
it.source(
target.extensions
.getByType(KotlinProjectExtension::class.java)
.sourceSets
.flatMap { set -> set.kotlin.sourceDirectories.files }
.findByType(KotlinProjectExtension::class.java)
?.sourceSets
?.flatMap { set -> set.kotlin.sourceDirectories.files } ?: setOf<File>()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.gradle.testkit.runner.TaskOutcome
import org.gradle.util.GFileUtils.parentMkdirs
import org.gradle.util.GFileUtils.writeFile
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir

Expand All @@ -17,99 +18,118 @@ class FormatKotlinTaskTest {
lateinit var testFolder: Path

@BeforeEach
fun setup() {
writeBuildScript()
writeSettingsFile()
}

private fun writeBuildScript() {
writeFile(
"""
plugins {
kotlin("jvm") version "1.3.72"
id("tech.formatter-kt.formatter")
}
""".trimIndent(),
testFolder.resolve("build.gradle.kts").toFile()
)
}

private fun writeSettingsFile() {
fun writeSettingsFile() {
writeFile(
"""rootProject.name = "test"""".trimIndent(),
testFolder.resolve("settings.gradle.kts").toFile()
)
}

@Test
fun `should format Kotlin source files`() {
val sourceDirectory = testFolder.resolve("src/main/kotlin/somepackage")
val sourceFile = sourceDirectory.resolve("AClass.kt")
parentMkdirs(sourceDirectory.toFile())
writeFile(
"""
package somepackage
class AClass {
val aProperty:
String = "Hello"
}
""".trimIndent(),
sourceFile.toFile()
)

runFormatter()
@Nested
inner class WithKotlinPlugin {
@BeforeEach
fun writeBuildScript() {
writeFile(
"""
plugins {
kotlin("jvm") version "1.3.72"
id("tech.formatter-kt.formatter")
}
""".trimIndent(),
testFolder.resolve("build.gradle.kts").toFile()
)
}

assertThat(Files.readString(sourceFile))
.isEqualTo(
@Test
fun `should format Kotlin source files`() {
val sourceDirectory = testFolder.resolve("src/main/kotlin/somepackage")
val sourceFile = sourceDirectory.resolve("AClass.kt")
parentMkdirs(sourceDirectory.toFile())
writeFile(
"""
package somepackage
class AClass {
val aProperty: String = "Hello"
class AClass {
val aProperty:
String = "Hello"
}
""".trimIndent()
""".trimIndent(),
sourceFile.toFile()
)
}

@Test
fun `should not format Java source files`() {
val sourceDirectory = testFolder.resolve("src/main/kotlin/somepackage")
writeKotlinSourceFile("AnotherClass", sourceDirectory)
val sourceFile = sourceDirectory.resolve("AClass.java")
parentMkdirs(sourceDirectory.toFile())
writeFile(
"""
package somepackage;
public class AClass { String aProperty =
"Hello"; }
""".trimIndent(),
sourceFile.toFile()
)
runFormatter()

val result = runFormatter()
assertThat(Files.readString(sourceFile))
.isEqualTo(
"""
package somepackage
class AClass {
val aProperty: String = "Hello"
}
""".trimIndent()
)
}

assertThat(result.task(":formatKotlin")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(Files.readString(sourceFile))
.isEqualTo(
@Test
fun `should not format Java source files`() {
val sourceDirectory = testFolder.resolve("src/main/kotlin/somepackage")
writeKotlinSourceFile("AnotherClass", sourceDirectory)
val sourceFile = sourceDirectory.resolve("AClass.java")
parentMkdirs(sourceDirectory.toFile())
writeFile(
"""
package somepackage;
public class AClass { String aProperty =
"Hello"; }
""".trimIndent()
""".trimIndent(),
sourceFile.toFile()
)

val result = runFormatter()

assertThat(result.task(":formatKotlin")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(Files.readString(sourceFile))
.isEqualTo(
"""
package somepackage;
public class AClass { String aProperty =
"Hello"; }
""".trimIndent()
)
}

private fun writeKotlinSourceFile(className: String, sourceDirectory: Path) {
val sourceFile = sourceDirectory.resolve("$className.kt")
writeFile(
"""
package somepackage
class $className
""".trimIndent(),
sourceFile.toFile()
)
}
}

private fun writeKotlinSourceFile(className: String, sourceDirectory: Path) {
val sourceFile = sourceDirectory.resolve("$className.kt")
writeFile(
"""
package somepackage
class $className
""".trimIndent(),
sourceFile.toFile()
)
@Nested
inner class WithoutKotlinPlugin {
@Test
fun `should skip task if Kotlin plugin was not applied`() {
writeFile(
"""
plugins {
java
id("tech.formatter-kt.formatter")
}
""".trimIndent(),
testFolder.resolve("build.gradle.kts").toFile()
)

val result = runFormatter()

assertThat(result.task(":formatKotlin")?.outcome).isEqualTo(TaskOutcome.NO_SOURCE)
}
}

private fun runFormatter(): BuildResult {
Expand Down

0 comments on commit 434f670

Please sign in to comment.