MiraiConsoleIntegrationTestBootstrap.kt 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 java.util.stream.Collectors
  19. import kotlin.io.path.inputStream
  20. import kotlin.io.path.isDirectory
  21. import kotlin.io.path.name
  22. import kotlin.reflect.KClass
  23. import kotlin.test.assertTrue
  24. class MiraiConsoleIntegrationTestBootstrap {
  25. @Test
  26. fun bootstrap() {
  27. /*
  28. implementation note:
  29. 不使用 @TempDir 是为了保存最后一次失败快照, 便于 debug
  30. */
  31. val workingDir = File("build/IntegrationTest") // mirai-console/backend/integration-test/build/IntegrationTest
  32. // 注意: 如果更改 workingDir, 还要更改 mirai-console/backend/integration-test/build.gradle.kts:60 (clean task 的依赖)
  33. val launcher = MiraiConsoleIntegrationTestLauncher()
  34. launcher.workingDir = workingDir
  35. launcher.plugins = readStringListFromEnv("IT_PLUGINS")
  36. launcher.points = resolveTestPoints().also { points ->
  37. // Avoid error in resolving points
  38. assertTrue { points.contains("net.mamoe.console.integrationtest.testpoints.MCITBSelfAssertions") }
  39. assertTrue { points.contains("net.mamoe.console.integrationtest.testpoints.DoNothingPoint") }
  40. assertTrue { points.contains("net.mamoe.console.integrationtest.testpoints.terminal.TestTerminalLogging") }
  41. }.asSequence().map { v ->
  42. when (v) {
  43. is Class<*> -> v.name
  44. is KClass<*> -> v.java.name
  45. is String -> v
  46. else -> v.javaClass.name
  47. }
  48. }.map { it.replace('/', '.') }.toMutableList()
  49. launcher.vmoptions = mutableListOf(
  50. *ManagementFactory.getRuntimeMXBean().inputArguments.filterNot {
  51. it.startsWith("-Djava.security.manager=")
  52. }.filterNot {
  53. it.startsWith("-Xmx")
  54. }.toTypedArray(),
  55. *System.getenv("IT_ARGS")!!.splitToSequence(",").map {
  56. Base64.getDecoder().decode(it).decodeToString()
  57. }.filter { it.isNotEmpty() }.toList().toTypedArray()
  58. )
  59. launcher.launch()
  60. }
  61. private fun resolveTestPoints(): Collection<Any> {
  62. val ptloc = MCITBSelfAssertions.javaClass.getResource(MCITBSelfAssertions.javaClass.simpleName + ".class")
  63. ptloc ?: error("Failed to resolve test points")
  64. val path = Paths.get(ptloc.toURI()).parent
  65. return Files.walk(path)
  66. .filter { !it.isDirectory() }
  67. .filter { it.name.endsWith(".class") }
  68. .map { pt ->
  69. pt.inputStream().use {
  70. ClassReader(it).className
  71. }
  72. }
  73. .map { it.replace('/', '.') }
  74. .filter { AbstractTestPoint::class.java.isAssignableFrom(Class.forName(it)) }
  75. .use { it.collect(Collectors.toList()) }
  76. }
  77. }