AbstractTest.kt 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.mirai.deps.test
  10. import org.gradle.api.internal.artifacts.mvnsettings.DefaultMavenFileLocations
  11. import org.gradle.testkit.runner.GradleRunner
  12. import org.gradle.testkit.runner.internal.DefaultGradleRunner
  13. import org.junit.jupiter.api.BeforeEach
  14. import org.junit.jupiter.api.extension.AfterEachCallback
  15. import org.junit.jupiter.api.extension.RegisterExtension
  16. import org.junit.jupiter.api.io.TempDir
  17. import java.io.File
  18. // Copied from mirai-console-gradle
  19. abstract class AbstractTest {
  20. companion object {
  21. const val miraiLocalVersion = "2.99.0-deps-test" // do Search Everywhere before changing this
  22. const val REASON_LOCAL_ARTIFACT_NOT_AVAILABLE = "local artifacts not available"
  23. private const val MIRAI_DEPS_TEST_MUST_RUN = "mirai.deps.test.must.run" // used by GitHub Actions scripts
  24. val mavenLocalDir: File by lazy {
  25. org.gradle.api.internal.artifacts.mvnsettings.DefaultLocalMavenRepositoryLocator(
  26. org.gradle.api.internal.artifacts.mvnsettings.DefaultMavenSettingsProvider(DefaultMavenFileLocations())
  27. ).localMavenRepository
  28. }
  29. @JvmStatic
  30. fun isMiraiLocalAvailable(): Boolean {
  31. return if (mavenLocalDir.resolve("net/mamoe/mirai-core/$miraiLocalVersion").exists()) {
  32. println(
  33. """
  34. [mirai-deps-test] Found local artifacts `$miraiLocalVersion`!
  35. Please note that you may need to manually update local artifacts if you have:
  36. - added/removed a dependency for mirai-core series modules
  37. - changed version of any of the dependencies for mirai-core series modules
  38. You can update by running `./gradlew publishMiraiLocalArtifacts`.
  39. """.trimIndent()
  40. )
  41. true
  42. } else {
  43. val message = """
  44. [mirai-deps-test] ERROR: Test is not run, because there are no local artifacts available for dependencies testing.
  45. Please build and publish local artifacts with version `$miraiLocalVersion` before running this test(:mirai-deps-test:test).
  46. This could have be automated but it will take a huge amount of time for your routine testing.
  47. You can run this test manually if you have:
  48. - added/removed a dependency for mirai-core series modules
  49. - changed version of any of the dependencies for mirai-core series modules
  50. Note that you can ignore this test if you did not change project (dependency) structure.
  51. And you don't need to worry if you does not run this test — this test is always executed on the CI when you make a PR.
  52. You can run `./gradlew publishMiraiLocalArtifacts` to publish local artifacts.
  53. Then you can run this test again. (By your original way or ./gradlew :mirai-deps-test:test)
  54. """.trimIndent()
  55. System.err.println(
  56. message
  57. )
  58. if (System.getProperty(MIRAI_DEPS_TEST_MUST_RUN, "false").toBoolean()) {
  59. throw AssertionError("System property `mirai.deps.test.must.run` is `true`, which requires the deps test to be run. \n\n$message")
  60. } else {
  61. false
  62. }
  63. }
  64. }
  65. }
  66. @JvmField
  67. @TempDir
  68. var tempDirField: File? = null
  69. val tempDir: File get() = tempDirField!!
  70. val kotlinVersion = BuildConfig.kotlinVersion
  71. lateinit var mainSrcDir: File
  72. lateinit var commonMainSrcDir: File
  73. lateinit var nativeMainSrcDir: File
  74. lateinit var testDir: File
  75. lateinit var buildFile: File
  76. lateinit var settingsFile: File
  77. lateinit var propertiesFile: File
  78. private inline fun <reified T> Any?.cast(): T = this as T
  79. @OptIn(ExperimentalStdlibApi::class)
  80. fun runGradle(vararg arguments: String) {
  81. System.gc()
  82. GradleRunner.create()
  83. .withProjectDir(tempDir)
  84. .withPluginClasspath()
  85. .withGradleVersion("7.2")
  86. .forwardOutput()
  87. .withEnvironment(System.getenv())
  88. .cast<DefaultGradleRunner>().withJvmArguments(buildList {
  89. add("-Xmx512m") // Kotlin MPP may need memory to build
  90. add("-Dfile.encoding=UTF-8")
  91. })
  92. .withArguments(buildList {
  93. addAll(arguments)
  94. add("-P")
  95. add("kotlin.compiler.execution.strategy=in-process")
  96. add("-D")
  97. add("org.gradle.jvmargs=-Xmx512m")
  98. add("-D")
  99. add("file.encoding=UTF-8")
  100. // add("--stacktrace")
  101. add("--info")
  102. })
  103. .build()
  104. }
  105. @BeforeEach
  106. fun setup() {
  107. println("Temp path is " + tempDir.absolutePath)
  108. settingsFile = File(tempDir, "settings.gradle")
  109. settingsFile.delete()
  110. settingsFile.writeText(
  111. """
  112. pluginManagement {
  113. repositories {
  114. gradlePluginPortal()
  115. mavenCentral()
  116. mavenLocal()
  117. }
  118. }
  119. """
  120. )
  121. File(tempDir, "gradle.properties").apply {
  122. delete()
  123. writeText(
  124. """
  125. org.gradle.daemon=false
  126. org.gradle.jvmargs=-Xmx4096m -Dfile.encoding=UTF-8
  127. """.trimIndent()
  128. )
  129. }
  130. mainSrcDir = tempDir.resolve("src/main/kotlin").apply { mkdirs() }
  131. commonMainSrcDir = tempDir.resolve("src/commonMain/kotlin").apply { mkdirs() }
  132. nativeMainSrcDir = tempDir.resolve("src/nativeMain/kotlin").apply { mkdirs() }
  133. testDir = tempDir.resolve("src/test/kotlin").apply { mkdirs() }
  134. buildFile = tempDir.resolve("build.gradle.kts")
  135. buildFile.writeText(
  136. """
  137. import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
  138. plugins {
  139. kotlin("jvm") version "1.7.0"
  140. }
  141. group = "org.example"
  142. version = "1.0-SNAPSHOT"
  143. repositories {
  144. mavenCentral()
  145. mavenLocal()
  146. }
  147. dependencies {
  148. testImplementation(kotlin("test"))
  149. implementation(kotlin("reflect"))
  150. testImplementation(kotlin("test-junit5"))
  151. testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.2")
  152. testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.2")
  153. }
  154. tasks.test {
  155. useJUnitPlatform()
  156. }
  157. tasks.withType<KotlinCompile> {
  158. kotlinOptions.jvmTarget = "1.8"
  159. }
  160. """.trimIndent() + "\n\n"
  161. )
  162. }
  163. @JvmField
  164. @RegisterExtension
  165. internal val after: AfterEachCallback = AfterEachCallback { context ->
  166. if (context.executionException.isPresent) {
  167. val inst = context.requiredTestInstance as AbstractTest
  168. println("====================== build.gradle ===========================")
  169. println(inst.tempDir.resolveFirstExisting("build.gradle", "build.gradle.kts").readTextIfFound())
  170. println("==================== settings.gradle ==========================")
  171. println(inst.tempDir.resolveFirstExisting("settings.gradle", "settings.gradle.kts").readTextIfFound())
  172. }
  173. }
  174. private fun File.resolveFirstExisting(vararg files: String): File? {
  175. return files.asSequence().map { resolve(it) }.firstOrNull { it.exists() }
  176. }
  177. private fun File?.readTextIfFound(): String =
  178. when {
  179. this == null -> "(not found)"
  180. exists() -> readText()
  181. else -> "($name not found)"
  182. }
  183. }