MiraiConsoleIntegrationTestBootstrap.kt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 2019-2022 Mamoe Technologies and contributors.
  3. *
  4. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  5. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
  6. *
  7. * https://github.com/mamoe/mirai/blob/dev/LICENSE
  8. */
  9. package net.mamoe.console.integrationtest
  10. import net.mamoe.console.integrationtest.testpoints.MCITBSelfAssertions
  11. import org.junit.jupiter.api.Test
  12. import org.objectweb.asm.ClassReader
  13. import java.io.File
  14. import java.lang.management.ManagementFactory
  15. import java.nio.file.Files
  16. import java.nio.file.Paths
  17. import java.util.*
  18. import kotlin.io.path.inputStream
  19. import kotlin.io.path.isDirectory
  20. import kotlin.io.path.name
  21. import kotlin.reflect.KClass
  22. import kotlin.test.assertTrue
  23. class MiraiConsoleIntegrationTestBootstrap {
  24. @Test
  25. fun bootstrap() {
  26. /*
  27. implementation note:
  28. 不使用 @TempDir 是为了保存最后一次失败快照, 便于 debug
  29. */
  30. val workingDir = File("build/IntegrationTest") // mirai-console/backend/integration-test/build/IntegrationTest
  31. // 注意: 如果更改 workingDir, 还要更改 mirai-console/backend/integration-test/build.gradle.kts:60 (clean task 的依赖)
  32. val launcher = MiraiConsoleIntegrationTestLauncher()
  33. launcher.workingDir = workingDir
  34. launcher.plugins = readStringListFromEnv("IT_PLUGINS")
  35. launcher.points = resolveTestPoints().also { points ->
  36. // Avoid error in resolving points
  37. assertTrue { points.contains("net.mamoe.console.integrationtest.testpoints.MCITBSelfAssertions") }
  38. assertTrue { points.contains("net.mamoe.console.integrationtest.testpoints.DoNothingPoint") }
  39. assertTrue { points.contains("net.mamoe.console.integrationtest.testpoints.terminal.TestTerminalLogging") }
  40. }.asSequence().map { v ->
  41. when (v) {
  42. is Class<*> -> v.name
  43. is KClass<*> -> v.java.name
  44. is String -> v
  45. else -> v.javaClass.name
  46. }
  47. }.map { it.replace('/', '.') }.toMutableList()
  48. launcher.vmoptions = mutableListOf(
  49. *ManagementFactory.getRuntimeMXBean().inputArguments.filterNot {
  50. it.startsWith("-Djava.security.manager=")
  51. }.filterNot {
  52. it.startsWith("-Xmx")
  53. }.toTypedArray(),
  54. *System.getenv("IT_ARGS")!!.splitToSequence(",").map {
  55. Base64.getDecoder().decode(it).decodeToString()
  56. }.filter { it.isNotEmpty() }.toList().toTypedArray()
  57. )
  58. launcher.launch()
  59. }
  60. private fun resolveTestPoints(): Collection<Any> {
  61. val ptloc = MCITBSelfAssertions.javaClass.getResource(MCITBSelfAssertions.javaClass.simpleName + ".class")
  62. ptloc ?: error("Failed to resolve test points")
  63. val path = Paths.get(ptloc.toURI()).parent
  64. return Files.walk(path)
  65. .filter { !it.isDirectory() }
  66. .filter { it.name.endsWith(".class") }
  67. .map { pt ->
  68. pt.inputStream().use {
  69. ClassReader(it).className
  70. }
  71. }
  72. .map { it.replace('/', '.') }
  73. .filter { AbstractTestPoint::class.java.isAssignableFrom(Class.forName(it)) }
  74. .use { it.toList() }
  75. }
  76. }