Add gradle tests

Co-authored-by: Bo Zhang <bo@gradle.com>
This commit is contained in:
Him188 2021-04-06 00:56:44 +08:00
parent 7402856b41
commit 9d052f60d5
13 changed files with 295 additions and 28 deletions

View File

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

View File

@ -13,6 +13,7 @@ plugins {
kotlin("jvm") kotlin("jvm")
id("java-gradle-plugin") id("java-gradle-plugin")
id("com.gradle.plugin-publish") id("com.gradle.plugin-publish")
groovy
id("java") id("java")
//signing //signing
`maven-publish` `maven-publish`
@ -21,13 +22,15 @@ plugins {
id("com.github.johnrengelman.shadow") id("com.github.johnrengelman.shadow")
} }
val integTest = sourceSets.create("integTest")
dependencies { dependencies {
compileOnly(gradleApi()) compileOnly(gradleApi())
compileOnly(gradleKotlinDsl()) compileOnly(gradleKotlinDsl())
compileOnly(kotlin("gradle-plugin-api").toString()) { api(kotlin("gradle-plugin-api").toString()) {
exclude("org.jetbrains.kotlin", "kotlin-stdlib") exclude("org.jetbrains.kotlin", "kotlin-stdlib")
} }
compileOnly(kotlin("gradle-plugin").toString()) { api(kotlin("gradle-plugin").toString()) {
exclude("org.jetbrains.kotlin", "kotlin-stdlib") exclude("org.jetbrains.kotlin", "kotlin-stdlib")
} }
@ -38,6 +41,18 @@ dependencies {
api("com.github.jengelman.gradle.plugins:shadow:6.0.0") api("com.github.jengelman.gradle.plugins:shadow:6.0.0")
api(`jetbrains-annotations`) api(`jetbrains-annotations`)
api("com.jfrog.bintray.gradle:gradle-bintray-plugin:${Versions.bintray}") 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 version = Versions.console
@ -54,6 +69,7 @@ pluginBundle {
} }
gradlePlugin { gradlePlugin {
testSourceSets(integTest)
plugins { plugins {
create("miraiConsole") { create("miraiConsole") {
id = "net.mamoe.mirai-console" 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 { tasks {
val compileKotlin by getting {} val compileKotlin by getting {}
val fillBuildConstants by registering { val fillBuildConstants by registering {
group = "mirai" group = "mirai"
doLast { 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 -> .let { file ->
file.writeText( file.writeText(
file.readText() file.readText()

View File

@ -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

View File

@ -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()
}
}

View File

@ -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()
}
}

View File

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

View File

@ -1,8 +1,8 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * 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
*/ */

View File

@ -1,8 +1,8 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * 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
*/ */

View File

@ -1,8 +1,8 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * 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
*/ */

View File

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

View File

@ -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()
}
}

View File

@ -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
}
}