@@ -2,42 +2,19 @@ package com.carrotsearch.gradle.junit4
22
33import com.carrotsearch.ant.tasks.junit4.JUnit4
44import org.gradle.api.AntBuilder
5- import org.gradle.api.GradleException
65import org.gradle.api.Plugin
76import org.gradle.api.Project
8- import org.gradle.api.Task
97import org.gradle.api.plugins.JavaBasePlugin
10- import org.gradle.api.tasks.TaskContainer
118import org.gradle.api.tasks.testing.Test
129
1310import java.util.concurrent.atomic.AtomicBoolean
1411
1512class RandomizedTestingPlugin implements Plugin<Project > {
1613
17- static private AtomicBoolean sanityCheckConfigured = new AtomicBoolean (false )
18-
1914 void apply (Project project ) {
2015 setupSeed(project)
21- replaceTestTask (project. tasks )
16+ createTestTask (project)
2217 configureAnt(project. ant)
23- configureSanityCheck(project)
24- }
25-
26- private static void configureSanityCheck (Project project ) {
27- // Check the task graph to confirm tasks were indeed replaced
28- // https://github.com/elastic/elasticsearch/issues/31324
29- if (sanityCheckConfigured. getAndSet(true ) == false ) {
30- project. rootProject. getGradle(). getTaskGraph(). whenReady {
31- def nonConforming = project. getGradle(). getTaskGraph(). allTasks
32- .findAll { it. name == " test" }
33- .findAll { (it instanceof RandomizedTestingTask ) == false }
34- .collect { " ${ it.path} -> ${ it.class} " }
35- if (nonConforming. isEmpty() == false ) {
36- throw new GradleException (" Found the ${ nonConforming.size()} `test` tasks:" +
37- " \n ${ nonConforming.join("\n ")} " )
38- }
39- }
40- }
4118 }
4219
4320 /**
@@ -67,32 +44,40 @@ class RandomizedTestingPlugin implements Plugin<Project> {
6744 }
6845 }
6946
70- static void replaceTestTask (TaskContainer tasks ) {
71- Test oldTestTask = tasks. findByPath(' test' )
72- if (oldTestTask == null ) {
73- // no test task, ok, user will use testing task on their own
47+ static void createTestTask (Project project ) {
48+ Test oldTestTask = project. tasks. findByPath(' test' )
49+ if (oldTestTask != null ) {
50+ oldTestTask. enabled = false
51+ if (oldTestTask. getDependsOn(). isEmpty() == false ) {
52+ // we used to pass dependencies along to the new task,
53+ // we no longer do, so make sure nobody relies on that.
54+ throw new Exception (" did not expect any dependencies for test task but got: ${ oldTestTask.getDependsOn()} " )
55+ }
56+ oldTestTask. dependsOn(' utest' )
57+ } else {
7458 return
7559 }
76- tasks. remove(oldTestTask)
7760
78- Map properties = [
79- name : ' test ' ,
61+ RandomizedTestingTask newTestTask = project . tasks . create( [
62+ name : ' utest ' ,
8063 type : RandomizedTestingTask ,
81- dependsOn : oldTestTask. dependsOn,
8264 group : JavaBasePlugin . VERIFICATION_GROUP ,
8365 description : ' Runs unit tests with the randomized testing framework'
84- ]
85- RandomizedTestingTask newTestTask = tasks. create(properties)
66+ ])
8667 newTestTask. classpath = oldTestTask. classpath
8768 newTestTask. testClassesDir = oldTestTask. project. sourceSets. test. output. classesDir
88- // since gradle 4.5, tasks immutable dependencies are "hidden" (do not show up in dependsOn)
89- // so we must explicitly add a dependency on generating the test classpath
9069 newTestTask. dependsOn(' testClasses' )
9170
92- // hack so check task depends on custom test
93- Task checkTask = tasks. findByPath(' check' )
94- checkTask. dependsOn. remove(oldTestTask)
95- checkTask. dependsOn. add(newTestTask)
71+ project. tasks. findByPath(' check' ). dependsOn. add(newTestTask)
72+ // if there isn't actually a tests folder disable the task. Since we still have IT and Test in mixed folders
73+ // need to check by file name convention
74+ Set<File > testSources = project. sourceSets. test. java. getFiles(). findAll { it. name. endsWith(" Tests.java" ) }
75+ if (testSources. isEmpty()) {
76+ newTestTask. enabled = false
77+ project. logger. info(" Found ${ testSources.size()} source files, utest task will be disabled" )
78+ } else {
79+ project. logger. debug(" Found ${ testSources.size()} source files, utest task will be enabled" )
80+ }
9681 }
9782
9883 static void configureAnt (AntBuilder ant ) {
0 commit comments