AbstractTest.kt 6.6 KB

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