From 2cf97a181fbd30e508a3f24f363e342a620e8b67 Mon Sep 17 00:00:00 2001 From: Him188 Date: Mon, 17 Apr 2023 15:49:57 +0100 Subject: [PATCH] [build] Use JUnit5 on Android --- buildSrc/src/main/kotlin/HmppConfigure.kt | 36 ++++++++++++++++--- gradle.properties | 6 ++-- mirai-core-api/build.gradle.kts | 2 +- mirai-core-utils/build.gradle.kts | 28 +++++++-------- mirai-core/build.gradle.kts | 3 +- .../kotlin/test/initPlatform.android.kt | 4 +-- 6 files changed, 54 insertions(+), 25 deletions(-) diff --git a/buildSrc/src/main/kotlin/HmppConfigure.kt b/buildSrc/src/main/kotlin/HmppConfigure.kt index a1a1c16aa..44e50f4b0 100644 --- a/buildSrc/src/main/kotlin/HmppConfigure.kt +++ b/buildSrc/src/main/kotlin/HmppConfigure.kt @@ -14,10 +14,7 @@ import com.google.gradle.osdetector.OsDetector import org.gradle.api.JavaVersion import org.gradle.api.Project import org.gradle.api.attributes.Attribute -import org.gradle.kotlin.dsl.extra -import org.gradle.kotlin.dsl.get -import org.gradle.kotlin.dsl.getting -import org.gradle.kotlin.dsl.withType +import org.gradle.kotlin.dsl.* import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation.Companion.MAIN_COMPILATION_NAME import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation.Companion.TEST_COMPILATION_NAME @@ -203,6 +200,12 @@ fun Project.configureJvmTargetsHierarchical() { // this can cause problems on sync // for (s in arrayOf("androidDebug", "androidRelease")) { // sourceSets.all { if (name in s) dependsOn(androidMain) } +// } + +// we should have added a "androidBaseTest" (or "androidTest") for "androidUnitTest" and "androidInstrumentedTest", +// but this currently cause bugs in IntelliJ (2023.2) +// val androidBaseTest = sourceSets.maybeCreate("androidBaseTest").apply { +// dependsOn(jvmBaseTest) // } val androidUnitTest by sourceSets.getting { dependsOn(jvmBaseTest) @@ -279,8 +282,33 @@ fun Project.configureAndroidTarget() { ) } } + +// if (USE_JUNIT5_FOR_ANDROID_TEST) { +// extensions.getByType(LibraryExtension::class.java).apply { +// defaultConfig { +// // 1) Make sure to use the AndroidJUnitRunner, or a subclass of it. This requires a dependency on androidx.test:runner, too! +// testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" +// // 2) Connect JUnit 5 to the runner +// testInstrumentationRunnerArguments["runnerBuilder"] = "de.mannodermaus.junit5.AndroidJUnit5Builder" +// } +// } +// +// dependencies { +// // 4) Jupiter API & Test Runner, if you don't have it already +// "androidTestImplementation"("androidx.test:runner:1.5.2") +// "androidTestImplementation"("org.junit.jupiter:junit-jupiter-api:5.9.2") + // runtimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.2") +// +// // 5) The instrumentation test companion libraries +// "androidTestImplementation"("de.mannodermaus.junit5:android-test-core:1.3.0") +// "androidTestRuntimeOnly"("de.mannodermaus.junit5:android-test-runner:1.3.0") +// } +// } + } +private const val USE_JUNIT5_FOR_ANDROID_TEST = true + /** * ``` diff --git a/gradle.properties b/gradle.properties index f3e7b5391..98fd2f018 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,7 +17,7 @@ kotlin.native.binary.memoryModel=experimental systemProp.org.gradle.internal.publish.checksums.insecure=true gnsp.disableApplyOnlyOnRootProjectEnforcement=true # We may target 15 with Kotlin 1.5 IR -mirai.android.target.api.level=24 +mirai.android.target.api.level=26 # Enable if you want to use mavenLocal for both Gradle plugin and project dependencies resolutions. systemProp.use.maven.local=false org.gradle.caching=true @@ -25,5 +25,5 @@ kotlin.native.ignoreIncorrectDependencies=true kotlin.mpp.enableCInteropCommonization=true kotlin.mpp.stability.nowarn=true kotlin.mpp.androidSourceSetLayoutVersion=2 - -android.disableAutomaticComponentCreation=true \ No newline at end of file +android.disableAutomaticComponentCreation=true +android.useAndroidX=true \ No newline at end of file diff --git a/mirai-core-api/build.gradle.kts b/mirai-core-api/build.gradle.kts index 4472d43fb..9c93a03ed 100644 --- a/mirai-core-api/build.gradle.kts +++ b/mirai-core-api/build.gradle.kts @@ -117,7 +117,7 @@ if (tasks.findByName("androidMainClasses") != null) { group = "verification" this.mustRunAfter("androidMainClasses") } - tasks.getByName("androidTest").dependsOn("checkAndroidApiLevel") + tasks.getByName("androidBaseTest").dependsOn("checkAndroidApiLevel") } configureMppPublishing() diff --git a/mirai-core-utils/build.gradle.kts b/mirai-core-utils/build.gradle.kts index 90bbada6b..cf1cc6656 100644 --- a/mirai-core-utils/build.gradle.kts +++ b/mirai-core-utils/build.gradle.kts @@ -90,20 +90,20 @@ kotlin { } } -//if (tasks.findByName("androidMainClasses") != null) { -// tasks.register("checkAndroidApiLevel") { -// doFirst { -// analyzes.AndroidApiLevelCheck.check( -// buildDir.resolve("classes/kotlin/android/main"), -// project.property("mirai.android.target.api.level")!!.toString().toInt(), -// project -// ) -// } -// group = "verification" -// this.mustRunAfter("androidMainClasses") -// } -// tasks.getByName("androidTest").dependsOn("checkAndroidApiLevel") -//} +if (tasks.findByName("androidMainClasses") != null) { + tasks.register("checkAndroidApiLevel") { + doFirst { + analyzes.AndroidApiLevelCheck.check( + buildDir.resolve("classes/kotlin/android/main"), + project.property("mirai.android.target.api.level")!!.toString().toInt(), + project + ) + } + group = "verification" + this.mustRunAfter("androidMainClasses") + } + tasks.getByName("androidBaseTest").dependsOn("checkAndroidApiLevel") +} //configureMppPublishing() diff --git a/mirai-core/build.gradle.kts b/mirai-core/build.gradle.kts index 943030d6a..e83d7fce0 100644 --- a/mirai-core/build.gradle.kts +++ b/mirai-core/build.gradle.kts @@ -22,6 +22,7 @@ plugins { id("me.him188.kotlin-dynamic-delegation") // id("me.him188.maven-central-publish") `maven-publish` + id("de.mannodermaus.android-junit5") version "1.8.2.1" } description = "Mirai Protocol implementation for QQ Android" @@ -243,7 +244,7 @@ if (tasks.findByName("androidMainClasses") != null) { group = "verification" this.mustRunAfter("androidMainClasses") } - tasks.getByName("androidTest").dependsOn("checkAndroidApiLevel") + tasks.getByName("androidBaseTest").dependsOn("checkAndroidApiLevel") } configureMppPublishing() diff --git a/mirai-core/src/androidInstrumentedTest/kotlin/test/initPlatform.android.kt b/mirai-core/src/androidInstrumentedTest/kotlin/test/initPlatform.android.kt index 6557d558d..8a2ce3a91 100644 --- a/mirai-core/src/androidInstrumentedTest/kotlin/test/initPlatform.android.kt +++ b/mirai-core/src/androidInstrumentedTest/kotlin/test/initPlatform.android.kt @@ -11,7 +11,7 @@ package net.mamoe.mirai.internal.test //import org.bouncycastle.jce.provider.BouncyCastleProvider import net.mamoe.mirai.utils.MiraiLogger -import org.junit.jupiter.api.Test +import org.junit.Test import kotlin.test.assertIs internal actual fun initPlatform() { @@ -29,7 +29,7 @@ private val init: Unit by lazy { internal actual class PlatformInitializationTest : AbstractTest() { - @Test + @org.junit.jupiter.api.Test actual fun test() { @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") assertIs(MiraiLogger.Factory.create(this::class, "1"))