Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Gradle TestKit Support Plugin for testing #456

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions gradle-plugin/plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.buildconfig)
alias(libs.plugins.detekt)
alias(libs.plugins.autonomousapps.testkit)

`java-gradle-plugin`
`maven-publish`
Expand Down Expand Up @@ -72,24 +73,17 @@ val writeSupportedVersionsFiles = tasks.register("writeSupportedVersionsFiles")
}
}
}

val functionalTest: SourceSet by sourceSets.creating {
// The classpath files are added as resources so that the test code can access them.
resources {
srcDir(writeSupportedVersionsFiles)
sourceSets {
functionalTest {
// The classpath files are added as resources so that the test code can access them.
resources {
srcDir(writeSupportedVersionsFiles)
}
}
}

val functionalTestTask =
tasks.register<Test>("functionalTest") {
description = "Runs the functional tests."
group = "verification"
testClassesDirs = functionalTest.output.classesDirs
classpath = functionalTest.runtimeClasspath
}

tasks.check {
dependsOn(functionalTestTask, tasks.validatePlugins)
dependsOn(tasks.validatePlugins)
}

val perfProjectTemplateResDir = project.layout.buildDirectory.dir("generated/performance-project-template/")
Expand All @@ -111,6 +105,11 @@ detekt {
baseline = rootProject.layout.projectDirectory.file("detekt/baseline.xml").asFile
}

gradleTestKitSupport {
withSupportLibrary()
withTruthLibrary()
}

dependencies {
compileOnly(gradleApi())
compileOnly(libs.android.gradle.plugin)
Expand Down Expand Up @@ -154,7 +153,6 @@ gradlePlugin {
tags = listOf("emerge", "emergetools", "android", "upload")
}
}
testSourceSets(functionalTest)
}

val emergeBaseUrl: String? by project
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.emergetools.android.gradle.base

import com.autonomousapps.kit.GradleBuilder
import com.emergetools.android.gradle.EmergePluginTest
import okhttp3.HttpUrl
import okhttp3.mockwebserver.MockWebServer
import org.gradle.internal.impldep.com.google.common.io.Files
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.util.GradleVersion
import org.gradle.util.internal.VersionNumber
import java.io.File

Expand Down Expand Up @@ -38,8 +40,6 @@ class EmergeGradleRunner private constructor(
fun create(projectDir: String): EmergeGradleRunner = EmergeGradleRunner(projectDir)
}

private val gradleRunner = GradleRunner.create()

private val baseUrl: HttpUrl
get() = server.url("/") // Starts the server

Expand All @@ -56,6 +56,10 @@ class EmergeGradleRunner private constructor(

private var environment: Map<String, String> = emptyMap()

private var gradleVersion: GradleVersion? = null

private var withDebug = false

fun withArguments(vararg arguments: String) =
apply {
this.arguments = arguments.toList()
Expand All @@ -68,14 +72,13 @@ class EmergeGradleRunner private constructor(

fun withGradleVersion(version: String) =
apply {
gradleRunner.withGradleVersion(version)
gradleVersion = GradleVersion.version(version)
}

@Suppress("unused")
fun withDebug(flag: Boolean) =
apply {
gradleRunner.withDebug(flag)
}
fun withDebug(flag: Boolean) {
withDebug = flag
}

fun withAndroidGradlePluginVersion(version: String) =
apply {
Expand Down Expand Up @@ -115,7 +118,7 @@ class EmergeGradleRunner private constructor(
this.environment = mapOf(*pairs)
}

private fun preBuild() {
private fun preBuild(): GradleRunner {
@Suppress("DEPRECATION")
tempProjectDir = Files.createTempDir()
testProjectDir(projectDir).copyRecursively(tempProjectDir!!)
Expand All @@ -137,14 +140,18 @@ class EmergeGradleRunner private constructor(
// Turns off repository discovery to test projects that don't use Git
"GIT_DIR" to tempProjectDir!!.path,
)
gradleRunner
.withProjectDir(tempProjectDir)
if (gradleVersion == null) {
gradleVersion = GradleVersion.current()
}
val runner = GradleBuilder.runner(gradleVersion!!, tempProjectDir!!)
return runner
.withDebug(withDebug)
.withArguments(arguments)
.withEnvironment(System.getenv() + customEnvironment)
// Must be called first in order to set the default plugin classpath.
.withPluginClasspath()
.withPluginClasspath(
gradleRunner.pluginClasspath +
runner.pluginClasspath +
androidGradlePluginClasspath(androidGradlePluginVersion) +
kotlinAndroidGradlePluginClasspath(kotlinAndroidGradlePluginVersion),
)
Expand Down Expand Up @@ -186,8 +193,7 @@ class EmergeGradleRunner private constructor(
}

private fun gradleRunnerBuild(buildReceiver: GradleRunner.() -> BuildResult): BuildResult {
preBuild()
val result = gradleRunner.buildReceiver()
val result = preBuild().buildReceiver()
assertions?.invoke(result, server)
postBuild()
return result
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
gradle-publish = { id = "com.gradle.plugin-publish", version = "1.3.1" }
grgit = { id = "org.ajoberstar.grgit", version = "5.3.0" }
autonomousapps-testkit = { id = "com.autonomousapps.testkit", version = "0.12" }

[libraries]
android-gradle-plugin = "com.android.tools.build:gradle:7.4.2"
Expand Down