diff --git a/mirai-console/backend/mirai-console/test/TestMiraiConosle.kt b/mirai-console/backend/mirai-console/test/TestMiraiConosle.kt index 0d633821a..f6a2fab17 100644 --- a/mirai-console/backend/mirai-console/test/TestMiraiConosle.kt +++ b/mirai-console/backend/mirai-console/test/TestMiraiConosle.kt @@ -31,69 +31,6 @@ import kotlin.coroutines.resume import kotlin.io.path.createTempDirectory import kotlin.test.assertNotNull -@OptIn(ConsoleInternalApi::class, kotlin.io.path.ExperimentalPathApi::class) -fun initTestEnvironment() { - object : MiraiConsoleImplementation { - override val rootPath: Path = createTempDirectory() - - override val frontEndDescription: MiraiConsoleFrontEndDescription - get() = object : MiraiConsoleFrontEndDescription { - override val name: String - get() = "Test" - override val vendor: String - get() = "Test" - override val version: SemVersion - get() = SemVersion("1.0.0") - - } - override val builtInPluginLoaders: List>> = listOf(lazy { JvmPluginLoader }) - override val jvmPluginLoader: JvmPluginLoader by lazy { - backendAccess.createDefaultJvmPluginLoader(coroutineContext) - } - override val consoleCommandSender: MiraiConsoleImplementation.ConsoleCommandSenderImpl = - object : MiraiConsoleImplementation.ConsoleCommandSenderImpl { - override suspend fun sendMessage(message: Message) { - println(message) - } - - override suspend fun sendMessage(message: String) { - println(message) - } - } - override val dataStorageForJvmPluginLoader: PluginDataStorage = MemoryPluginDataStorage() - override val configStorageForJvmPluginLoader: PluginDataStorage = MemoryPluginDataStorage() - override val dataStorageForBuiltIns: PluginDataStorage = MemoryPluginDataStorage() - override val configStorageForBuiltIns: PluginDataStorage = MemoryPluginDataStorage() - - override val consoleInput: ConsoleInput = object : ConsoleInput { - override suspend fun requestInput(hint: String): String { - println(hint) - return readLine() ?: error("No stdin") - } - } - - override fun createLoginSolver(requesterBot: Long, configuration: BotConfiguration): LoginSolver = - LoginSolver.Default!! - - override fun createLogger(identity: String?): MiraiLogger { - return PlatformLogger(identity) - } - - override val consoleDataScope: MiraiConsoleImplementation.ConsoleDataScope by lazy { - MiraiConsoleImplementation.ConsoleDataScope.createDefault( - coroutineContext, - dataStorageForBuiltIns, - configStorageForBuiltIns - ) - } - override val coroutineContext: CoroutineContext = - CoroutineName("Console Main") + SupervisorJob() + CoroutineExceptionHandler { _, throwable -> - throwable.printStackTrace() - } - }.start() - CommandManager -} - internal object Testing { @Volatile internal var cont: Continuation? = null diff --git a/mirai-console/backend/mirai-console/test/command/TestCommand.kt b/mirai-console/backend/mirai-console/test/command/InstanceTestCommand.kt similarity index 99% rename from mirai-console/backend/mirai-console/test/command/TestCommand.kt rename to mirai-console/backend/mirai-console/test/command/InstanceTestCommand.kt index 9a13ddb41..3c933f4d6 100644 --- a/mirai-console/backend/mirai-console/test/command/TestCommand.kt +++ b/mirai-console/backend/mirai-console/test/command/InstanceTestCommand.kt @@ -22,7 +22,7 @@ import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregisterCommand import net.mamoe.mirai.console.command.descriptor.CommandValueArgumentParser import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors import net.mamoe.mirai.console.command.descriptor.buildCommandArgumentContext -import net.mamoe.mirai.console.testFramework.AbstractConsoleTest +import net.mamoe.mirai.console.testFramework.AbstractConsoleInstanceTest import net.mamoe.mirai.console.internal.command.CommandManagerImpl import net.mamoe.mirai.console.internal.command.flattenCommandComponents import net.mamoe.mirai.message.data.* @@ -98,7 +98,7 @@ internal val owner by lazy { TestUnitCommandOwner } @OptIn(ExperimentalCommandDescriptors::class) -internal class TestCommand : AbstractConsoleTest() { +internal class InstanceTestCommand : AbstractConsoleInstanceTest() { @Test fun testRegister() { try { diff --git a/mirai-console/backend/mirai-console/test/data/JavaPluginDescriptionTests.kt b/mirai-console/backend/mirai-console/test/data/JavaPluginDescriptionTests.kt index c8e8f3372..9129497f7 100644 --- a/mirai-console/backend/mirai-console/test/data/JavaPluginDescriptionTests.kt +++ b/mirai-console/backend/mirai-console/test/data/JavaPluginDescriptionTests.kt @@ -8,10 +8,10 @@ */ package net.mamoe.mirai.console.data -import net.mamoe.mirai.console.testFramework.AbstractConsoleTest +import net.mamoe.mirai.console.testFramework.AbstractConsoleInstanceTest @Suppress("unused") -class JavaPluginDescriptionTests : AbstractConsoleTest() { +class JavaPluginDescriptionTests : AbstractConsoleInstanceTest() { // @Test // fun testSimpleValue() { diff --git a/mirai-console/backend/mirai-console/test/data/PluginMovingTests.kt b/mirai-console/backend/mirai-console/test/data/PluginMovingTests.kt index 4ae8f8c35..7b943e6ec 100644 --- a/mirai-console/backend/mirai-console/test/data/PluginMovingTests.kt +++ b/mirai-console/backend/mirai-console/test/data/PluginMovingTests.kt @@ -10,7 +10,7 @@ package net.mamoe.mirai.console.data import net.mamoe.mirai.console.MiraiConsole -import net.mamoe.mirai.console.testFramework.AbstractConsoleTest +import net.mamoe.mirai.console.testFramework.AbstractConsoleInstanceTest import net.mamoe.mirai.console.internal.data.mkdir import net.mamoe.mirai.console.plugin.PluginManager import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.load @@ -21,7 +21,7 @@ import net.mamoe.mirai.console.plugin.name import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test -class PluginMovingTests : AbstractConsoleTest() { +class PluginMovingTests : AbstractConsoleInstanceTest() { private val mockPluginWithName = object : KotlinPlugin(JvmPluginDescription("org.test1.test1", "1.0.0", "test1")) {} private val mockPluginWithName2 = object : KotlinPlugin(JvmPluginDescription("org.test2.test2", "1.0.0", "test2")) {} diff --git a/mirai-console/backend/mirai-console/test/permission/PermissionsBasicsTest.kt b/mirai-console/backend/mirai-console/test/permission/PermissionsBasicsTest.kt index 029e9ceee..a07ce6c77 100644 --- a/mirai-console/backend/mirai-console/test/permission/PermissionsBasicsTest.kt +++ b/mirai-console/backend/mirai-console/test/permission/PermissionsBasicsTest.kt @@ -13,6 +13,7 @@ import org.junit.jupiter.api.Test import kotlin.test.assertFails internal class PermissionsBasicsTest { + @Suppress("ILLEGAL_PERMISSION_NAMESPACE", "ILLEGAL_PERMISSION_NAME") // inspection by mirai-console-intellij @Test fun testInvalidPermissionId() { assertFails { PermissionId("space namespace", "name") } @@ -22,9 +23,4 @@ internal class PermissionsBasicsTest { assertFails { PermissionId("namespace:name", "name") } assertFails { PermissionId("namespace", "namespace:name") } } - - @Test - fun parentsWithSelfSequence() { - - } } \ No newline at end of file diff --git a/mirai-console/backend/mirai-console/test/testFramework/AbstractConsoleTest.kt b/mirai-console/backend/mirai-console/test/testFramework/AbstractConsoleInstanceTest.kt similarity index 69% rename from mirai-console/backend/mirai-console/test/testFramework/AbstractConsoleTest.kt rename to mirai-console/backend/mirai-console/test/testFramework/AbstractConsoleInstanceTest.kt index dc35932ca..66dc5bdc9 100644 --- a/mirai-console/backend/mirai-console/test/testFramework/AbstractConsoleTest.kt +++ b/mirai-console/backend/mirai-console/test/testFramework/AbstractConsoleInstanceTest.kt @@ -14,22 +14,26 @@ import kotlinx.coroutines.cancelAndJoin import kotlinx.coroutines.runBlocking import net.mamoe.mirai.console.MiraiConsole import net.mamoe.mirai.console.MiraiConsoleImplementation -import net.mamoe.mirai.console.initTestEnvironment +import net.mamoe.mirai.console.MiraiConsoleImplementation.Companion.start +import net.mamoe.mirai.console.command.CommandManager import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.BeforeEach -abstract class AbstractConsoleTest { +abstract class AbstractConsoleInstanceTest { val mockPlugin by lazy { mockKotlinPlugin() } + private lateinit var implementation: MiraiConsoleImplementation + val consoleImplementation: MiraiConsoleImplementation by ::implementation @BeforeEach protected open fun initializeConsole() { - initTestEnvironment() + this.implementation = MockConsoleImplementation().apply { start() } + CommandManager } @AfterEach - protected open fun cancelConsole() { + protected open fun stopConsole() { if (MiraiConsoleImplementation.instanceInitialized) { try { runBlocking { MiraiConsole.job.cancelAndJoin() } @@ -44,8 +48,8 @@ abstract class AbstractConsoleTest { } companion object { - fun mockKotlinPlugin(): KotlinPlugin { - return object : KotlinPlugin(JvmPluginDescription("org.test.test", "1.0.0")) {} + fun mockKotlinPlugin(id: String = "org.test.test"): KotlinPlugin { + return object : KotlinPlugin(JvmPluginDescription(id, "1.0.0")) {} } } } \ No newline at end of file diff --git a/mirai-console/backend/mirai-console/test/testFramework/MockConsoleImplementation.kt b/mirai-console/backend/mirai-console/test/testFramework/MockConsoleImplementation.kt new file mode 100644 index 000000000..9ba8d2822 --- /dev/null +++ b/mirai-console/backend/mirai-console/test/testFramework/MockConsoleImplementation.kt @@ -0,0 +1,102 @@ +/* + * Copyright 2019-2021 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 + */ + +package net.mamoe.mirai.console.testFramework + +import kotlinx.coroutines.CoroutineExceptionHandler +import kotlinx.coroutines.CoroutineName +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.cancel +import net.mamoe.mirai.console.MiraiConsoleFrontEndDescription +import net.mamoe.mirai.console.MiraiConsoleImplementation +import net.mamoe.mirai.console.MiraiConsoleImplementation.Companion.start +import net.mamoe.mirai.console.data.MemoryPluginDataStorage +import net.mamoe.mirai.console.data.PluginDataStorage +import net.mamoe.mirai.console.plugin.jvm.JvmPluginLoader +import net.mamoe.mirai.console.plugin.loader.PluginLoader +import net.mamoe.mirai.console.util.ConsoleInput +import net.mamoe.mirai.console.util.SemVersion +import net.mamoe.mirai.message.data.Message +import net.mamoe.mirai.utils.BotConfiguration +import net.mamoe.mirai.utils.LoginSolver +import net.mamoe.mirai.utils.MiraiLogger +import net.mamoe.mirai.utils.PlatformLogger +import java.nio.file.Path +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract +import kotlin.coroutines.CoroutineContext +import kotlin.io.path.createTempDirectory + +open class MockConsoleImplementation : MiraiConsoleImplementation { + override val rootPath: Path = createTempDirectory() + + override val frontEndDescription: MiraiConsoleFrontEndDescription + get() = object : MiraiConsoleFrontEndDescription { + override val name: String get() = "Test" + override val vendor: String get() = "Test" + override val version: SemVersion get() = SemVersion("1.0.0") + } + override val builtInPluginLoaders: List>> = listOf(lazy { JvmPluginLoader }) + override val jvmPluginLoader: JvmPluginLoader by lazy { + backendAccess.createDefaultJvmPluginLoader(coroutineContext) + } + override val consoleCommandSender: MiraiConsoleImplementation.ConsoleCommandSenderImpl = + object : MiraiConsoleImplementation.ConsoleCommandSenderImpl { + override suspend fun sendMessage(message: Message) { + println(message) + } + + override suspend fun sendMessage(message: String) { + println(message) + } + } + override val dataStorageForJvmPluginLoader: PluginDataStorage = MemoryPluginDataStorage() + override val configStorageForJvmPluginLoader: PluginDataStorage = MemoryPluginDataStorage() + override val dataStorageForBuiltIns: PluginDataStorage = MemoryPluginDataStorage() + override val configStorageForBuiltIns: PluginDataStorage = MemoryPluginDataStorage() + + override val consoleInput: ConsoleInput = object : ConsoleInput { + override suspend fun requestInput(hint: String): String { + println(hint) + return readLine() ?: error("No stdin") + } + } + + override fun createLoginSolver(requesterBot: Long, configuration: BotConfiguration): LoginSolver = + LoginSolver.Default!! + + override fun createLogger(identity: String?): MiraiLogger { + return PlatformLogger(identity) + } + + override val consoleDataScope: MiraiConsoleImplementation.ConsoleDataScope by lazy { + MiraiConsoleImplementation.ConsoleDataScope.createDefault( + coroutineContext, + dataStorageForBuiltIns, + configStorageForBuiltIns + ) + } + override val coroutineContext: CoroutineContext = + CoroutineName("Console Main") + SupervisorJob() + CoroutineExceptionHandler { _, throwable -> + throwable.printStackTrace() + } + +} + +inline fun withConsoleImplementation(crossinline block: MockConsoleImplementation.() -> R): R { + contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } + MockConsoleImplementation().run { + start() + try { + return block() + } finally { + cancel() + } + } +} \ No newline at end of file diff --git a/mirai-console/backend/mirai-console/test/testFramework/test/FrameworkTest.kt b/mirai-console/backend/mirai-console/test/testFramework/test/FrameworkInstanceTest.kt similarity index 78% rename from mirai-console/backend/mirai-console/test/testFramework/test/FrameworkTest.kt rename to mirai-console/backend/mirai-console/test/testFramework/test/FrameworkInstanceTest.kt index f81d22e8c..93501f666 100644 --- a/mirai-console/backend/mirai-console/test/testFramework/test/FrameworkTest.kt +++ b/mirai-console/backend/mirai-console/test/testFramework/test/FrameworkInstanceTest.kt @@ -7,14 +7,14 @@ * https://github.com/mamoe/mirai/blob/dev/LICENSE */ -package net.mamoe.mirai.console.framework.test +package net.mamoe.mirai.console.testFramework.test -import net.mamoe.mirai.console.testFramework.AbstractConsoleTest +import net.mamoe.mirai.console.testFramework.AbstractConsoleInstanceTest import net.mamoe.mirai.console.plugin.PluginManager import org.junit.jupiter.api.Test import kotlin.test.assertEquals -class FrameworkTest : AbstractConsoleTest() { +class FrameworkInstanceTest : AbstractConsoleInstanceTest() { @Test fun testConsole1() {