Skip to content

Commit e9ef5bd

Browse files
authored
Converting randomized testing to create a separate unitTest task instead of replacing the builtin test task (#36311)
- Create a separate unitTest task instead of Gradle's built in - convert all configuration to use the new task - the built in task is now disabled
1 parent 8015560 commit e9ef5bd

File tree

67 files changed

+87
-122
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+87
-122
lines changed

benchmarks/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ mainClassName = 'org.openjdk.jmh.Main'
2424
assemble.enabled = false
2525
archivesBaseName = 'elasticsearch-benchmarks'
2626

27-
test.enabled = false
27+
unitTest.enabled = false
2828

2929
dependencies {
3030
compile("org.elasticsearch:elasticsearch:${version}") {

buildSrc/build.gradle

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ if (project != rootProject) {
179179
jarHell.enabled = false
180180
thirdPartyAudit.enabled = false
181181

182-
test {
183-
include "**/*Tests.class"
184-
exclude "**/*IT.class"
182+
unitTest {
185183
// The test task is configured to runtimeJava version, but build-tools doesn't support all of them, so test
186184
// with compiler instead on the ones that are too old.
187185
if (project.runtimeJavaVersion <= JavaVersion.VERSION_1_10) {

buildSrc/src/main/groovy/com/carrotsearch/gradle/junit4/RandomizedTestingPlugin.groovy

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,14 @@ import com.carrotsearch.ant.tasks.junit4.JUnit4
44
import org.gradle.api.Plugin
55
import org.gradle.api.Project
66
import org.gradle.api.Task
7-
import org.gradle.api.UnknownTaskException
8-
import org.gradle.api.plugins.JavaBasePlugin
97
import org.gradle.api.tasks.TaskContainer
10-
import org.gradle.api.tasks.TaskProvider
11-
import org.gradle.api.tasks.testing.Test
128

139
class RandomizedTestingPlugin implements Plugin<Project> {
1410

1511
void apply(Project project) {
1612
setupSeed(project)
17-
replaceTestTask(project.tasks)
13+
createUnitTestTask(project.tasks)
1814
configureAnt(project.ant)
19-
configureSanityCheck(project)
20-
}
21-
22-
private static void configureSanityCheck(Project project) {
23-
// Check the task graph to confirm tasks were indeed replaced
24-
// https://github.com/elastic/elasticsearch/issues/31324
25-
project.rootProject.getGradle().getTaskGraph().whenReady {
26-
Task test = project.getTasks().findByName("test")
27-
if (test != null && (test instanceof RandomizedTestingTask) == false) {
28-
throw new IllegalStateException("Test task was not replaced in project ${project.path}. Found ${test.getClass()}")
29-
}
30-
}
3115
}
3216

3317
/**
@@ -57,35 +41,15 @@ class RandomizedTestingPlugin implements Plugin<Project> {
5741
}
5842
}
5943

60-
static void replaceTestTask(TaskContainer tasks) {
61-
// Gradle 4.8 introduced lazy tasks, thus we deal both with the `test` task as well as it's provider
62-
// https://github.com/gradle/gradle/issues/5730#issuecomment-398822153
63-
// since we can't be sure if the task was ever realized, we remove both the provider and the task
64-
TaskProvider<Test> oldTestProvider
65-
try {
66-
oldTestProvider = tasks.named('test')
67-
} catch (UnknownTaskException unused) {
68-
// no test task, ok, user will use testing task on their own
69-
return
44+
static void createUnitTestTask(TaskContainer tasks) {
45+
// only create a unitTest task if the `test` task exists as some project don't make use of it.
46+
tasks.matching { it.name == "test" }.all {
47+
// We don't want to run any tests with the Gradle test runner since we add our own randomized runner
48+
it.enabled = false
49+
RandomizedTestingTask unitTest = tasks.create('unitTest', RandomizedTestingTask)
50+
unitTest.description = 'Runs unit tests with the randomized testing framework'
51+
it.dependsOn unitTest
7052
}
71-
Test oldTestTask = oldTestProvider.get()
72-
73-
// we still have to use replace here despite the remove above because the task container knows about the provider
74-
// by the same name
75-
RandomizedTestingTask newTestTask = tasks.replace('test', RandomizedTestingTask)
76-
newTestTask.configure{
77-
group = JavaBasePlugin.VERIFICATION_GROUP
78-
description = 'Runs unit tests with the randomized testing framework'
79-
dependsOn oldTestTask.dependsOn, 'testClasses'
80-
classpath = oldTestTask.classpath
81-
testClassesDirs = oldTestTask.project.sourceSets.test.output.classesDirs
82-
}
83-
84-
// hack so check task depends on custom test
85-
Task checkTask = tasks.getByName('check')
86-
checkTask.dependsOn.remove(oldTestProvider)
87-
checkTask.dependsOn.remove(oldTestTask)
88-
checkTask.dependsOn.add(newTestTask)
8953
}
9054

9155
static void configureAnt(AntBuilder ant) {

buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import org.gradle.api.artifacts.ProjectDependency
4040
import org.gradle.api.artifacts.ResolvedArtifact
4141
import org.gradle.api.artifacts.dsl.RepositoryHandler
4242
import org.gradle.api.execution.TaskExecutionGraph
43+
import org.gradle.api.plugins.JavaBasePlugin
4344
import org.gradle.api.plugins.JavaPlugin
4445
import org.gradle.api.publish.maven.MavenPublication
4546
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
@@ -888,15 +889,22 @@ class BuildPlugin implements Plugin<Project> {
888889
parallelism System.getProperty('tests.jvms', project.rootProject.ext.defaultParallel)
889890
onNonEmptyWorkDirectory 'wipe'
890891
leaveTemporary true
892+
project.sourceSets.matching { it.name == "test" }.all { test ->
893+
task.testClassesDirs = test.output.classesDirs
894+
task.classpath = test.runtimeClasspath
895+
}
896+
group = JavaBasePlugin.VERIFICATION_GROUP
897+
dependsOn 'testClasses'
891898

892899
// Make sure all test tasks are configured properly
893900
if (name != "test") {
894901
project.tasks.matching { it.name == "test"}.all { testTask ->
895-
task.testClassesDirs = testTask.testClassesDirs
896-
task.classpath = testTask.classpath
897902
task.shouldRunAfter testTask
898903
}
899904
}
905+
if (name == "unitTest") {
906+
include("**/*Tests.class")
907+
}
900908

901909
// TODO: why are we not passing maxmemory to junit4?
902910
jvmArg '-Xmx' + System.getProperty('tests.heap.size', '512m')
@@ -986,8 +994,6 @@ class BuildPlugin implements Plugin<Project> {
986994

987995
exclude '**/*$*.class'
988996

989-
dependsOn(project.tasks.testClasses)
990-
991997
project.plugins.withType(ShadowPlugin).whenPluginAdded {
992998
// Test against a shadow jar if we made one
993999
classpath -= project.tasks.compileJava.outputs.files

buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestIntegTestTask.groovy

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public class RestIntegTestTask extends DefaultTask {
5555
super.dependsOn(runner)
5656
clusterInit = project.tasks.create(name: "${name}Cluster#init", dependsOn: project.testClasses)
5757
runner.dependsOn(clusterInit)
58-
runner.classpath = project.sourceSets.test.runtimeClasspath
59-
runner.testClassesDirs = project.sourceSets.test.output.classesDirs
6058
clusterConfig = project.extensions.create("${name}Cluster", ClusterConfiguration.class, project)
6159

6260
// override/add more for rest tests

buildSrc/src/testKit/elasticsearch.build/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ forbiddenApisTest.enabled = false
2727
// requires dependency on testing fw
2828
jarHell.enabled = false
2929
// we don't have tests for now
30-
test.enabled = false
30+
unitTest.enabled = false
3131

3232
task hello {
3333
doFirst {

client/benchmark/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ archivesBaseName = 'client-benchmarks'
2929
mainClassName = 'org.elasticsearch.client.benchmark.BenchmarkMain'
3030

3131
// never try to invoke tests on the benchmark project - there aren't any
32-
test.enabled = false
32+
unitTest.enabled = false
3333

3434
dependencies {
3535
compile 'org.apache.commons:commons-math3:3.2'

client/client-benchmark-noop-api-plugin/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ dependenciesInfo.enabled = false
3636
compileJava.options.compilerArgs << "-Xlint:-cast,-deprecation,-rawtypes,-try,-unchecked"
3737

3838
// no unit tests
39-
test.enabled = false
39+
unitTest.enabled = false
4040
integTest.enabled = false

client/test/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ namingConventions.enabled = false
5555

5656
//we aren't releasing this jar
5757
thirdPartyAudit.enabled = false
58-
test.enabled = false
58+
unitTest.enabled = false

distribution/bwc/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ bwcVersions.forPreviousUnreleased { VersionCollection.UnreleasedVersionInfo unre
3939
apply plugin: 'distribution'
4040
// Not published so no need to assemble
4141
assemble.enabled = false
42-
assemble.dependsOn.remove('buildBwcVersion')
4342

4443
File checkoutDir = file("${buildDir}/bwc/checkout-${bwcBranch}")
4544

0 commit comments

Comments
 (0)