Skip to content

Commit

Permalink
Inject plugin versions into test builds (#19)
Browse files Browse the repository at this point in the history
This change injects the version of the coverage (i.e. this) plugin and
the version of the plugin under test into the test build via the
initscript so that they no longer have to be specified using
placeholders.
  • Loading branch information
ogolberg authored Jan 18, 2025
1 parent 7520c6f commit 7195a52
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 25 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ Test project files may contain Ant-style placeholders. The predefined placeholde
* `@VERSION@` - the version of the plugin under test

In the test project's `build.gradle.kts`, make sure to apply the coverage plugin, in addition to the plugin under test.
Note that both plugins require versions which can be specified using the placeholders above.

```kotlin
plugins {
id("com.toasttab.testkit.coverage") version "@TESTKIT_PLUGIN_VERSION@"
id("my.plugin.under.test") version "@VERSION@"
id("com.toasttab.testkit.coverage")
id("my.plugin.under.test")
}
```

Expand Down
4 changes: 0 additions & 4 deletions common/build.gradle.kts

This file was deleted.

8 changes: 1 addition & 7 deletions integration-tests/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import com.gradle.publish.PublishPlugin
import com.gradle.publish.PublishTask
import com.toasttab.gradle.testkit.shared.RepositoryDescriptor
import com.toasttab.gradle.testkit.shared.configureIntegrationPublishing
import com.toasttab.gradle.testkit.shared.publishOnlyIf

Expand Down Expand Up @@ -30,12 +28,8 @@ jacoco {

tasks {
test {
systemProperty("version", "$version")
systemProperty("testkit-plugin-version", "$version")
systemProperty("testkit-integration-repo", layout.buildDirectory.dir("testkit-integration-repo").get().asFile.path)

reports {
junitXml.required = true
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class FlushJacocoPluginIntegrationTest {

@Test
fun `coverage is flushed`() {
val version = System.getProperty("version")

val file = dir.resolve("testkit.exec")
val recorder = CoverageRecorder(CoverageSettings("", "", file.toString()))

Expand All @@ -44,8 +42,8 @@ class FlushJacocoPluginIntegrationTest {
"""
plugins {
java
id("com.toasttab.testkit.coverage") version("$version")
id("com.toasttab.testkit.integration.test$i") version("$version")
id("com.toasttab.testkit.coverage")
id("com.toasttab.testkit.integration.test$i")
}
""".trimIndent()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ class TestProjectExtension : ParameterResolver, BeforeAllCallback, AfterTestExec

fun createProject(projectDir: Path, gradleVersion: GradleVersionArgument, cleanup: Boolean = true, coverageRecorder: CoverageRecorder?): TestProject {
val integrationRepo = System.getProperty("testkit-integration-repo")
val projectVersion = System.getProperty("testkit-project-version")
val pluginVersion = System.getProperty("testkit-plugin-version")
val plugins = System.getProperty("testkit-plugin-ids")?.split(',')?.joinToString(separator = "\n") {
"""id("$it") version("$projectVersion")"""
} ?: ""

val initArgs = if (integrationRepo != null) {
projectDir.appendToFile(
Expand All @@ -150,6 +155,11 @@ class TestProjectExtension : ParameterResolver, BeforeAllCallback, AfterTestExec
maven(url = "file://$integrationRepo")
gradlePluginPortal()
}
plugins {
id("com.toasttab.testkit.coverage") version("$pluginVersion")
$plugins
}
}
}
""".trimIndent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@ package com.toasttab.gradle.testkit.shared
import org.gradle.api.Project
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
import org.gradle.api.attributes.Attribute
import org.gradle.api.internal.plugins.PluginDescriptor
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.maven.tasks.PublishToMavenLocal
import org.gradle.api.publish.maven.tasks.PublishToMavenRepository
import org.gradle.api.publish.tasks.GenerateModuleMetadata
import org.gradle.api.tasks.testing.Test
import org.gradle.jvm.tasks.Jar
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.withType
import org.gradle.plugin.devel.GradlePluginDevelopmentExtension
import org.gradle.plugin.devel.PluginDeclaration
import java.net.URI

sealed interface RepositoryDescriptor {
Expand Down Expand Up @@ -160,14 +164,24 @@ private fun Project.configureIntegrationPublishingForDependency(project: Project
dependsOn("${project.path}:publishTestkitIntegrationPublicationTo${repo.capitalizedName}Repository")
}

project.extensions.findByType(
GradlePluginDevelopmentExtension::class.java
)?.plugins?.forEach { plugin ->
val name = "publish" + plugin.name.simpleCapitalize() + "PluginMarkerMavenPublicationTo${repo.capitalizedName}Repository"
tasks.named<Test>("test") {
val extension = project.extensions.findByType<GradlePluginDevelopmentExtension>()

tasks.named("test") {
dependsOn("${project.path}:$name")
if (extension == null) {
logger.warn("No GradlePluginDevelopmentExtension found for project ${project.path}")
} else {
if (extension.plugins.isEmpty()) {
logger.warn("No plugins are declared in project ${project.path}")
} else {
for (plugin in extension.plugins) {
dependsOn("${project.path}:${plugin.publishTask(repo)}")
}
}

systemProperty("testkit-plugin-ids", extension.plugins.joinToString(separator = ",") { it.id })
}

systemProperty("testkit-project-version", "${project.version}")
}

if (coverage is CoverageConfiguration.Jacoco) {
Expand All @@ -187,4 +201,7 @@ private fun Project.configureIntegrationPublishingForDependency(project: Project
}
}

fun String.simpleCapitalize() = replaceFirstChar(Char::titlecaseChar)
private fun PluginDeclaration.publishTask(repo: RepositoryDescriptor.MavenRemote) =
"publish${name.simpleCapitalize()}PluginMarkerMavenPublicationTo${repo.capitalizedName}Repository"

private fun String.simpleCapitalize() = replaceFirstChar(Char::titlecaseChar)
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class TestkitPlugin @Inject constructor(
systemProperty("testkit-coverage-output", "${destfile.get()}")
systemProperty("testkit-projects", "${testProjectDir.get()}")
systemProperty("testkit-integration-repo", project.integrationDirectory().path)
systemProperty("testkit-plugin-version", BuildConfig.VERSION)
}

project.configureIntegrationPublishing()
Expand Down

0 comments on commit 7195a52

Please sign in to comment.