From 9d052f60d5147f4ae0062397ad34cfa72efdb8df Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 6 Apr 2021 00:56:44 +0800 Subject: [PATCH] Add gradle tests Co-authored-by: Bo Zhang --- build.gradle.kts | 2 + tools/gradle-plugin/build.gradle.kts | 33 ++++++- tools/gradle-plugin/gradle.properties | 9 ++ .../mirai/console/gradle/AbstractTest.groovy | 81 ++++++++++++++++ .../console/gradle/TestPluginApply.groovy | 22 +++++ .../{ => main/kotlin}/BuildMiraiPluginTask.kt | 3 +- .../kotlin}/IGNORED_DEPENDENCIES_IN_SHADOW.kt | 8 +- .../kotlin}/MiraiConsoleExtension.kt | 8 +- .../kotlin}/MiraiConsoleGradlePlugin.kt | 8 +- .../src/{ => main/kotlin}/VersionConstants.kt | 0 .../src/{ => main/kotlin}/publishing.kt | 28 +++--- .../mirai/console/gradle/AbstractTest.groovy | 95 +++++++++++++++++++ .../console/gradle/TestPluginApply.groovy | 26 +++++ 13 files changed, 295 insertions(+), 28 deletions(-) create mode 100644 tools/gradle-plugin/gradle.properties create mode 100644 tools/gradle-plugin/src/integTest/groovy/net/mamoe/mirai/console/gradle/AbstractTest.groovy create mode 100644 tools/gradle-plugin/src/integTest/groovy/net/mamoe/mirai/console/gradle/TestPluginApply.groovy rename tools/gradle-plugin/src/{ => main/kotlin}/BuildMiraiPluginTask.kt (92%) rename tools/gradle-plugin/src/{ => main/kotlin}/IGNORED_DEPENDENCIES_IN_SHADOW.kt (92%) rename tools/gradle-plugin/src/{ => main/kotlin}/MiraiConsoleExtension.kt (95%) rename tools/gradle-plugin/src/{ => main/kotlin}/MiraiConsoleGradlePlugin.kt (95%) rename tools/gradle-plugin/src/{ => main/kotlin}/VersionConstants.kt (100%) rename tools/gradle-plugin/src/{ => main/kotlin}/publishing.kt (90%) create mode 100644 tools/gradle-plugin/test/net/mamoe/mirai/console/gradle/AbstractTest.groovy create mode 100644 tools/gradle-plugin/test/net/mamoe/mirai/console/gradle/TestPluginApply.groovy diff --git a/build.gradle.kts b/build.gradle.kts index ea22da9cf..3253b2512 100644 --- a/build.gradle.kts +++ b/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"))) diff --git a/tools/gradle-plugin/build.gradle.kts b/tools/gradle-plugin/build.gradle.kts index 0c2d42dd4..7074e480d 100644 --- a/tools/gradle-plugin/build.gradle.kts +++ b/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("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() diff --git a/tools/gradle-plugin/gradle.properties b/tools/gradle-plugin/gradle.properties new file mode 100644 index 000000000..e61f25586 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/tools/gradle-plugin/src/integTest/groovy/net/mamoe/mirai/console/gradle/AbstractTest.groovy b/tools/gradle-plugin/src/integTest/groovy/net/mamoe/mirai/console/gradle/AbstractTest.groovy new file mode 100644 index 000000000..14f18b826 --- /dev/null +++ b/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() + } +} diff --git a/tools/gradle-plugin/src/integTest/groovy/net/mamoe/mirai/console/gradle/TestPluginApply.groovy b/tools/gradle-plugin/src/integTest/groovy/net/mamoe/mirai/console/gradle/TestPluginApply.groovy new file mode 100644 index 000000000..85b39def1 --- /dev/null +++ b/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() + } +} diff --git a/tools/gradle-plugin/src/BuildMiraiPluginTask.kt b/tools/gradle-plugin/src/main/kotlin/BuildMiraiPluginTask.kt similarity index 92% rename from tools/gradle-plugin/src/BuildMiraiPluginTask.kt rename to tools/gradle-plugin/src/main/kotlin/BuildMiraiPluginTask.kt index 9241be641..0abbdb7e1 100644 --- a/tools/gradle-plugin/src/BuildMiraiPluginTask.kt +++ b/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() { /** diff --git a/tools/gradle-plugin/src/IGNORED_DEPENDENCIES_IN_SHADOW.kt b/tools/gradle-plugin/src/main/kotlin/IGNORED_DEPENDENCIES_IN_SHADOW.kt similarity index 92% rename from tools/gradle-plugin/src/IGNORED_DEPENDENCIES_IN_SHADOW.kt rename to tools/gradle-plugin/src/main/kotlin/IGNORED_DEPENDENCIES_IN_SHADOW.kt index 28d43efb5..ef6985654 100644 --- a/tools/gradle-plugin/src/IGNORED_DEPENDENCIES_IN_SHADOW.kt +++ b/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 diff --git a/tools/gradle-plugin/src/MiraiConsoleExtension.kt b/tools/gradle-plugin/src/main/kotlin/MiraiConsoleExtension.kt similarity index 95% rename from tools/gradle-plugin/src/MiraiConsoleExtension.kt rename to tools/gradle-plugin/src/main/kotlin/MiraiConsoleExtension.kt index 06c4f989a..f7518d5c5 100644 --- a/tools/gradle-plugin/src/MiraiConsoleExtension.kt +++ b/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") diff --git a/tools/gradle-plugin/src/MiraiConsoleGradlePlugin.kt b/tools/gradle-plugin/src/main/kotlin/MiraiConsoleGradlePlugin.kt similarity index 95% rename from tools/gradle-plugin/src/MiraiConsoleGradlePlugin.kt rename to tools/gradle-plugin/src/main/kotlin/MiraiConsoleGradlePlugin.kt index 2b692ed2d..d94606251 100644 --- a/tools/gradle-plugin/src/MiraiConsoleGradlePlugin.kt +++ b/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 diff --git a/tools/gradle-plugin/src/VersionConstants.kt b/tools/gradle-plugin/src/main/kotlin/VersionConstants.kt similarity index 100% rename from tools/gradle-plugin/src/VersionConstants.kt rename to tools/gradle-plugin/src/main/kotlin/VersionConstants.kt diff --git a/tools/gradle-plugin/src/publishing.kt b/tools/gradle-plugin/src/main/kotlin/publishing.kt similarity index 90% rename from tools/gradle-plugin/src/publishing.kt rename to tools/gradle-plugin/src/main/kotlin/publishing.kt index 39482d1d7..464c6ab6e 100644 --- a/tools/gradle-plugin/src/publishing.kt +++ b/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().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) } } diff --git a/tools/gradle-plugin/test/net/mamoe/mirai/console/gradle/AbstractTest.groovy b/tools/gradle-plugin/test/net/mamoe/mirai/console/gradle/AbstractTest.groovy new file mode 100644 index 000000000..8b8c7fb46 --- /dev/null +++ b/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 getProxy() { + if (System.getenv("user.name") == "Him188") new Pair("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() + } +} diff --git a/tools/gradle-plugin/test/net/mamoe/mirai/console/gradle/TestPluginApply.groovy b/tools/gradle-plugin/test/net/mamoe/mirai/console/gradle/TestPluginApply.groovy new file mode 100644 index 000000000..06f0dff65 --- /dev/null +++ b/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 + } +}