Jelajahi Sumber

[build] Make mirai-core `AbstractTest` public, and add `dependsOnCoreJvmTest` in buildSrc

Him188 3 tahun lalu
induk
melakukan
c74e9d2858

+ 41 - 0
buildSrc/src/main/kotlin/TestDependencies.kt

@@ -0,0 +1,41 @@
+/*
+ * Copyright 2019-2023 Mamoe Technologies and contributors.
+ *
+ * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
+ * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
+ *
+ * https://github.com/mamoe/mirai/blob/dev/LICENSE
+ */
+
+import org.gradle.api.Project
+import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
+
+/**
+ * 配置 test 依赖 mirai-core:jvmTest, 可访问 `AbstractTest` 等
+ * 
+ * 用法:
+ * 
+ * *build.gradle.kts*
+ * ```
+ * kotlin.sourceSets.test {
+ *     dependsOnCoreJvmTest(project)
+ * }
+ * ```
+ */
+fun KotlinSourceSet.dependsOnCoreJvmTest(project: Project) {
+    project.evaluationDependsOn(":mirai-core")
+    dependencies {
+        implementation(
+            project(":mirai-core").dependencyProject.kotlinMpp!!.targets
+                .single { it.name == "jvm" }
+                .compilations.getByName("test")
+                .output.allOutputs
+        )
+        implementation(
+            project(":mirai-core").dependencyProject.kotlinMpp!!.targets
+                .single { it.name == "jvmBase" }
+                .compilations.getByName("test")
+                .output.allOutputs
+        )
+    }
+}

+ 3 - 25
mirai-core/src/commonTest/kotlin/test/initPlatform.common.kt

@@ -1,5 +1,5 @@
 /*
- * Copyright 2019-2022 Mamoe Technologies and contributors.
+ * Copyright 2019-2023 Mamoe Technologies and contributors.
  *
  * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@@ -9,38 +9,16 @@
 
 package net.mamoe.mirai.internal.test
 
-import kotlinx.coroutines.CloseableCoroutineDispatcher
 import kotlinx.coroutines.CoroutineDispatcher
-import kotlinx.coroutines.DelicateCoroutinesApi
-import kotlinx.coroutines.ExperimentalCoroutinesApi
-import kotlinx.coroutines.test.StandardTestDispatcher
-import kotlin.test.AfterTest
 import kotlin.test.Test
 
 internal expect fun initPlatform()
 
-
-@Suppress("UnnecessaryOptInAnnotation") // on JVM
-@OptIn(ExperimentalCoroutinesApi::class, DelicateCoroutinesApi::class)
-internal abstract class CommonAbstractTest {
-    private val dispatchers = mutableListOf<CloseableCoroutineDispatcher>()
-
-    fun borrowSingleThreadDispatcher(): CoroutineDispatcher {
-        return StandardTestDispatcher()
-    }
-
-    @AfterTest
-    fun closeAllDispatchers() {
-        for (dispatcher in dispatchers) {
-            dispatcher.close()
-        }
-    }
-}
-
 /**
  * All test classes should inherit from [AbstractTest]
  */
-internal expect abstract class AbstractTest() : CommonAbstractTest {
+expect abstract class AbstractTest() { // public, can be used in other modules
+    fun borrowSingleThreadDispatcher(): CoroutineDispatcher
 
     companion object
 }

+ 2 - 2
mirai-core/src/commonTest/kotlin/testFramework/DebugProbes.kt

@@ -1,5 +1,5 @@
 /*
- * Copyright 2019-2022 Mamoe Technologies and contributors.
+ * Copyright 2019-2023 Mamoe Technologies and contributors.
  *
  * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@@ -16,7 +16,7 @@ import kotlinx.coroutines.Job
  * Mirror of kotlinx-coroutines-debug to be used in common sources.
  */
 @Suppress("RedundantSetter", "RedundantGetter")
-internal expect object DebugProbes {
+expect object DebugProbes {
 
     /**
      * Whether coroutine creation stack traces should be sanitized.

+ 9 - 4
mirai-core/src/jvmBaseTest/kotlin/test/AbstractTest.kt

@@ -1,5 +1,5 @@
 /*
- * Copyright 2019-2022 Mamoe Technologies and contributors.
+ * Copyright 2019-2023 Mamoe Technologies and contributors.
  *
  * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@@ -9,8 +9,10 @@
 
 package net.mamoe.mirai.internal.test
 
+import kotlinx.coroutines.CoroutineDispatcher
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.debug.DebugProbes
+import kotlinx.coroutines.test.StandardTestDispatcher
 import net.mamoe.mirai.IMirai
 import net.mamoe.mirai.internal.network.framework.SynchronizedStdoutLogger
 import net.mamoe.mirai.internal.testFramework.TestFactory
@@ -26,10 +28,13 @@ import kotlin.reflect.full.functions
 import kotlin.reflect.full.hasAnnotation
 
 @Timeout(value = 7, unit = TimeUnit.MINUTES)
-internal actual abstract class AbstractTest actual constructor() : CommonAbstractTest() {
+actual abstract class AbstractTest actual constructor() {
+    actual fun borrowSingleThreadDispatcher(): CoroutineDispatcher {
+        return StandardTestDispatcher()
+    }
+
     @OptIn(ExperimentalCoroutinesApi::class)
     actual companion object {
-        @OptIn(ExperimentalStdlibApi::class)
         @BeforeAll
         @JvmStatic
         fun checkTestFactories(testInfo: TestInfo) {
@@ -49,7 +54,7 @@ internal actual abstract class AbstractTest actual constructor() : CommonAbstrac
         init {
             initPlatform()
 
-            DebugProbes.install()
+            kotlin.runCatching { DebugProbes.install() }
 
             @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
             net.mamoe.mirai.utils.MiraiLoggerFactoryImplementationBridge.wrapCurrent {

+ 3 - 3
mirai-core/src/jvmBaseTest/kotlin/testFramework/DebugProbes.kt

@@ -1,5 +1,5 @@
 /*
- * Copyright 2019-2022 Mamoe Technologies and contributors.
+ * Copyright 2019-2023 Mamoe Technologies and contributors.
  *
  * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@@ -11,6 +11,6 @@ package net.mamoe.mirai.internal.testFramework
 
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 
-@Suppress("ACTUAL_WITHOUT_EXPECT")
+@Suppress("ACTUAL_WITHOUT_EXPECT", "NO_ACTUAL_CLASS_MEMBER_FOR_EXPECTED_CLASS")
 @OptIn(ExperimentalCoroutinesApi::class)
-internal actual typealias DebugProbes = kotlinx.coroutines.debug.DebugProbes
+actual typealias DebugProbes = kotlinx.coroutines.debug.DebugProbes

+ 2 - 2
mirai-core/src/jvmTest/kotlin/bootstrap/RunMessageDecodingRecorder.kt

@@ -1,5 +1,5 @@
 /*
- * Copyright 2019-2022 Mamoe Technologies and contributors.
+ * Copyright 2019-2023 Mamoe Technologies and contributors.
  *
  * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@@ -21,7 +21,7 @@ import net.mamoe.mirai.utils.readResource
 import net.mamoe.yamlkt.Yaml
 import kotlin.concurrent.thread
 
-suspend fun main() {
+internal suspend fun main() {
     Runtime.getRuntime().addShutdownHook(thread(start = false) {
         Bot.instances.forEach {
             it.close()

+ 3 - 3
mirai-core/src/jvmTest/kotlin/bootstrap/RunNoticeRecorder.kt

@@ -1,5 +1,5 @@
 /*
- * Copyright 2019-2022 Mamoe Technologies and contributors.
+ * Copyright 2019-2023 Mamoe Technologies and contributors.
  *
  * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@@ -22,12 +22,12 @@ import net.mamoe.yamlkt.Yaml
 import kotlin.concurrent.thread
 
 @Serializable
-data class LocalAccount(
+internal data class LocalAccount(
     val id: Long,
     val password: String
 )
 
-suspend fun main() {
+internal suspend fun main() {
     Runtime.getRuntime().addShutdownHook(thread(start = false) {
         Bot.instances.forEach {
             it.close()

+ 6 - 2
mirai-core/src/nativeTest/kotlin/test/PlatformInitializationTest.kt

@@ -1,5 +1,5 @@
 /*
- * Copyright 2019-2022 Mamoe Technologies and contributors.
+ * Copyright 2019-2023 Mamoe Technologies and contributors.
  *
  * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@@ -9,6 +9,8 @@
 
 package net.mamoe.mirai.internal.test
 
+import kotlinx.coroutines.CoroutineDispatcher
+import kotlinx.coroutines.test.StandardTestDispatcher
 import net.mamoe.mirai.IMirai
 import net.mamoe.mirai.internal.initMirai
 import net.mamoe.mirai.utils.setSystemProp
@@ -31,7 +33,9 @@ internal actual class PlatformInitializationTest actual constructor() : Abstract
  * Note: To run a test in native sourceSets, use IDEA key shortcut 'control + shift + R' on macOS and 'Ctrl + Shift + R' on Windows.
  * Or you can right-click the function name of the test case and invoke 'Run ...'. You should not expect to see a button icon around the line numbers.
  */
-internal actual abstract class AbstractTest actual constructor() : CommonAbstractTest() {
+actual abstract class AbstractTest actual constructor() {
+    actual fun borrowSingleThreadDispatcher(): CoroutineDispatcher = StandardTestDispatcher()
+
     init {
         Companion
     }

+ 2 - 2
mirai-core/src/nativeTest/kotlin/testFramework/DebugProbes.kt

@@ -1,5 +1,5 @@
 /*
- * Copyright 2019-2022 Mamoe Technologies and contributors.
+ * Copyright 2019-2023 Mamoe Technologies and contributors.
  *
  * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@@ -13,7 +13,7 @@ import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.Job
 
 @Suppress("UNUSED_PARAMETER", "unused")
-internal actual object DebugProbes {
+actual object DebugProbes {
 
     /**
      * Prints  hierarchy representation from [jobToString] to the given .