Explorar el Código

Add gradle tests

Co-authored-by: Bo Zhang <[email protected]>
Him188 hace 5 años
padre
commit
9d052f60d5

+ 2 - 0
build.gradle.kts

@@ -141,6 +141,8 @@ fun Project.configureEncoding() {
 }
 
 fun Project.configureSourceSets() {
+    val flatten = extra.runCatching { get("flatten.sourceset") }.getOrNull()?.toString()?.toBoolean() ?: true
+    if (!flatten) return
     sourceSets {
         findByName("main")?.apply {
             resources.setSrcDirs(listOf(projectDir.resolve("resources")))

+ 30 - 3
tools/gradle-plugin/build.gradle.kts

@@ -13,6 +13,7 @@ plugins {
     kotlin("jvm")
     id("java-gradle-plugin")
     id("com.gradle.plugin-publish")
+    groovy
     id("java")
     //signing
     `maven-publish`
@@ -21,13 +22,15 @@ plugins {
     id("com.github.johnrengelman.shadow")
 }
 
+val integTest = sourceSets.create("integTest")
+
 dependencies {
     compileOnly(gradleApi())
     compileOnly(gradleKotlinDsl())
-    compileOnly(kotlin("gradle-plugin-api").toString()) {
+    api(kotlin("gradle-plugin-api").toString()) {
         exclude("org.jetbrains.kotlin", "kotlin-stdlib")
     }
-    compileOnly(kotlin("gradle-plugin").toString()) {
+    api(kotlin("gradle-plugin").toString()) {
         exclude("org.jetbrains.kotlin", "kotlin-stdlib")
     }
 
@@ -38,6 +41,18 @@ dependencies {
     api("com.github.jengelman.gradle.plugins:shadow:6.0.0")
     api(`jetbrains-annotations`)
     api("com.jfrog.bintray.gradle:gradle-bintray-plugin:${Versions.bintray}")
+
+
+    testApi(kotlin("test-junit5"))
+    testApi("org.junit.jupiter:junit-jupiter-api:${Versions.junit}")
+    testApi("org.junit.jupiter:junit-jupiter-params:${Versions.junit}")
+
+    "integTestApi"(kotlin("test-junit5"))
+    "integTestApi"("org.junit.jupiter:junit-jupiter-api:${Versions.junit}")
+    "integTestApi"("org.junit.jupiter:junit-jupiter-params:${Versions.junit}")
+    "integTestImplementation"("org.junit.jupiter:junit-jupiter-engine:${Versions.junit}")
+//    "integTestImplementation"("org.spockframework:spock-core:1.3-groovy-2.5")
+    "integTestImplementation"(gradleTestKit())
 }
 
 version = Versions.console
@@ -54,6 +69,7 @@ pluginBundle {
 }
 
 gradlePlugin {
+    testSourceSets(integTest)
     plugins {
         create("miraiConsole") {
             id = "net.mamoe.mirai-console"
@@ -71,13 +87,24 @@ kotlin.target.compilations.all {
     }
 }
 
+val integrationTestTask = tasks.register<Test>("integTest") {
+    description = "Runs the integration tests."
+    group = "verification"
+    testClassesDirs = integTest.output.classesDirs
+    classpath = integTest.runtimeClasspath
+    mustRunAfter(tasks.test)
+}
+tasks.check {
+    dependsOn(integrationTestTask)
+}
+
 tasks {
     val compileKotlin by getting {}
 
     val fillBuildConstants by registering {
         group = "mirai"
         doLast {
-            (compileKotlin as org.jetbrains.kotlin.gradle.tasks.KotlinCompile).source.filter { it.name == "VersionConstants.kt" }.single()
+            projectDir.resolve("src").walk().filter { it.name == "VersionConstants.kt" }.single()
                 .let { file ->
                     file.writeText(
                         file.readText()

+ 9 - 0
tools/gradle-plugin/gradle.properties

@@ -0,0 +1,9 @@
+#
+# 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/master/LICENSE
+#
+flatten.sourceset=false

+ 81 - 0
tools/gradle-plugin/src/integTest/groovy/net/mamoe/mirai/console/gradle/AbstractTest.groovy

@@ -0,0 +1,81 @@
+/*
+ * 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/master/LICENSE
+ */
+
+package net.mamoe.mirai.console.gradle
+
+
+import org.gradle.testkit.runner.GradleRunner
+import org.gradle.testkit.runner.internal.PluginUnderTestMetadataReading
+import org.junit.jupiter.api.AfterEach
+import org.junit.jupiter.api.BeforeEach
+import org.junit.jupiter.api.io.TempDir
+
+abstract class AbstractTest {
+    @TempDir
+    public File tempDir
+    File buildFile
+    File settingsFile
+    File propertiesFile
+
+    def gradleRunner() {
+        println(PluginUnderTestMetadataReading.readImplementationClasspath())
+        GradleRunner.create()
+                .withProjectDir(tempDir)
+                .withPluginClasspath()
+                .forwardOutput()
+                .withEnvironment(System.getenv())
+    }
+
+    @BeforeEach
+    void setup() {
+        println('Temp path is ' + tempDir.absolutePath)
+
+        settingsFile = new File(tempDir, "settings.gradle")
+        settingsFile.delete()
+        settingsFile << """
+            pluginManagement {
+                repositories {
+                    gradlePluginPortal()
+                    mavenCentral()
+                }
+            }
+        """
+
+        buildFile = new File(tempDir, "build.gradle")
+        buildFile.delete()
+        buildFile << """
+            plugins {
+                id("org.jetbrains.kotlin.jvm") version "1.4.30"
+                id("net.mamoe.mirai-console")
+            }
+            
+            repositories {
+                mavenCentral()
+            }
+        """
+
+
+//        buildFile = new File(tempDir, "build.gradle.kts")
+//        buildFile.delete()
+//        buildFile << """
+//            plugins {
+//                kotlin("jvm") version "1.4.30"
+//                id("net.mamoe.mirai-console")
+//            }
+//            repositories {
+//                mavenCentral()
+//            }
+//        """
+    }
+
+    @AfterEach
+    void cleanup() {
+        tempDir.deleteDir()
+    }
+}

+ 22 - 0
tools/gradle-plugin/src/integTest/groovy/net/mamoe/mirai/console/gradle/TestPluginApply.groovy

@@ -0,0 +1,22 @@
+/*
+ * 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/master/LICENSE
+ */
+
+package net.mamoe.mirai.console.gradle
+
+import org.junit.jupiter.api.Test
+
+class TestPluginApply extends AbstractTest {
+
+    @Test
+    void "can apply plugin"() {
+        gradleRunner()
+                .withArguments("clean", "--stacktrace")
+                .build()
+    }
+}

+ 2 - 1
tools/gradle-plugin/src/BuildMiraiPluginTask.kt → tools/gradle-plugin/src/main/kotlin/BuildMiraiPluginTask.kt

@@ -14,9 +14,10 @@ import org.gradle.api.tasks.CacheableTask
 import org.gradle.api.tasks.OutputFile
 import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
 import java.io.File
+import javax.inject.Inject
 
 @CacheableTask
-public open class BuildMiraiPluginTask(
+public open class BuildMiraiPluginTask @Inject constructor(
     @JvmField internal val target: KotlinTarget
 ) : ShadowJar() {
     /**

+ 4 - 4
tools/gradle-plugin/src/IGNORED_DEPENDENCIES_IN_SHADOW.kt → tools/gradle-plugin/src/main/kotlin/IGNORED_DEPENDENCIES_IN_SHADOW.kt

@@ -1,10 +1,10 @@
 /*
- * Copyright 2019-2020 Mamoe Technologies and contributors.
+ * Copyright 2019-2021 Mamoe Technologies and contributors.
  *
- * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
- * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found through the following link.
+ *  此源代码的使用受 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/master/LICENSE
+ *  https://github.com/mamoe/mirai/blob/master/LICENSE
  */
 
 @file:JvmMultifileClass

+ 4 - 4
tools/gradle-plugin/src/MiraiConsoleExtension.kt → tools/gradle-plugin/src/main/kotlin/MiraiConsoleExtension.kt

@@ -1,10 +1,10 @@
 /*
- * Copyright 2019-2020 Mamoe Technologies and contributors.
+ * Copyright 2019-2021 Mamoe Technologies and contributors.
  *
- * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
- * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found through the following link.
+ *  此源代码的使用受 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/master/LICENSE
+ *  https://github.com/mamoe/mirai/blob/master/LICENSE
  */
 
 @file:Suppress("unused", "MemberVisibilityCanBePrivate")

+ 4 - 4
tools/gradle-plugin/src/MiraiConsoleGradlePlugin.kt → tools/gradle-plugin/src/main/kotlin/MiraiConsoleGradlePlugin.kt

@@ -1,10 +1,10 @@
 /*
- * Copyright 2019-2020 Mamoe Technologies and contributors.
+ * Copyright 2019-2021 Mamoe Technologies and contributors.
  *
- * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
- * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found through the following link.
+ *  此源代码的使用受 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/master/LICENSE
+ *  https://github.com/mamoe/mirai/blob/master/LICENSE
  */
 
 @file:JvmMultifileClass

+ 0 - 0
tools/gradle-plugin/src/VersionConstants.kt → tools/gradle-plugin/src/main/kotlin/VersionConstants.kt


+ 16 - 12
tools/gradle-plugin/src/publishing.kt → tools/gradle-plugin/src/main/kotlin/publishing.kt

@@ -109,14 +109,16 @@ private fun Project.registerPublishPluginTasks(target: KotlinTarget, isSingleTar
                     "${it.group}:${it.name}:${it.version}"
                 }.distinct()
 
-                val json = Gson().toJson(PluginMetadata(
-                    metadataVersion = 1,
-                    groupId = mirai.publishing.groupId ?: project.group.toString(),
-                    artifactId = mirai.publishing.artifactId ?: project.name,
-                    version = mirai.publishing.version ?: project.version.toString(),
-                    description = mirai.publishing.description ?: project.description,
-                    dependencies = dependencies
-                ))
+                val json = Gson().toJson(
+                    PluginMetadata(
+                        metadataVersion = 1,
+                        groupId = mirai.publishing.groupId ?: project.group.toString(),
+                        artifactId = mirai.publishing.artifactId ?: project.name,
+                        version = mirai.publishing.version ?: project.version.toString(),
+                        description = mirai.publishing.description ?: project.description,
+                        dependencies = dependencies
+                    )
+                )
 
                 logger.info("Generated mirai plugin metadata json: $json")
 
@@ -209,10 +211,12 @@ private fun Project.registerMavenPublications(target: KotlinTarget, isSingleTarg
 
                 artifact(sourcesJar.get())
                 artifact(tasks.filterIsInstance<BuildMiraiPluginTask>().single { it.target == target })
-                artifact(mapOf(
-                    "source" to tasks.getByName("generatePluginMetadata".wrapNameWithPlatform(target, isSingleTarget)).outputs.files.singleFile,
-                    "extension" to "mirai.metadata"
-                ))
+                artifact(
+                    mapOf(
+                        "source" to tasks.getByName("generatePluginMetadata".wrapNameWithPlatform(target, isSingleTarget)).outputs.files.singleFile,
+                        "extension" to "mirai.metadata"
+                    )
+                )
 
                 mirai.publishing.mavenPublicationConfigs.forEach { it.invoke(this) }
             }

+ 95 - 0
tools/gradle-plugin/test/net/mamoe/mirai/console/gradle/AbstractTest.groovy

@@ -0,0 +1,95 @@
+/*
+ * 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/master/LICENSE
+ */
+
+package net.mamoe.mirai.console.gradle
+
+import kotlin.Pair
+import org.gradle.testkit.runner.GradleRunner
+import org.junit.jupiter.api.AfterEach
+import org.junit.jupiter.api.BeforeEach
+import org.junit.jupiter.api.io.TempDir
+
+abstract class AbstractTest {
+    @TempDir
+    public File tempDir
+    File buildFile
+    File settingsFile
+    File propertiesFile
+
+    private static Pair<String, Integer> getProxy() {
+        if (System.getenv("user.name") == "Him188") new Pair<String, Integer>("127.0.0.1", 7890)
+        else null
+    }
+
+    def gradleRunner() {
+        GradleRunner.create()
+                .withProjectDir(tempDir)
+                .withPluginClasspath()
+                .forwardOutput()
+                .withEnvironment(System.getenv())
+    }
+
+    @BeforeEach
+    void setup() {
+        println('Temp path is ' + tempDir.absolutePath)
+
+        settingsFile = new File(tempDir, "settings.gradle")
+        settingsFile.delete()
+        settingsFile << """
+            pluginManagement {
+                repositories {
+                    gradlePluginPortal()
+                    mavenCentral()
+                }
+            }
+        """
+
+
+        propertiesFile = new File(tempDir, "gradle.properties")
+        propertiesFile.delete()
+        def proxy = getProxy()
+        if (proxy != null) propertiesFile << """
+            |systemProp.http.proxyHost=${proxy.first}
+            |systemProp.http.proxyPort=${proxy.second}
+            |systemProp.https.proxyHost=${proxy.first}
+            |systemProp.https.proxyPort=${proxy.second}
+        """.stripMargin()
+
+
+//        buildFile = new File(tempDir, "build.gradle")
+//        buildFile.delete()
+//        buildFile << """
+//            plugins {
+//                id 'org.jetbrains.kotlin.jvm' version '1.4.32'
+//                id 'net.mamoe.mirai-console'
+//            }
+//            repositories {
+//                mavenCentral()
+//            }
+//        """
+
+
+        buildFile = new File(tempDir, "build.gradle.kts")
+        buildFile.delete()
+        buildFile << """
+            plugins {
+                kotlin("jvm") version "1.4.30"
+                id("net.mamoe.mirai-console")
+            }
+            repositories {
+                mavenCentral()
+            }
+        """
+    }
+
+    @AfterEach
+    void cleanup() {
+        tempDir.deleteDir()
+    }
+}

+ 26 - 0
tools/gradle-plugin/test/net/mamoe/mirai/console/gradle/TestPluginApply.groovy

@@ -0,0 +1,26 @@
+/*
+ * 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/master/LICENSE
+ */
+
+package net.mamoe.mirai.console.gradle
+
+import org.junit.jupiter.api.Test
+
+import static org.gradle.testkit.runner.TaskOutcome.SUCCESS
+
+class TestPluginApply extends AbstractTest {
+
+    @Test
+    void "can apply plugin"() {
+        def result = gradleRunner()
+                .withArguments("clean", "--stacktrace")
+                .build()
+
+        assert result.task('clean').outcome == SUCCESS
+    }
+}