|
| 1 | +package kotlinx.validation.test |
| 2 | + |
| 3 | +import dev.adamko.kotlin.binary_compatibility_validator.test.utils.* |
| 4 | +import io.kotest.assertions.asClue |
| 5 | +import io.kotest.assertions.withClue |
| 6 | +import io.kotest.core.spec.style.FunSpec |
| 7 | +import io.kotest.engine.spec.tempdir |
| 8 | +import io.kotest.matchers.paths.shouldBeAFile |
| 9 | +import io.kotest.matchers.paths.shouldNotExist |
| 10 | +import io.kotest.matchers.shouldBe |
| 11 | +import java.nio.file.Path |
| 12 | +import kotlin.io.path.readText |
| 13 | +import org.gradle.testkit.runner.TaskOutcome.SUCCESS |
| 14 | + |
| 15 | +class JavaTestFixturesTest : FunSpec({ |
| 16 | + |
| 17 | + context("when a project has the java-test-fixtures plugin applied") { |
| 18 | + |
| 19 | + context("and the testFixtures BCV target is enabled") { |
| 20 | + val project = createTestFixturesProject(bcvTestFixturesTargetEnabled = true) |
| 21 | + |
| 22 | + test("expect :apiDump task passes") { |
| 23 | + project.runner.withArguments("apiDump").build { |
| 24 | + withClue(output) { |
| 25 | + task(":apiDump") shouldHaveOutcome SUCCESS |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + test("expect :apiCheck task passes") { |
| 31 | + project.runner.withArguments("apiCheck").build { |
| 32 | + withClue(output) { |
| 33 | + task(":apiCheck") shouldHaveOutcome SUCCESS |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + test("expect correct kotlinJvm api declaration is generated") { |
| 39 | + project.projectDir.resolve("api/kotlinJvm/java-test-fixtures-test.api").asClue { |
| 40 | + it.shouldBeAFile() |
| 41 | + it.readText().invariantNewlines() shouldBe /* language=TEXT */ """ |
| 42 | + |public final class Hello { |
| 43 | + | public fun <init> (Ljava/lang/String;)V |
| 44 | + | public final fun greeting ()Ljava/lang/String; |
| 45 | + |} |
| 46 | + | |
| 47 | + | |
| 48 | + """.trimMargin() |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + test("expect correct testFixtures api declaration is generated") { |
| 53 | + project.projectDir.resolve("api/testFixtures/java-test-fixtures-test.api").asClue { |
| 54 | + it.shouldBeAFile() |
| 55 | + it.readText().invariantNewlines() shouldBe /* language=TEXT */ """ |
| 56 | + |public final class HelloHelperKt { |
| 57 | + | public static final fun standardHello ()LHello; |
| 58 | + |} |
| 59 | + | |
| 60 | + | |
| 61 | + """.trimMargin() |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + context("and the testFixtures BCV target is disabled") { |
| 66 | + val project = createTestFixturesProject(bcvTestFixturesTargetEnabled = false) |
| 67 | + |
| 68 | + test("expect :apiDump task passes") { |
| 69 | + project.runner.withArguments("apiDump").build { |
| 70 | + withClue(output) { |
| 71 | + task(":apiDump") shouldHaveOutcome SUCCESS |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + test("expect :apiCheck task passes") { |
| 77 | + project.runner.withArguments("apiCheck").build { |
| 78 | + withClue(output) { |
| 79 | + task(":apiCheck") shouldHaveOutcome SUCCESS |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + test("expect correct kotlinJvm api declaration is generated") { |
| 85 | + project.projectDir.resolve("api/java-test-fixtures-test.api").asClue { |
| 86 | + it.shouldBeAFile() |
| 87 | + it.readText().invariantNewlines() shouldBe /* language=TEXT */ """ |
| 88 | + |public final class Hello { |
| 89 | + | public fun <init> (Ljava/lang/String;)V |
| 90 | + | public final fun greeting ()Ljava/lang/String; |
| 91 | + |} |
| 92 | + | |
| 93 | + | |
| 94 | + """.trimMargin() |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + test("expect testFixtures api declaration is not generated") { |
| 99 | + project.projectDir.resolve("api/testFixtures/java-test-fixtures-test.api") |
| 100 | + .shouldNotExist() |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +}) |
| 105 | + |
| 106 | + |
| 107 | +private fun FunSpec.createTestFixturesProject( |
| 108 | + bcvTestFixturesTargetEnabled: Boolean, |
| 109 | + projectDir: Path = tempdir().toPath() |
| 110 | +): GradleProjectTest { |
| 111 | + return gradleKtsProjectTest("java-test-fixtures-test", projectDir) { |
| 112 | + |
| 113 | + buildGradleKts = """ |
| 114 | + |plugins { |
| 115 | + | kotlin("jvm") version "1.7.10" |
| 116 | + | id("dev.adamko.kotlin.binary-compatibility-validator") version "0.0.1-SNAPSHOT" |
| 117 | + | `java-test-fixtures` |
| 118 | + |} |
| 119 | + | |
| 120 | + |binaryCompatibilityValidator { |
| 121 | + | testFixtures { |
| 122 | + | enabled.set($bcvTestFixturesTargetEnabled) |
| 123 | + | } |
| 124 | + |} |
| 125 | + | |
| 126 | + """.trimMargin() |
| 127 | + |
| 128 | + dir("src/main/kotlin") { |
| 129 | + createKotlinFile( |
| 130 | + "Hello.kt", |
| 131 | + """ |
| 132 | + |class Hello(private val response: String) { |
| 133 | + | fun greeting() = response |
| 134 | + |} |
| 135 | + | |
| 136 | + """.trimMargin() |
| 137 | + ) |
| 138 | + } |
| 139 | + dir("src/testFixtures/kotlin") { |
| 140 | + createKotlinFile( |
| 141 | + "HelloHelper.kt", |
| 142 | + """ |
| 143 | + |fun standardHello() = Hello("standard") |
| 144 | + | |
| 145 | + """.trimMargin() |
| 146 | + ) |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments