diff --git a/plugin/src/main/kotlin/org/kotlin/formatter/plugin/KotlinFormatterPlugin.kt b/plugin/src/main/kotlin/org/kotlin/formatter/plugin/KotlinFormatterPlugin.kt index da31bcd..f8698cd 100644 --- a/plugin/src/main/kotlin/org/kotlin/formatter/plugin/KotlinFormatterPlugin.kt +++ b/plugin/src/main/kotlin/org/kotlin/formatter/plugin/KotlinFormatterPlugin.kt @@ -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 @@ -15,9 +16,9 @@ class KotlinFormatterPlugin : Plugin { .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() ) } } diff --git a/plugin/src/test/kotlin/org/kotlin/formatter/plugin/FormatKotlinTaskTest.kt b/plugin/src/test/kotlin/org/kotlin/formatter/plugin/FormatKotlinTaskTest.kt index 7e1353a..738711c 100644 --- a/plugin/src/test/kotlin/org/kotlin/formatter/plugin/FormatKotlinTaskTest.kt +++ b/plugin/src/test/kotlin/org/kotlin/formatter/plugin/FormatKotlinTaskTest.kt @@ -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 @@ -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 {