From 81a4a5cf0152cd926dab1a7574c2ef78ba304217 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Mon, 4 Nov 2024 22:48:54 -0500 Subject: [PATCH] Add basic support for JUnit 5 https://github.com/cashapp/burst/issues/35 This is missing the ability to mix and match parameters supported by Burst with parameters supported by JUnit 5. --- .../burst/gradle/BurstGradlePluginTest.kt | 19 +++++++++ .../src/test/projects/junit5/build.gradle.kts | 41 +++++++++++++++++++ .../test/projects/junit5/lib/build.gradle.kts | 8 ++++ .../junit5/lib/src/test/kotlin/CoffeeTest.kt | 22 ++++++++++ .../test/projects/junit5/settings.gradle.kts | 9 ++++ .../kotlin/app/cash/burst/kotlin/BurstApis.kt | 10 ++++- 6 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 burst-gradle-plugin/src/test/projects/junit5/build.gradle.kts create mode 100644 burst-gradle-plugin/src/test/projects/junit5/lib/build.gradle.kts create mode 100644 burst-gradle-plugin/src/test/projects/junit5/lib/src/test/kotlin/CoffeeTest.kt create mode 100644 burst-gradle-plugin/src/test/projects/junit5/settings.gradle.kts diff --git a/burst-gradle-plugin/src/test/kotlin/app/cash/burst/gradle/BurstGradlePluginTest.kt b/burst-gradle-plugin/src/test/kotlin/app/cash/burst/gradle/BurstGradlePluginTest.kt index 736b1e2..79c686c 100644 --- a/burst-gradle-plugin/src/test/kotlin/app/cash/burst/gradle/BurstGradlePluginTest.kt +++ b/burst-gradle-plugin/src/test/kotlin/app/cash/burst/gradle/BurstGradlePluginTest.kt @@ -261,6 +261,25 @@ class BurstGradlePluginTest { } } + @Test + fun junit5() { + val projectDir = File("src/test/projects/junit5") + + val taskName = ":lib:test" + val result = createRunner(projectDir, "clean", taskName).build() + assertThat(result.task(taskName)!!.outcome).isIn(*SUCCESS_OUTCOMES) + + val testResults = projectDir.resolve("lib/build/test-results") + + with(readTestSuite(testResults.resolve("test/TEST-CoffeeTest_Regular.xml"))) { + assertThat(testCases.map { it.name }).containsExactlyInAnyOrder( + "test_Milk()", + "test_None()", + "test_Oat()", + ) + } + } + private fun createRunner( projectDir: File, vararg taskNames: String, diff --git a/burst-gradle-plugin/src/test/projects/junit5/build.gradle.kts b/burst-gradle-plugin/src/test/projects/junit5/build.gradle.kts new file mode 100644 index 0000000..738b082 --- /dev/null +++ b/burst-gradle-plugin/src/test/projects/junit5/build.gradle.kts @@ -0,0 +1,41 @@ +import org.jetbrains.kotlin.gradle.dsl.JvmTarget +import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile + +buildscript { + repositories { + maven { + url = file("$rootDir/../../../../../build/testMaven").toURI() + } + mavenCentral() + google() + } + dependencies { + classpath("app.cash.burst:burst-gradle-plugin:${project.property("burstVersion")}") + classpath(libs.kotlin.gradlePlugin) + } +} + +allprojects { + repositories { + maven { + url = file("$rootDir/../../../../../build/testMaven").toURI() + } + mavenCentral() + google() + } + + tasks.withType(JavaCompile::class.java).configureEach { + sourceCompatibility = "1.8" + targetCompatibility = "1.8" + } + + tasks.withType(KotlinJvmCompile::class.java).configureEach { + compilerOptions { + jvmTarget.set(JvmTarget.JVM_1_8) + } + } + + tasks.withType { + useJUnitPlatform() + } +} diff --git a/burst-gradle-plugin/src/test/projects/junit5/lib/build.gradle.kts b/burst-gradle-plugin/src/test/projects/junit5/lib/build.gradle.kts new file mode 100644 index 0000000..36cb092 --- /dev/null +++ b/burst-gradle-plugin/src/test/projects/junit5/lib/build.gradle.kts @@ -0,0 +1,8 @@ +plugins { + kotlin("jvm") + id("app.cash.burst") +} + +dependencies { + testImplementation(kotlin("test")) +} diff --git a/burst-gradle-plugin/src/test/projects/junit5/lib/src/test/kotlin/CoffeeTest.kt b/burst-gradle-plugin/src/test/projects/junit5/lib/src/test/kotlin/CoffeeTest.kt new file mode 100644 index 0000000..9833508 --- /dev/null +++ b/burst-gradle-plugin/src/test/projects/junit5/lib/src/test/kotlin/CoffeeTest.kt @@ -0,0 +1,22 @@ +import app.cash.burst.Burst +import kotlin.test.BeforeTest +import kotlin.test.Test + +@Burst +class CoffeeTest( + private val espresso: Espresso, +) { + @BeforeTest + fun setUp() { + println("set up $espresso") + } + + @Test + fun test(dairy: Dairy) { + println("running $espresso $dairy") + } +} + +enum class Espresso { Decaf, Regular, Double } + +enum class Dairy { None, Milk, Oat } diff --git a/burst-gradle-plugin/src/test/projects/junit5/settings.gradle.kts b/burst-gradle-plugin/src/test/projects/junit5/settings.gradle.kts new file mode 100644 index 0000000..78eeed4 --- /dev/null +++ b/burst-gradle-plugin/src/test/projects/junit5/settings.gradle.kts @@ -0,0 +1,9 @@ +dependencyResolutionManagement { + versionCatalogs { + create("libs") { + from(files("../../../../../gradle/libs.versions.toml")) + } + } +} + +include(":lib") diff --git a/burst-kotlin-plugin/src/main/kotlin/app/cash/burst/kotlin/BurstApis.kt b/burst-kotlin-plugin/src/main/kotlin/app/cash/burst/kotlin/BurstApis.kt index 7e5615a..9b50fa0 100644 --- a/burst-kotlin-plugin/src/main/kotlin/app/cash/burst/kotlin/BurstApis.kt +++ b/burst-kotlin-plugin/src/main/kotlin/app/cash/burst/kotlin/BurstApis.kt @@ -37,6 +37,10 @@ internal class BurstApis private constructor( return BurstApis(pluginContext, junitPackage) } + if (pluginContext.referenceClass(junit5TestClassId) != null) { + return BurstApis(pluginContext, junit5Package) + } + if (pluginContext.referenceClass(kotlinTestClassId) != null) { return BurstApis(pluginContext, kotlinTestPackage) } @@ -56,11 +60,15 @@ private val burstValuesId = burstFqPackage.callableId("burstValues") private val junitPackage = FqPackageName("org.junit") private val junitTestClassId = junitPackage.classId("Test") +private val junit5Package = FqPackageName("org.junit.jupiter.api") +private val junit5TestClassId = junit5Package.classId("Test") private val kotlinTestPackage = FqPackageName("kotlin.test") private val kotlinTestClassId = kotlinTestPackage.classId("Test") internal val IrAnnotationContainer.hasAtTest: Boolean - get() = hasAnnotation(junitTestClassId) || hasAnnotation(kotlinTestClassId) + get() = hasAnnotation(junitTestClassId) || + hasAnnotation(junit5TestClassId) || + hasAnnotation(kotlinTestClassId) internal val IrAnnotationContainer.hasAtBurst: Boolean get() = hasAnnotation(burstAnnotationId)