@@ -4,30 +4,14 @@ import com.carrotsearch.ant.tasks.junit4.JUnit4
44import org.gradle.api.Plugin
55import org.gradle.api.Project
66import org.gradle.api.Task
7- import org.gradle.api.UnknownTaskException
8- import org.gradle.api.plugins.JavaBasePlugin
97import org.gradle.api.tasks.TaskContainer
10- import org.gradle.api.tasks.TaskProvider
11- import org.gradle.api.tasks.testing.Test
128
139class 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 ) {
0 commit comments