Skip to content

Commit 9f4b41d

Browse files
committed
chore: test jvm constraints plugin now configures jmh plugin
1 parent 493d1e3 commit 9f4b41d

5 files changed

Lines changed: 40 additions & 12 deletions

File tree

buildSrc/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ dependencies {
109109
implementation("com.fasterxml.jackson.core:jackson-core")
110110

111111
compileOnly(libs.develocity)
112+
113+
testImplementation("me.champeau.jmh:jmh-gradle-plugin:0.7.3")
112114
}
113115

114116
tasks.compileKotlin {

buildSrc/src/main/kotlin/datadog/gradle/plugin/testJvmConstraints/TestJvmConstraintsPlugin.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.gradle.api.JavaVersion
66
import org.gradle.api.Plugin
77
import org.gradle.api.Project
88
import org.gradle.api.plugins.JavaPlugin
9+
import org.gradle.api.provider.Property
910
import org.gradle.api.provider.Provider
1011
import org.gradle.api.tasks.testing.Test
1112
import org.gradle.kotlin.dsl.*
@@ -42,6 +43,16 @@ class TestJvmConstraintsPlugin : Plugin<Project> {
4243
configureTestJvm(testJvmSpec, taskExtension)
4344
}
4445

46+
// JMH plugin is not applied on every project
47+
project.pluginManager.withPlugin("me.champeau.jmh") {
48+
val jmh = project.extensions.getByName("jmh")
49+
50+
// Avoid linking against JMH types, which are unavailable to the parent buildSrc classloader.
51+
@Suppress("UNCHECKED_CAST")
52+
val jvm = jmh.javaClass.getMethod("getJvm").invoke(jmh) as Property<String>
53+
jvm.set(testJvmSpec.javaTestLauncher.map { it.executablePath.asFile.absolutePath })
54+
}
55+
4556
// Jacoco plugin is not applied on every project
4657
project.pluginManager.withPlugin("org.gradle.jacoco") {
4758
project.tasks.withType<Test>().configureEach {

buildSrc/src/test/kotlin/datadog/gradle/plugin/testJvmConstraints/TestJvmConstraintsPluginTest.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package datadog.gradle.plugin.testJvmConstraints
22

33
import datadog.gradle.plugin.testJvmConstraints.TestJvmConstraintsExtension.Companion.TEST_JVM_CONSTRAINTS
4+
import me.champeau.jmh.JmhParameters
45
import org.assertj.core.api.Assertions.assertThat
6+
import org.gradle.api.JavaVersion
57
import org.gradle.api.tasks.testing.Test as GradleTest
68
import org.gradle.testfixtures.ProjectBuilder
79
import org.gradle.testing.jacoco.plugins.JacocoTaskExtension
@@ -21,6 +23,31 @@ class TestJvmConstraintsPluginTest {
2123
assertThat(testTask.extensions.findByName(TEST_JVM_CONSTRAINTS)).isInstanceOf(TestJvmConstraintsExtension::class.java)
2224
}
2325

26+
@Test
27+
fun `plugin configures jmh to use the test jvm`() {
28+
val propertyName = "org.gradle.project.${TestJvmSpec.TEST_JVM}"
29+
val previousValue = System.setProperty(propertyName, JavaVersion.current().majorVersion)
30+
31+
try {
32+
val project = ProjectBuilder.builder().build()
33+
34+
project.pluginManager.apply("dd-trace-java.test-jvm-constraints")
35+
assertThat(project.extensions.findByName("jmh")).isNull()
36+
37+
project.pluginManager.apply("me.champeau.jmh")
38+
39+
val jmh = project.extensions.getByType(JmhParameters::class.java)
40+
val expectedExecutable = TestJvmSpec(project).javaTestLauncher.get().executablePath.asFile.absolutePath
41+
assertThat(jmh.jvm.get()).isEqualTo(expectedExecutable)
42+
} finally {
43+
if (previousValue == null) {
44+
System.clearProperty(propertyName)
45+
} else {
46+
System.setProperty(propertyName, previousValue)
47+
}
48+
}
49+
}
50+
2451
@Test
2552
fun `jacoco is disabled for additional test jvm when coverage is not checked`() {
2653
val testTask = testTaskWithJacoco()

dd-trace-core/build.gradle

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import datadog.gradle.plugin.testJvmConstraints.TestJvmSpec
2-
31
plugins {
42
id 'me.champeau.jmh'
53
id 'dd-trace-java.version-file'
@@ -135,8 +133,4 @@ jmh {
135133
if (project.hasProperty('jmh.profilers')) {
136134
profilers = project.property('jmh.profilers').tokenize(',')
137135
}
138-
if (project.hasProperty('testJvm')) {
139-
def testJvmSpec = new TestJvmSpec(project)
140-
jvm = testJvmSpec.javaTestLauncher.map { it.executablePath.asFile.absolutePath }
141-
}
142136
}

internal-api/build.gradle.kts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import datadog.gradle.plugin.testJvmConstraints.TestJvmSpec
21
import de.thetaphi.forbiddenapis.gradle.CheckForbiddenApis
32
import groovy.lang.Closure
43

@@ -291,9 +290,4 @@ jmh {
291290
if (project.hasProperty("jmh.includes")) {
292291
includes.add(project.property("jmh.includes") as String)
293292
}
294-
295-
if (project.hasProperty("testJvm")) {
296-
val testJvmSpec = TestJvmSpec(project)
297-
jvm.set(testJvmSpec.javaTestLauncher.map { it.executablePath.asFile.absolutePath })
298-
}
299293
}

0 commit comments

Comments
 (0)