Skip to content

Commit

Permalink
Create tests for script tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Roggstars committed Nov 26, 2024
1 parent 27ddc95 commit 6a86702
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,40 @@ internal class KtfmtCheckTaskIntegrationTest {
assertThat(result.task(":ktfmtCheckMain")?.outcome).isEqualTo(NO_SOURCE)
}

@Test
fun `check script task should validate top-level script file`() {
createTempFile(content = "val answer=42\n", fileName = "TestFile.kts", path = "")

val result =
GradleRunner.create()
.withProjectDir(tempDir)
.withPluginClasspath()
.withArguments("ktfmtCheckScript")
.buildAndFail()

assertThat(result.task(":ktfmtCheckScript")?.outcome).isEqualTo(FAILED)
assertThat(result.output).containsMatch("Invalid formatting for: .*TestFile.kts")
}

@Test
fun `check script task should ignore non top-level script files`() {
createTempFile(
content = "val answer=42\n",
fileName = "TestFile.kts",
path = "src/main/java",
)

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

assertThat(result.task(":ktfmtCheckScript")?.outcome).isEqualTo(SUCCESS)
}

private fun appendToBuildGradle(content: String) {
tempDir.resolve("build.gradle.kts").apply {
appendText(System.lineSeparator())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,57 @@ internal class KtfmtFormatTaskIntegrationTest {
assertThat(result.task(":ktfmtFormatMain")?.outcome).isEqualTo(NO_SOURCE)
}

@Test
fun `format script task should fail if top-level script file could not be parsed`() {
val scriptFile = createTempFile(content = "val answer=\n", fileName = "my.kts", path = "")

val result =
GradleRunner.create()
.withProjectDir(tempDir)
.withPluginClasspath()
.withArguments("ktfmtFormatScript")
.forwardOutput()
.buildAndFail()

val actual = scriptFile.readText()
assertThat(actual).isEqualTo("val answer=\n")
assertThat(result.task(":ktfmtFormatScript")?.outcome).isEqualTo(FAILED)
}

@Test
fun `format script task should format top-level script file`() {
val scriptFile = createTempFile(content = "val answer=42\n", fileName = "my.kts", path = "")

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

val actual = scriptFile.readText()
assertThat(actual).isEqualTo("val answer = 42\n")
assertThat(result.task(":ktfmtFormatScript")?.outcome).isEqualTo(SUCCESS)
}

@Test
fun `format script task should not format non top-level script file`() {
val scriptFile = createTempFile(content = "val answer=42\n", fileName = "my.kts")

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

val actual = scriptFile.readText()
assertThat(actual).isEqualTo("val answer=42\n")
assertThat(result.task(":ktfmtFormatScript")?.outcome).isEqualTo(SUCCESS)
}

private fun createTempFile(
@Language("kotlin") content: String,
fileName: String = "TestFile.kt",
Expand All @@ -462,11 +513,10 @@ internal class KtfmtFormatTaskIntegrationTest {
writeText(content)
}

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

0 comments on commit 6a86702

Please sign in to comment.