|
@@ -1,20 +1,44 @@
|
|
|
package com.carrotsearch.gradle.junit4
|
|
|
|
|
|
import com.carrotsearch.ant.tasks.junit4.JUnit4
|
|
|
-import org.gradle.api.AntBuilder
|
|
|
+import org.gradle.api.GradleException
|
|
|
import org.gradle.api.Plugin
|
|
|
import org.gradle.api.Project
|
|
|
import org.gradle.api.Task
|
|
|
+import org.gradle.api.UnknownTaskException
|
|
|
import org.gradle.api.plugins.JavaBasePlugin
|
|
|
import org.gradle.api.tasks.TaskContainer
|
|
|
+import org.gradle.api.tasks.TaskProvider
|
|
|
import org.gradle.api.tasks.testing.Test
|
|
|
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean
|
|
|
+
|
|
|
class RandomizedTestingPlugin implements Plugin<Project> {
|
|
|
|
|
|
+ static private AtomicBoolean sanityCheckConfigured = new AtomicBoolean(false)
|
|
|
+
|
|
|
void apply(Project project) {
|
|
|
setupSeed(project)
|
|
|
replaceTestTask(project.tasks)
|
|
|
configureAnt(project.ant)
|
|
|
+ configureSanityCheck(project)
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void configureSanityCheck(Project project) {
|
|
|
+ // Check the task graph to confirm tasks were indeed replaced
|
|
|
+ // https://github.com/elastic/elasticsearch/issues/31324
|
|
|
+ if (sanityCheckConfigured.getAndSet(true) == false) {
|
|
|
+ project.rootProject.getGradle().getTaskGraph().whenReady {
|
|
|
+ List<Task> nonConforming = project.getGradle().getTaskGraph().allTasks
|
|
|
+ .findAll { it.name == "test" }
|
|
|
+ .findAll { (it instanceof RandomizedTestingTask) == false}
|
|
|
+ .collect { "${it.path} -> ${it.class}" }
|
|
|
+ if (nonConforming.isEmpty() == false) {
|
|
|
+ throw new GradleException("Found the ${nonConforming.size()} `test` tasks:" +
|
|
|
+ "\n ${nonConforming.join("\n ")}")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -45,29 +69,32 @@ class RandomizedTestingPlugin implements Plugin<Project> {
|
|
|
}
|
|
|
|
|
|
static void replaceTestTask(TaskContainer tasks) {
|
|
|
- Test oldTestTask = tasks.findByPath('test')
|
|
|
- if (oldTestTask == null) {
|
|
|
+ // Gradle 4.8 introduced lazy tasks, thus we deal both with the `test` task as well as it's provider
|
|
|
+ // https://github.com/gradle/gradle/issues/5730#issuecomment-398822153
|
|
|
+ // since we can't be sure if the task was ever realized, we remove both the provider and the task
|
|
|
+ TaskProvider<Test> oldTestProvider
|
|
|
+ try {
|
|
|
+ oldTestProvider = tasks.getByNameLater(Test, 'test')
|
|
|
+ } catch (UnknownTaskException unused) {
|
|
|
// no test task, ok, user will use testing task on their own
|
|
|
return
|
|
|
}
|
|
|
- tasks.remove(oldTestTask)
|
|
|
+ Test oldTestTask = oldTestProvider.get()
|
|
|
|
|
|
- Map properties = [
|
|
|
- name: 'test',
|
|
|
- type: RandomizedTestingTask,
|
|
|
- dependsOn: oldTestTask.dependsOn,
|
|
|
- group: JavaBasePlugin.VERIFICATION_GROUP,
|
|
|
- description: 'Runs unit tests with the randomized testing framework'
|
|
|
- ]
|
|
|
- RandomizedTestingTask newTestTask = tasks.create(properties)
|
|
|
- newTestTask.classpath = oldTestTask.classpath
|
|
|
- newTestTask.testClassesDir = oldTestTask.project.sourceSets.test.output.classesDir
|
|
|
- // since gradle 4.5, tasks immutable dependencies are "hidden" (do not show up in dependsOn)
|
|
|
- // so we must explicitly add a dependency on generating the test classpath
|
|
|
- newTestTask.dependsOn('testClasses')
|
|
|
+ // we still have to use replace here despite the remove above because the task container knows about the provider
|
|
|
+ // by the same name
|
|
|
+ RandomizedTestingTask newTestTask = tasks.replace('test', RandomizedTestingTask)
|
|
|
+ newTestTask.configure{
|
|
|
+ group = JavaBasePlugin.VERIFICATION_GROUP
|
|
|
+ description = 'Runs unit tests with the randomized testing framework'
|
|
|
+ dependsOn oldTestTask.dependsOn, 'testClasses'
|
|
|
+ classpath = oldTestTask.classpath
|
|
|
+ testClassesDir = oldTestTask.project.sourceSets.test.output.classesDir
|
|
|
+ }
|
|
|
|
|
|
// hack so check task depends on custom test
|
|
|
- Task checkTask = tasks.findByPath('check')
|
|
|
+ Task checkTask = tasks.getByName('check')
|
|
|
+ checkTask.dependsOn.remove(oldTestProvider)
|
|
|
checkTask.dependsOn.remove(oldTestTask)
|
|
|
checkTask.dependsOn.add(newTestTask)
|
|
|
}
|