Skip to content

Commit 2194059

Browse files
committed
Create configure-tests convention plugin
1 parent 45b8e15 commit 2194059

File tree

3 files changed

+257
-120
lines changed

3 files changed

+257
-120
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import org.gradle.api.tasks.testing.Test
2+
import org.gradle.api.tasks.testing.junitplatform.JUnitPlatformOptions
3+
import org.gradle.api.services.BuildService
4+
import org.gradle.api.services.BuildServiceParameters
5+
import java.time.Duration
6+
import java.time.temporal.ChronoUnit
7+
8+
fun isTestingInstrumentation(project: Project): Boolean {
9+
return listOf(
10+
"junit-4.10",
11+
"cucumber",
12+
"cucumber-junit-4",
13+
"junit-4.13",
14+
"munit-junit-4",
15+
"junit-5.3",
16+
"junit-5.8",
17+
"cucumber-junit-5",
18+
"spock-junit-5",
19+
"testng-6",
20+
"testng-7",
21+
"karate",
22+
"scalatest",
23+
"selenium",
24+
"weaver"
25+
).contains(project.name)
26+
}
27+
28+
// Need concrete implementation of BuildService in Kotlin
29+
abstract class ForkedTestLimit : BuildService<BuildServiceParameters.None>
30+
31+
val forkedTestLimit = gradle.sharedServices.registerIfAbsent("forkedTestLimit", ForkedTestLimit::class.java) {
32+
maxParallelUsages.set(3)
33+
}
34+
35+
// Go through the Test tasks and configure them
36+
tasks.withType(Test::class.java).configureEach {
37+
// Disable all tests if skipTests property was specified
38+
onlyIf { !project.rootProject.hasProperty("skipTests") }
39+
40+
// Enable force rerun of tests with -Prerun.tests.${project.name}
41+
outputs.upToDateWhen {
42+
!project.rootProject.hasProperty("rerun.tests.${project.name}")
43+
}
44+
45+
// Avoid executing classes used to test testing frameworks instrumentation
46+
if (isTestingInstrumentation(project)) {
47+
exclude("**/TestSucceed*")
48+
exclude("**/TestFailed*")
49+
exclude("**/TestFailedWithSuccessPercentage*")
50+
exclude("**/TestError*")
51+
exclude("**/TestSkipped*")
52+
exclude("**/TestSkippedClass*")
53+
exclude("**/TestInheritance*", "**/BaseTestInheritance*")
54+
exclude("**/TestFactory*")
55+
exclude("**/TestParameterized*")
56+
exclude("**/TestRepeated*")
57+
exclude("**/TestTemplate*")
58+
exclude("**/TestDisableTestTrace*")
59+
exclude("**/TestAssumption*", "**/TestSuiteSetUpAssumption*")
60+
exclude("**/TestUnskippable*")
61+
exclude("**/TestWithSetup*")
62+
}
63+
64+
// Split up tests that want to run forked in their own separate JVM for generated tasks
65+
if (name.startsWith("forkedTest") || name.endsWith("ForkedTest")) {
66+
setExcludes(emptyList())
67+
setIncludes(listOf("**/*ForkedTest*"))
68+
forkEvery = 1
69+
// Limit the number of concurrent forked tests
70+
usesService(forkedTestLimit)
71+
onlyIf { !project.rootProject.hasProperty("skipForkedTests") }
72+
} else {
73+
exclude("**/*ForkedTest*")
74+
}
75+
76+
// Set test timeout for 20 minutes. Default job timeout is 1h (configured on CI level).
77+
timeout.set(Duration.of(20, ChronoUnit.MINUTES))
78+
}
79+
80+
val allTestsTask = tasks.maybeCreate("allTests")
81+
val allLatestDepTestsTask = tasks.maybeCreate("allLatestDepTests")
82+
val checkTask = tasks.named("check")
83+
84+
project.afterEvaluate {
85+
tasks.withType(Test::class.java).forEach {
86+
// Add test to appropriate aggregate task
87+
if (it.name.contains("latest", ignoreCase = true)) {
88+
allLatestDepTestsTask.dependsOn(it)
89+
} else if (it.name != "traceAgentTest") {
90+
allTestsTask.dependsOn(it)
91+
}
92+
// Make check depend on this test task
93+
checkTask.configure {
94+
dependsOn(it)
95+
}
96+
}
97+
}
98+
99+
// Setup flaky tests jobs. Done in afterEvaluate so that it applies to latestDepTest.
100+
project.afterEvaluate {
101+
tasks.withType(Test::class.java).configureEach {
102+
// Flaky tests management for JUnit 5
103+
if (testFramework is JUnitPlatformOptions) {
104+
val junitPlatform = testFramework as JUnitPlatformOptions
105+
if (project.rootProject.hasProperty("skipFlakyTests")) {
106+
junitPlatform.excludeTags("flaky")
107+
} else if (project.rootProject.hasProperty("runFlakyTests")) {
108+
junitPlatform.includeTags("flaky")
109+
}
110+
}
111+
112+
// Flaky tests management for Spock
113+
if (project.rootProject.hasProperty("skipFlakyTests")) {
114+
jvmArgs("-Drun.flaky.tests=false")
115+
} else if (project.rootProject.hasProperty("runFlakyTests")) {
116+
jvmArgs("-Drun.flaky.tests=true")
117+
}
118+
}
119+
}
120+
121+
if (!(project.property("activePartition") as Boolean)) {
122+
project.afterEvaluate {
123+
tasks.withType(Test::class.java).configureEach {
124+
enabled = false
125+
}
126+
}
127+
}
128+
129+
// tasks.withType(Test).configureEach {
130+
// // https://docs.gradle.com/develocity/flaky-test-detection/
131+
// // https://docs.gradle.com/develocity/gradle-plugin/current/#test_retry
132+
// develocity.testRetry {
133+
// if (providers.environmentVariable("CI").isPresent()) {
134+
// maxRetries = 3
135+
// }
136+
// }
137+
// }

gradle/configure_tests.gradle

Lines changed: 119 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,133 @@
1-
import java.time.Duration
2-
import java.time.temporal.ChronoUnit
1+
// import java.time.Duration
2+
// import java.time.temporal.ChronoUnit
33

4-
def isTestingInstrumentation(Project project) {
5-
return [
6-
"junit-4.10",
7-
"cucumber",
8-
"cucumber-junit-4",
9-
"junit-4.13",
10-
"munit-junit-4",
11-
"junit-5.3",
12-
"junit-5.8",
13-
"cucumber-junit-5",
14-
"spock-junit-5",
15-
"testng-6",
16-
"testng-7",
17-
"karate",
18-
"scalatest",
19-
"selenium",
20-
"weaver"
21-
].contains(project.name)
22-
}
4+
// def isTestingInstrumentation(Project project) {
5+
// return [
6+
// "junit-4.10",
7+
// "cucumber",
8+
// "cucumber-junit-4",
9+
// "junit-4.13",
10+
// "munit-junit-4",
11+
// "junit-5.3",
12+
// "junit-5.8",
13+
// "cucumber-junit-5",
14+
// "spock-junit-5",
15+
// "testng-6",
16+
// "testng-7",
17+
// "karate",
18+
// "scalatest",
19+
// "selenium",
20+
// "weaver"
21+
// ].contains(project.name)
22+
// }
2323

24-
def forkedTestLimit = gradle.sharedServices.registerIfAbsent("forkedTestLimit", BuildService) {
25-
maxParallelUsages = 3
26-
}
24+
// def forkedTestLimit = gradle.sharedServices.registerIfAbsent("forkedTestLimit", BuildService) {
25+
// maxParallelUsages = 3
26+
// }
2727

28-
testing {
29-
suites.configureEach {
30-
// Use JUnit 5 to run tests
31-
useJUnitJupiter()
32-
}
33-
}
28+
// testing {
29+
// suites.configureEach {
30+
// // Use JUnit 5 to run tests
31+
// useJUnitJupiter()
32+
// }
33+
// }
3434

35-
// Go through the Test tasks and configure them
36-
tasks.withType(Test).configureEach {
37-
// Disable all tests if skipTests property was specified
38-
onlyIf { !project.rootProject.hasProperty("skipTests") }
35+
// // Go through the Test tasks and configure them
36+
// tasks.withType(Test).configureEach {
37+
// // Disable all tests if skipTests property was specified
38+
// onlyIf { !project.rootProject.hasProperty("skipTests") }
3939

40-
// Enable force rerun of tests with -Prerun.tests.${project.name}
41-
outputs.upToDateWhen {
42-
!project.rootProject.hasProperty("rerun.tests.${project.name}")
43-
}
40+
// // Enable force rerun of tests with -Prerun.tests.${project.name}
41+
// outputs.upToDateWhen {
42+
// !project.rootProject.hasProperty("rerun.tests.${project.name}")
43+
// }
4444

45-
// Avoid executing classes used to test testing frameworks instrumentation
46-
if (isTestingInstrumentation(project)) {
47-
exclude("**/TestSucceed*")
48-
exclude("**/TestFailed*")
49-
exclude("**/TestFailedWithSuccessPercentage*")
50-
exclude("**/TestError*")
51-
exclude("**/TestSkipped*")
52-
exclude("**/TestSkippedClass*")
53-
exclude("**/TestInheritance*", "**/BaseTestInheritance*")
54-
exclude("**/TestFactory*")
55-
exclude("**/TestParameterized*")
56-
exclude("**/TestRepeated*")
57-
exclude("**/TestTemplate*")
58-
exclude("**/TestDisableTestTrace*")
59-
exclude("**/TestAssumption*", "**/TestSuiteSetUpAssumption*")
60-
exclude("**/TestUnskippable*")
61-
exclude("**/TestWithSetup*")
62-
}
45+
// // Avoid executing classes used to test testing frameworks instrumentation
46+
// if (isTestingInstrumentation(project)) {
47+
// exclude("**/TestSucceed*")
48+
// exclude("**/TestFailed*")
49+
// exclude("**/TestFailedWithSuccessPercentage*")
50+
// exclude("**/TestError*")
51+
// exclude("**/TestSkipped*")
52+
// exclude("**/TestSkippedClass*")
53+
// exclude("**/TestInheritance*", "**/BaseTestInheritance*")
54+
// exclude("**/TestFactory*")
55+
// exclude("**/TestParameterized*")
56+
// exclude("**/TestRepeated*")
57+
// exclude("**/TestTemplate*")
58+
// exclude("**/TestDisableTestTrace*")
59+
// exclude("**/TestAssumption*", "**/TestSuiteSetUpAssumption*")
60+
// exclude("**/TestUnskippable*")
61+
// exclude("**/TestWithSetup*")
62+
// }
6363

64-
// Split up tests that want to run forked in their own separate JVM for generated tasks
65-
if (name.startsWith("forkedTest") || name.endsWith("ForkedTest")) {
66-
setExcludes([])
67-
setIncludes(["**/*ForkedTest*"])
68-
forkEvery = 1
69-
// Limit the number of concurrent forked tests
70-
usesService(forkedTestLimit)
71-
onlyIf { !project.rootProject.hasProperty("skipForkedTests") }
72-
} else {
73-
exclude("**/*ForkedTest*")
74-
}
64+
// // Split up tests that want to run forked in their own separate JVM for generated tasks
65+
// if (name.startsWith("forkedTest") || name.endsWith("ForkedTest")) {
66+
// setExcludes([])
67+
// setIncludes(["**/*ForkedTest*"])
68+
// forkEvery = 1
69+
// // Limit the number of concurrent forked tests
70+
// usesService(forkedTestLimit)
71+
// onlyIf { !project.rootProject.hasProperty("skipForkedTests") }
72+
// } else {
73+
// exclude("**/*ForkedTest*")
74+
// }
7575

76-
// Set test timeout for 20 minutes. Default job timeout is 1h (configured on CI level).
77-
timeout = Duration.of(20, ChronoUnit.MINUTES)
76+
// // Set test timeout for 20 minutes. Default job timeout is 1h (configured on CI level).
77+
// timeout = Duration.of(20, ChronoUnit.MINUTES)
7878

79-
check.dependsOn(it)
80-
}
79+
// check.dependsOn(it)
80+
// }
8181

82-
Task allTestsTask = tasks.maybeCreate('allTests')
83-
Task allLatestDepTestsTask = tasks.maybeCreate('allLatestDepTests')
84-
project.afterEvaluate {
85-
tasks.withType(Test).each {
86-
if (it.name.containsIgnoreCase('latest')) {
87-
allLatestDepTestsTask.dependsOn it
88-
} else if (it.name != 'traceAgentTest') {
89-
allTestsTask.dependsOn it
90-
}
91-
}
92-
}
82+
// Task allTestsTask = tasks.maybeCreate('allTests')
83+
// Task allLatestDepTestsTask = tasks.maybeCreate('allLatestDepTests')
84+
// project.afterEvaluate {
85+
// tasks.withType(Test).each {
86+
// if (it.name.containsIgnoreCase('latest')) {
87+
// allLatestDepTestsTask.dependsOn it
88+
// } else if (it.name != 'traceAgentTest') {
89+
// allTestsTask.dependsOn it
90+
// }
91+
// }
92+
// }
9393

94-
// Setup flaky tests jobs. Done in afterEvaluate so that it applies to latestDepTest.
95-
project.afterEvaluate {
96-
tasks.withType(Test).configureEach {
97-
// Flaky tests management for JUnit 5
98-
testFramework {
99-
if (it instanceof JUnitPlatformOptions) {
100-
if (project.rootProject.hasProperty("skipFlakyTests")) {
101-
excludeTags("flaky")
102-
} else if (project.rootProject.hasProperty("runFlakyTests")) {
103-
includeTags("flaky")
104-
}
105-
}
106-
}
94+
// // Setup flaky tests jobs. Done in afterEvaluate so that it applies to latestDepTest.
95+
// project.afterEvaluate {
96+
// tasks.withType(Test).configureEach {
97+
// // Flaky tests management for JUnit 5
98+
// testFramework {
99+
// if (it instanceof JUnitPlatformOptions) {
100+
// if (project.rootProject.hasProperty("skipFlakyTests")) {
101+
// excludeTags("flaky")
102+
// } else if (project.rootProject.hasProperty("runFlakyTests")) {
103+
// includeTags("flaky")
104+
// }
105+
// }
106+
// }
107107

108-
// Flaky tests management for Spock
109-
if (project.rootProject.hasProperty("skipFlakyTests")) {
110-
jvmArgs += ["-Drun.flaky.tests=false"]
111-
} else if (project.rootProject.hasProperty("runFlakyTests")) {
112-
jvmArgs += ["-Drun.flaky.tests=true"]
113-
}
114-
}
115-
}
108+
// // Flaky tests management for Spock
109+
// if (project.rootProject.hasProperty("skipFlakyTests")) {
110+
// jvmArgs += ["-Drun.flaky.tests=false"]
111+
// } else if (project.rootProject.hasProperty("runFlakyTests")) {
112+
// jvmArgs += ["-Drun.flaky.tests=true"]
113+
// }
114+
// }
115+
// }
116116

117-
if (!project.property("activePartition")) {
118-
project.afterEvaluate {
119-
tasks.withType(Test).configureEach {
120-
enabled = false
121-
}
122-
}
123-
}
117+
// if (!project.property("activePartition")) {
118+
// project.afterEvaluate {
119+
// tasks.withType(Test).configureEach {
120+
// enabled = false
121+
// }
122+
// }
123+
// }
124124

125-
tasks.withType(Test).configureEach {
126-
// https://docs.gradle.com/develocity/flaky-test-detection/
127-
// https://docs.gradle.com/develocity/gradle-plugin/current/#test_retry
128-
develocity.testRetry {
129-
if (providers.environmentVariable("CI").isPresent()) {
130-
maxRetries = 3
131-
}
132-
}
133-
}
125+
// tasks.withType(Test).configureEach {
126+
// // https://docs.gradle.com/develocity/flaky-test-detection/
127+
// // https://docs.gradle.com/develocity/gradle-plugin/current/#test_retry
128+
// develocity.testRetry {
129+
// if (providers.environmentVariable("CI").isPresent()) {
130+
// maxRetries = 3
131+
// }
132+
// }
133+
// }

gradle/java_no_deps.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,4 +466,4 @@ tasks.register('testJar', Jar) {
466466
archiveClassifier = 'test'
467467
}
468468

469-
apply from: "$rootDir/gradle/configure_tests.gradle"
469+
apply plugin: 'datadog.configure-tests'

0 commit comments

Comments
 (0)