Skip to content

Commit

Permalink
#292 add test to verify if the plugin detects sources correctly if th…
Browse files Browse the repository at this point in the history
…e project is using a flattened project structure
  • Loading branch information
simonhauck committed Jul 17, 2024
1 parent 21d4b27 commit 0e8fa8e
Showing 1 changed file with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.google.common.truth.Truth.assertThat
import java.io.File
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.gradle.testkit.runner.TaskOutcome.FAILED
import org.gradle.testkit.runner.TaskOutcome.FROM_CACHE
import org.gradle.testkit.runner.TaskOutcome.SUCCESS
Expand All @@ -21,8 +22,6 @@ internal class KtfmtCheckTaskIntegrationTest {

@BeforeEach
fun setUp() {
File(tempDir, "src/main/java").mkdirs()
File(tempDir, "src/test/java").mkdirs()
File("src/test/resources/jvmProject").copyRecursively(tempDir)
}

Expand Down Expand Up @@ -257,13 +256,50 @@ internal class KtfmtCheckTaskIntegrationTest {
assertThat(result!!.output).contains("Reusing configuration cache.")
}

@Test
fun `check task should detect the source and test files in a flattened project structure`() {
appendToBuildGradle("""
|kotlin {
| sourceSets.main {
| kotlin.srcDirs("src")
| }
| sourceSets.test {
| kotlin.srcDirs("test")
| }
|}
""".trimMargin())

createTempFile("val answer = 42", path = "src/someFolder")
createTempFile("val answer = 42", path = "test/someOtherFolder")

val result =
GradleRunner.create()
.withProjectDir(tempDir)
.withPluginClasspath()
.withArguments("ktfmtCheck")
.build()

assertThat(result.task(":ktfmtCheckMain")?.outcome).isNotEqualTo(TaskOutcome.NO_SOURCE)
assertThat(result.task(":ktfmtCheckTest")?.outcome).isNotEqualTo(TaskOutcome.NO_SOURCE)
}

private fun appendToBuildGradle(content: String) {
tempDir.resolve("build.gradle.kts").apply {
appendText(System.lineSeparator())
appendText(content)
}
}


private fun createTempFile(
@Language("kotlin") content: String,
fileName: String = "TestFile.kt",
path: String = "src/main/java"
) =
File(File(tempDir, path), fileName).apply {
): File {
return tempDir.resolve(path).resolve(fileName).apply {
parentFile.mkdirs()
createNewFile()
writeText(content)
}
}
}

0 comments on commit 0e8fa8e

Please sign in to comment.