-
Notifications
You must be signed in to change notification settings - Fork 315
Introduces testJvmConstraint Gradle extension to replace extra properties
#9892
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
Open
bric3
wants to merge
17
commits into
master
Choose a base branch
from
bdu/from-extra-props-to-extensions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
914c780
chore: Starts extracting testJvm configuration to another script plugin
bric3 770111c
chore: Moved test jvm constraint logic to convention plugin
bric3 c6cda9f
chore: Moved additional test suite constraints to new test task exten…
bric3 dfefaea
chore: Configure test jvm constraint on test task, added project wide…
bric3 9f643d4
chore: configure source set via extension method
bric3 594cbb1
chore: Use source set configuration
bric3 dcce4f2
chore: Renames jvmConstraints to testJvmConstraints
bric3 3ef9429
fix: profiling-controller-oracle tests could never run, introduce an …
bric3 39c00ee
refactor: Simplify test jvm enablement code
bric3 975044a
chore: Handle test sources compilation override
bric3 deaebf1
chore: testJvm is optional
bric3 4a2aa18
refactor: small rename job
bric3 1e2e37b
refactor: Convert remaining usage of min/max jdk properties to our ex…
bric3 f5a892a
refactor: Renames min|maxJavaVersionForTests to min|maxJavaVersion
bric3 79cf261
chore: Make spotless happy
bric3 04bd398
fix: Apply test sources compiler settings via common compilerConfigur…
bric3 5a502dc
fix: Make vertx compile test with Java 11
bric3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
buildSrc/src/main/kotlin/datadog.test-jvm-contraints.gradle.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import datadog.gradle.plugin.testJvmConstraints.ProvideJvmArgsOnJvmLauncherVersion | ||
| import datadog.gradle.plugin.testJvmConstraints.TestJvmConstraintsExtension | ||
| import datadog.gradle.plugin.testJvmConstraints.TestJvmSpec | ||
| import datadog.gradle.plugin.testJvmConstraints.isJavaVersionAllowed | ||
| import datadog.gradle.plugin.testJvmConstraints.isTestJvmAllowed | ||
|
|
||
| plugins { | ||
| java | ||
| } | ||
|
|
||
| val projectExtension = extensions.create<TestJvmConstraintsExtension>(TestJvmConstraintsExtension.NAME) | ||
|
|
||
| val testJvmSpec = TestJvmSpec(project) | ||
|
|
||
| tasks.withType<Test>().configureEach { | ||
| if (extensions.findByName(TestJvmConstraintsExtension.NAME) != null) { | ||
| return@configureEach | ||
| } | ||
|
|
||
| inputs.property("testJvm", testJvmSpec.testJvmProperty).optional(true) | ||
|
|
||
| val taskExtension = project.objects.newInstance<TestJvmConstraintsExtension>().also { | ||
| configureConventions(it, projectExtension) | ||
| } | ||
|
|
||
| inputs.property("${TestJvmConstraintsExtension.NAME}.allowReflectiveAccessToJdk", taskExtension.allowReflectiveAccessToJdk).optional(true) | ||
| inputs.property("${TestJvmConstraintsExtension.NAME}.excludeJdk", taskExtension.excludeJdk) | ||
| inputs.property("${TestJvmConstraintsExtension.NAME}.includeJdk", taskExtension.includeJdk) | ||
| inputs.property("${TestJvmConstraintsExtension.NAME}.forceJdk", taskExtension.forceJdk) | ||
| inputs.property("${TestJvmConstraintsExtension.NAME}.minJavaVersion", taskExtension.minJavaVersion).optional(true) | ||
| inputs.property("${TestJvmConstraintsExtension.NAME}.maxJavaVersion", taskExtension.maxJavaVersion).optional(true) | ||
|
|
||
| extensions.add(TestJvmConstraintsExtension.NAME, taskExtension) | ||
|
|
||
| configureTestJvm(taskExtension) | ||
| } | ||
|
|
||
| /** | ||
| * Provide arguments if condition is met. | ||
| */ | ||
| fun Test.conditionalJvmArgs( | ||
| applyFromVersion: JavaVersion, | ||
| jvmArgsToApply: List<String>, | ||
| additionalCondition: Provider<Boolean> = project.providers.provider { true } | ||
| ) { | ||
| jvmArgumentProviders.add( | ||
| ProvideJvmArgsOnJvmLauncherVersion( | ||
| this, | ||
| applyFromVersion, | ||
| jvmArgsToApply, | ||
| additionalCondition | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Configure the jvm launcher of the test task and ensure the test task | ||
| * can be run with the test task launcher. | ||
| */ | ||
| private fun Test.configureTestJvm(extension: TestJvmConstraintsExtension) { | ||
| if (testJvmSpec.javaTestLauncher.isPresent) { | ||
| javaLauncher = testJvmSpec.javaTestLauncher | ||
| onlyIf("Allowed or forced JDK") { | ||
| extension.isTestJvmAllowed(testJvmSpec) | ||
| } | ||
| } else { | ||
| onlyIf("Is current Daemon JVM allowed") { | ||
| extension.isJavaVersionAllowed(JavaVersion.current()) | ||
| } | ||
| } | ||
|
|
||
| // temporary workaround when using Java16+: some tests require reflective access to java.lang/java.util | ||
| conditionalJvmArgs( | ||
| JavaVersion.VERSION_16, | ||
| listOf( | ||
| "--add-opens=java.base/java.lang=ALL-UNNAMED", | ||
| "--add-opens=java.base/java.util=ALL-UNNAMED" | ||
| ), | ||
| extension.allowReflectiveAccessToJdk | ||
| ) | ||
| } | ||
|
|
||
| // Jacoco plugin is not applied on every project | ||
| pluginManager.withPlugin("org.gradle.jacoco") { | ||
| tasks.withType<Test>().configureEach { | ||
| // Disable jacoco for additional 'testJvm' tests to speed things up a bit | ||
| if (testJvmSpec.javaTestLauncher.isPresent) { | ||
| extensions.configure<JacocoTaskExtension> { | ||
| isEnabled = false | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Configures the convention, this tells Gradle where to look for values. | ||
| * | ||
| * Currently, the extension is still configured to look at project's _extra_ properties. | ||
| */ | ||
| private fun Test.configureConventions( | ||
| taskExtension: TestJvmConstraintsExtension, | ||
| projectExtension: TestJvmConstraintsExtension | ||
| ) { | ||
| taskExtension.minJavaVersion.convention(projectExtension.minJavaVersion | ||
| .orElse(providers.provider { project.findProperty("${name}MinJavaVersionForTests") as? JavaVersion }) | ||
| .orElse(providers.provider { project.findProperty("minJavaVersion") as? JavaVersion }) | ||
| ) | ||
| taskExtension.maxJavaVersion.convention(projectExtension.maxJavaVersion | ||
| .orElse(providers.provider { project.findProperty("${name}MaxJavaVersionForTests") as? JavaVersion }) | ||
| .orElse(providers.provider { project.findProperty("maxJavaVersion") as? JavaVersion }) | ||
| ) | ||
| taskExtension.forceJdk.convention(projectExtension.forceJdk | ||
| .orElse(providers.provider { | ||
| @Suppress("UNCHECKED_CAST") | ||
| project.findProperty("forceJdk") as? List<String> ?: emptyList() | ||
| }) | ||
| ) | ||
| taskExtension.excludeJdk.convention(projectExtension.excludeJdk | ||
| .orElse(providers.provider { | ||
| @Suppress("UNCHECKED_CAST") | ||
| project.findProperty("excludeJdk") as? List<String> ?: emptyList() | ||
| }) | ||
| ) | ||
| taskExtension.allowReflectiveAccessToJdk.convention(projectExtension.allowReflectiveAccessToJdk | ||
| .orElse(providers.provider { project.findProperty("allowReflectiveAccessToJdk") as? Boolean }) | ||
| ) | ||
| } | ||
38 changes: 38 additions & 0 deletions
38
...ain/kotlin/datadog/gradle/plugin/testJvmConstraints/ProvideJvmArgsOnJvmLauncherVersion.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package datadog.gradle.plugin.testJvmConstraints | ||
|
|
||
| import org.gradle.api.JavaVersion | ||
| import org.gradle.api.provider.Provider | ||
| import org.gradle.api.tasks.Input | ||
| import org.gradle.api.tasks.Internal | ||
| import org.gradle.api.tasks.Optional | ||
| import org.gradle.api.tasks.testing.Test | ||
| import org.gradle.process.CommandLineArgumentProvider | ||
|
|
||
| class ProvideJvmArgsOnJvmLauncherVersion( | ||
| @get:Internal | ||
| val test: Test, | ||
|
|
||
| @get:Input | ||
| val applyFromVersion: JavaVersion, | ||
|
|
||
| @get:Input | ||
| val jvmArgsToApply: List<String>, | ||
|
|
||
| @get:Input | ||
| @get:Optional | ||
| val additionalCondition: Provider<Boolean> | ||
| ) : CommandLineArgumentProvider { | ||
|
|
||
| override fun asArguments(): Iterable<String> { | ||
| val launcherVersion = test.javaLauncher | ||
| .map { JavaVersion.toVersion(it.metadata.languageVersion.asInt()) } | ||
| .orElse(JavaVersion.current()) | ||
| .get() | ||
|
|
||
| return if (launcherVersion.isCompatibleWith(applyFromVersion) && additionalCondition.getOrElse(true)) { | ||
| jvmArgsToApply | ||
| } else { | ||
| emptyList() | ||
| } | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
...c/src/main/kotlin/datadog/gradle/plugin/testJvmConstraints/TestJvmConstraintsExtension.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||
| package datadog.gradle.plugin.testJvmConstraints | ||||||
|
|
||||||
| import org.gradle.api.JavaVersion | ||||||
| import org.gradle.api.provider.ListProperty | ||||||
| import org.gradle.api.provider.Property | ||||||
|
|
||||||
| interface TestJvmConstraintsExtension { | ||||||
| /** | ||||||
| * Sets an explicit minimum bound to allowed JDK version | ||||||
| */ | ||||||
| val minJavaVersion: Property<JavaVersion> | ||||||
|
|
||||||
| /** | ||||||
| * Sets an explicit maximum bound to allowed JDK version | ||||||
| */ | ||||||
| val maxJavaVersion: Property<JavaVersion> | ||||||
|
|
||||||
| /** | ||||||
| * List of allowed JDK names (passed through the `testJvm` property). | ||||||
| */ | ||||||
| val forceJdk: ListProperty<String> | ||||||
|
|
||||||
| /** | ||||||
| * List of included JDK names (passed through the `testJvm` property). | ||||||
| */ | ||||||
| val includeJdk: ListProperty<String> | ||||||
|
|
||||||
| /** | ||||||
| * List of excluded JDK names (passed through the `testJvm` property). | ||||||
| */ | ||||||
| val excludeJdk: ListProperty<String> | ||||||
|
|
||||||
| /** | ||||||
| * Indicate if test jvm allows reflective access to JDK modules, in particular this toggle | ||||||
| * openning `java.base/java.lang` and `java.base/java.util`. | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Does this make sense? |
||||||
| */ | ||||||
| val allowReflectiveAccessToJdk: Property<Boolean> | ||||||
|
|
||||||
| companion object { | ||||||
| const val NAME = "testJvmConstraint" | ||||||
| } | ||||||
| } | ||||||
48 changes: 48 additions & 0 deletions
48
buildSrc/src/main/kotlin/datadog/gradle/plugin/testJvmConstraints/TestJvmConstraintsUtils.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package datadog.gradle.plugin.testJvmConstraints | ||
|
|
||
| import org.gradle.api.JavaVersion | ||
| import org.gradle.api.logging.Logging | ||
|
|
||
| private val logger = Logging.getLogger("TestJvmConstraintsUtils") | ||
|
|
||
| internal fun TestJvmConstraintsExtension.isJavaVersionAllowed(version: JavaVersion): Boolean { | ||
| return isWithinAllowedRange(version) | ||
| } | ||
|
|
||
| internal fun TestJvmConstraintsExtension.isTestJvmAllowed(testJvmSpec: TestJvmSpec): Boolean { | ||
| val testJvmName = testJvmSpec.normalizedTestJvm.get() | ||
|
|
||
| val included = includeJdk.get() | ||
| if (included.isNotEmpty() && included.none { it.equals(testJvmName, ignoreCase = true) }) { | ||
| return false | ||
| } | ||
|
|
||
| val excluded = excludeJdk.get() | ||
| if (excluded.isNotEmpty() && excluded.any { it.equals(testJvmName, ignoreCase = true) }) { | ||
| return false | ||
| } | ||
|
|
||
| val launcherVersion = JavaVersion.toVersion(testJvmSpec.javaTestLauncher.get().metadata.languageVersion.asInt()) | ||
| if (!isWithinAllowedRange(launcherVersion) && forceJdk.get().none { it.equals(testJvmName, ignoreCase = true) }) { | ||
| return false | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| private fun TestJvmConstraintsExtension.isWithinAllowedRange(currentJvmVersion: JavaVersion): Boolean { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: rename |
||
| val definedMin = minJavaVersion.isPresent | ||
| val definedMax = maxJavaVersion.isPresent | ||
|
|
||
| if (definedMin && (minJavaVersion.get()) > currentJvmVersion) { | ||
| logger.info("isWithinAllowedRange returns false b/o minProp=${minJavaVersion.get()} is defined and greater than version=$currentJvmVersion") | ||
| return false | ||
| } | ||
|
|
||
| if (definedMax && (maxJavaVersion.get()) < currentJvmVersion) { | ||
| logger.info("isWithinAllowedRange returns false b/o maxProp=${maxJavaVersion.get()} is defined and lower than version=$currentJvmVersion") | ||
| return false | ||
| } | ||
|
|
||
| return true | ||
| } | ||
88 changes: 88 additions & 0 deletions
88
buildSrc/src/main/kotlin/datadog/gradle/plugin/testJvmConstraints/TestJvmSpec.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package datadog.gradle.plugin.testJvmConstraints | ||
|
|
||
| import org.gradle.kotlin.dsl.support.serviceOf | ||
| import org.gradle.api.GradleException | ||
| import org.gradle.api.Project | ||
| import org.gradle.jvm.toolchain.JavaLauncher | ||
| import org.gradle.jvm.toolchain.JavaToolchainService | ||
| import java.nio.file.Files | ||
| import java.nio.file.Path | ||
| import java.nio.file.Paths | ||
|
|
||
|
|
||
| class TestJvmSpec(val project: Project) { | ||
| companion object { | ||
| const val TEST_JVM = "testJvm" | ||
| } | ||
|
|
||
| private val currentJavaHomePath = project.providers.systemProperty("java.home").map { it.normalizeToJDKJavaHome() } | ||
|
|
||
| val testJvmProperty = project.providers.gradleProperty(TEST_JVM) | ||
|
|
||
| val normalizedTestJvm = testJvmProperty.map { testJvm -> | ||
| if (testJvm.isBlank()) { | ||
| throw GradleException("testJvm property is blank") | ||
| } | ||
|
|
||
| // "stable" is calculated as the largest X found in JAVA_X_HOME | ||
| if (testJvm == "stable") { | ||
| val javaVersions = project.providers.environmentVariablesPrefixedBy("JAVA_").map { javaHomes -> | ||
| javaHomes | ||
| .filter { it.key.matches(Regex("^JAVA_[0-9]+_HOME$")) } | ||
| .map { Regex("^JAVA_(\\d+)_HOME$").find(it.key)!!.groupValues[1].toInt() } | ||
| }.get() | ||
|
|
||
| if (javaVersions.isEmpty()) { | ||
| throw GradleException("No valid JAVA_X_HOME environment variables found.") | ||
| } | ||
|
|
||
| javaVersions.max().toString() | ||
| } else { | ||
| testJvm | ||
| } | ||
| }.map { project.logger.info("normalized testJvm: $it"); it } | ||
|
|
||
| val testJvmHomePath = normalizedTestJvm.map { | ||
| if (Files.exists(Paths.get(it))) { | ||
| it.normalizeToJDKJavaHome() | ||
| } else { | ||
| val matcher = Regex("([a-zA-Z]*)([0-9]+)").find(it) | ||
| if (matcher == null) { | ||
| throw GradleException("Unable to find launcher for Java '$it'. It needs to match '([a-zA-Z]*)([0-9]+)'.") | ||
| } | ||
| val testJvmEnv = "JAVA_${it}_HOME" | ||
| val testJvmHome = project.providers.environmentVariable(testJvmEnv).orNull | ||
| if (testJvmHome == null) { | ||
| throw GradleException("Unable to find launcher for Java '$it'. Have you set '$testJvmEnv'?") | ||
| } | ||
|
|
||
| testJvmHome.normalizeToJDKJavaHome() | ||
| } | ||
| }.map { project.logger.info("testJvm home path: $it"); it } | ||
|
|
||
| val javaTestLauncher = project.providers.zip(testJvmHomePath, normalizedTestJvm) { testJvmHome, testJvm -> | ||
| // Only change test JVM if it's not the one we are running the gradle build with | ||
| if (currentJavaHomePath.get() == testJvmHome) { | ||
| project.providers.provider<JavaLauncher?> { null } | ||
| } else { | ||
| // This is using internal APIs | ||
| val jvmSpec = org.gradle.jvm.toolchain.internal.SpecificInstallationToolchainSpec( | ||
| project.serviceOf<org.gradle.api.internal.provider.PropertyFactory>(), | ||
| project.file(testJvmHome) | ||
| ) | ||
|
|
||
| // The provider always says that a value is present so we need to wrap it for proper error messages | ||
| project.javaToolchains.launcherFor(jvmSpec).orElse(project.providers.provider { | ||
| throw GradleException("Unable to find launcher for Java $testJvm. Does '$testJvmHome' point to a JDK?") | ||
| }) | ||
| } | ||
| }.flatMap { it }.map { project.logger.info("testJvm launcher: ${it.executablePath}"); it } | ||
|
|
||
| private fun String.normalizeToJDKJavaHome(): Path { | ||
| val javaHome = project.file(this).toPath().toRealPath() | ||
| return if (javaHome.endsWith("jre")) javaHome.parent else javaHome | ||
| } | ||
|
|
||
| private val Project.javaToolchains: JavaToolchainService get() = | ||
| extensions.getByName("javaToolchains") as JavaToolchainService | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 13 additions & 9 deletions
22
dd-java-agent/agent-profiling/profiling-controller-ddprof/build.gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it make sense to extract
TestJvmConstraintsExtension.NAMEto local var?