@@ -4,30 +4,14 @@ import com.carrotsearch.ant.tasks.junit4.JUnit4
4
4
import org.gradle.api.Plugin
5
5
import org.gradle.api.Project
6
6
import org.gradle.api.Task
7
- import org.gradle.api.UnknownTaskException
8
- import org.gradle.api.plugins.JavaBasePlugin
9
7
import org.gradle.api.tasks.TaskContainer
10
- import org.gradle.api.tasks.TaskProvider
11
- import org.gradle.api.tasks.testing.Test
12
8
13
9
class RandomizedTestingPlugin implements Plugin<Project > {
14
10
15
11
void apply (Project project ) {
16
12
setupSeed(project)
17
- replaceTestTask (project. tasks)
13
+ createUnitTestTask (project. tasks)
18
14
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
- }
31
15
}
32
16
33
17
/**
@@ -57,35 +41,15 @@ class RandomizedTestingPlugin implements Plugin<Project> {
57
41
}
58
42
}
59
43
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
70
52
}
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)
89
53
}
90
54
91
55
static void configureAnt (AntBuilder ant ) {
0 commit comments