Skip to content

Commit 7195a52

Browse files
authored
Inject plugin versions into test builds (#19)
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.
1 parent 7520c6f commit 7195a52

File tree

7 files changed

+40
-25
lines changed

7 files changed

+40
-25
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ Test project files may contain Ant-style placeholders. The predefined placeholde
4242
* `@VERSION@` - the version of the plugin under test
4343

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

4746
```kotlin
4847
plugins {
49-
id("com.toasttab.testkit.coverage") version "@TESTKIT_PLUGIN_VERSION@"
50-
id("my.plugin.under.test") version "@VERSION@"
48+
id("com.toasttab.testkit.coverage")
49+
id("my.plugin.under.test")
5150
}
5251
```
5352

common/build.gradle.kts

Lines changed: 0 additions & 4 deletions
This file was deleted.

integration-tests/build.gradle.kts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import com.gradle.publish.PublishPlugin
21
import com.gradle.publish.PublishTask
3-
import com.toasttab.gradle.testkit.shared.RepositoryDescriptor
42
import com.toasttab.gradle.testkit.shared.configureIntegrationPublishing
53
import com.toasttab.gradle.testkit.shared.publishOnlyIf
64

@@ -30,12 +28,8 @@ jacoco {
3028

3129
tasks {
3230
test {
33-
systemProperty("version", "$version")
31+
systemProperty("testkit-plugin-version", "$version")
3432
systemProperty("testkit-integration-repo", layout.buildDirectory.dir("testkit-integration-repo").get().asFile.path)
35-
36-
reports {
37-
junitXml.required = true
38-
}
3933
}
4034
}
4135

integration-tests/src/test/kotlin/com/toasttab/gradle/testkit/FlushJacocoPluginIntegrationTest.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ class FlushJacocoPluginIntegrationTest {
3131

3232
@Test
3333
fun `coverage is flushed`() {
34-
val version = System.getProperty("version")
35-
3634
val file = dir.resolve("testkit.exec")
3735
val recorder = CoverageRecorder(CoverageSettings("", "", file.toString()))
3836

@@ -44,8 +42,8 @@ class FlushJacocoPluginIntegrationTest {
4442
"""
4543
plugins {
4644
java
47-
id("com.toasttab.testkit.coverage") version("$version")
48-
id("com.toasttab.testkit.integration.test$i") version("$version")
45+
id("com.toasttab.testkit.coverage")
46+
id("com.toasttab.testkit.integration.test$i")
4947
}
5048
""".trimIndent()
5149
)

junit5/src/main/kotlin/com/toasttab/gradle/testkit/TestProjectExtension.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ class TestProjectExtension : ParameterResolver, BeforeAllCallback, AfterTestExec
138138

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

142147
val initArgs = if (integrationRepo != null) {
143148
projectDir.appendToFile(
@@ -150,6 +155,11 @@ class TestProjectExtension : ParameterResolver, BeforeAllCallback, AfterTestExec
150155
maven(url = "file://$integrationRepo")
151156
gradlePluginPortal()
152157
}
158+
159+
plugins {
160+
id("com.toasttab.testkit.coverage") version("$pluginVersion")
161+
$plugins
162+
}
153163
}
154164
}
155165
""".trimIndent()

shared-build-logic/src/main/kotlin/com/toasttab/gradle/testkit/shared/IntegrationPublishing.kt

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,22 @@ package com.toasttab.gradle.testkit.shared
1818
import org.gradle.api.Project
1919
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
2020
import org.gradle.api.attributes.Attribute
21+
import org.gradle.api.internal.plugins.PluginDescriptor
2122
import org.gradle.api.publish.PublishingExtension
2223
import org.gradle.api.publish.maven.MavenPublication
2324
import org.gradle.api.publish.maven.tasks.PublishToMavenLocal
2425
import org.gradle.api.publish.maven.tasks.PublishToMavenRepository
2526
import org.gradle.api.publish.tasks.GenerateModuleMetadata
27+
import org.gradle.api.tasks.testing.Test
2628
import org.gradle.jvm.tasks.Jar
2729
import org.gradle.kotlin.dsl.create
30+
import org.gradle.kotlin.dsl.findByType
2831
import org.gradle.kotlin.dsl.get
2932
import org.gradle.kotlin.dsl.named
3033
import org.gradle.kotlin.dsl.register
3134
import org.gradle.kotlin.dsl.withType
3235
import org.gradle.plugin.devel.GradlePluginDevelopmentExtension
36+
import org.gradle.plugin.devel.PluginDeclaration
3337
import java.net.URI
3438

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

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

168-
tasks.named("test") {
169-
dependsOn("${project.path}:$name")
170+
if (extension == null) {
171+
logger.warn("No GradlePluginDevelopmentExtension found for project ${project.path}")
172+
} else {
173+
if (extension.plugins.isEmpty()) {
174+
logger.warn("No plugins are declared in project ${project.path}")
175+
} else {
176+
for (plugin in extension.plugins) {
177+
dependsOn("${project.path}:${plugin.publishTask(repo)}")
178+
}
179+
}
180+
181+
systemProperty("testkit-plugin-ids", extension.plugins.joinToString(separator = ",") { it.id })
170182
}
183+
184+
systemProperty("testkit-project-version", "${project.version}")
171185
}
172186

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

190-
fun String.simpleCapitalize() = replaceFirstChar(Char::titlecaseChar)
204+
private fun PluginDeclaration.publishTask(repo: RepositoryDescriptor.MavenRemote) =
205+
"publish${name.simpleCapitalize()}PluginMarkerMavenPublicationTo${repo.capitalizedName}Repository"
206+
207+
private fun String.simpleCapitalize() = replaceFirstChar(Char::titlecaseChar)

testkit-plugin/src/main/kotlin/com/toasttab/gradle/testkit/TestkitPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class TestkitPlugin @Inject constructor(
7070
systemProperty("testkit-coverage-output", "${destfile.get()}")
7171
systemProperty("testkit-projects", "${testProjectDir.get()}")
7272
systemProperty("testkit-integration-repo", project.integrationDirectory().path)
73+
systemProperty("testkit-plugin-version", BuildConfig.VERSION)
7374
}
7475

7576
project.configureIntegrationPublishing()

0 commit comments

Comments
 (0)