summaryrefslogtreecommitdiff
path: root/buildSrc/src/main/groovy/com/carrotsearch/gradle/junit4/RandomizedTestingTask.groovy
blob: ccb5d5904bf0c5a1df2e8b7b95a600ad5ae0a94a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package com.carrotsearch.gradle.junit4

import com.carrotsearch.ant.tasks.junit4.ListenersList
import com.carrotsearch.ant.tasks.junit4.listeners.AggregatedEventListener
import com.esotericsoftware.kryo.serializers.FieldSerializer
import groovy.xml.NamespaceBuilder
import groovy.xml.NamespaceBuilderSupport
import org.apache.tools.ant.BuildException
import org.apache.tools.ant.DefaultLogger
import org.apache.tools.ant.RuntimeConfigurable
import org.apache.tools.ant.UnknownElement
import org.gradle.api.DefaultTask
import org.gradle.api.file.FileCollection
import org.gradle.api.file.FileTreeElement
import org.gradle.api.internal.tasks.options.Option
import org.gradle.api.specs.Spec
import org.gradle.api.tasks.*
import org.gradle.api.tasks.util.PatternFilterable
import org.gradle.api.tasks.util.PatternSet
import org.gradle.logging.ProgressLoggerFactory
import org.gradle.util.ConfigureUtil

import javax.inject.Inject

class RandomizedTestingTask extends DefaultTask {

    // TODO: change to "executable" to match gradle test params?
    @Optional
    @Input
    String jvm = 'java'

    @Optional
    @Input
    File workingDir = new File(project.buildDir, 'testrun' + File.separator + name)

    @Optional
    @Input
    FileCollection classpath

    @Input
    String parallelism = '1'

    @InputDirectory
    File testClassesDir

    @Optional
    @Input
    boolean haltOnFailure = true

    @Optional
    @Input
    boolean shuffleOnSlave = true

    @Optional
    @Input
    boolean enableAssertions = true

    @Optional
    @Input
    boolean enableSystemAssertions = true

    @Optional
    @Input
    boolean leaveTemporary = false

    @Optional
    @Input
    String ifNoTests = 'ignore'

    TestLoggingConfiguration testLoggingConfig = new TestLoggingConfiguration()

    BalancersConfiguration balancersConfig = new BalancersConfiguration(task: this)
    ListenersConfiguration listenersConfig = new ListenersConfiguration(task: this)

    List<String> jvmArgs = new ArrayList<>()

    @Optional
    @Input
    String argLine = null

    Map<String, Object> systemProperties = new HashMap<>()
    PatternFilterable patternSet = new PatternSet()

    RandomizedTestingTask() {
        outputs.upToDateWhen {false} // randomized tests are never up to date
        listenersConfig.listeners.add(new TestProgressLogger(factory: getProgressLoggerFactory()))
        listenersConfig.listeners.add(new TestReportLogger(logger: logger, config: testLoggingConfig))
    }

    @Inject
    ProgressLoggerFactory getProgressLoggerFactory() {
        throw new UnsupportedOperationException();
    }

    void jvmArgs(Iterable<String> arguments) {
        jvmArgs.addAll(arguments)
    }

    void jvmArg(String argument) {
        jvmArgs.add(argument)
    }

    void systemProperty(String property, Object value) {
        systemProperties.put(property, value)
    }

    void include(String... includes) {
        this.patternSet.include(includes);
    }

    void include(Iterable<String> includes) {
        this.patternSet.include(includes);
    }

    void include(Spec<FileTreeElement> includeSpec) {
        this.patternSet.include(includeSpec);
    }

    void include(Closure includeSpec) {
        this.patternSet.include(includeSpec);
    }

    void exclude(String... excludes) {
        this.patternSet.exclude(excludes);
    }

    void exclude(Iterable<String> excludes) {
        this.patternSet.exclude(excludes);
    }

    void exclude(Spec<FileTreeElement> excludeSpec) {
        this.patternSet.exclude(excludeSpec);
    }

    void exclude(Closure excludeSpec) {
        this.patternSet.exclude(excludeSpec);
    }

    @Input
    void testLogging(Closure closure) {
        ConfigureUtil.configure(closure, testLoggingConfig)
    }

    @Input
    void balancers(Closure closure) {
        ConfigureUtil.configure(closure, balancersConfig)
    }

    @Input
    void listeners(Closure closure) {
        ConfigureUtil.configure(closure, listenersConfig)
    }

    @Option(
            option = "tests",
            description = "Sets test class or method name to be included. This is for IDEs. Use -Dtests.class and -Dtests.method"
    )
    void setTestNameIncludePattern(String testNamePattern) {
        // This is only implemented to give support for IDEs running tests. There are 3 patterns expected:
        // * An exact test class and method
        // * An exact test class
        // * A package name prefix, ending with .*
        // There is no way to distinguish the first two without looking at classes, so we use the rule
        // that class names start with an uppercase letter...
        // TODO: this doesn't work yet, but not sure why...intellij says it is using --tests, and this work from the command line...
        String[] parts = testNamePattern.split('\\.')
        String lastPart = parts[parts.length - 1]
        String classname
        String methodname = null
        if (lastPart.equals('*') || lastPart.charAt(0).isUpperCase()) {
            // package name or class name, just pass through
            classname = testNamePattern
        } else {
            // method name, need to separate
            methodname = lastPart
            classname = testNamePattern.substring(0, testNamePattern.length() - lastPart.length() - 1)
        }
        ant.setProperty('tests.class', classname)
        if (methodname != null) {
            ant.setProperty('tests.method', methodname)
        }
    }

    @TaskAction
    void executeTests() {
        Map attributes = [
            jvm: jvm,
            parallelism: parallelism,
            heartbeat: testLoggingConfig.slowTests.heartbeat,
            dir: workingDir,
            tempdir: new File(workingDir, 'temp'),
            haltOnFailure: true, // we want to capture when a build failed, but will decide whether to rethrow later
            shuffleOnSlave: shuffleOnSlave,
            leaveTemporary: leaveTemporary,
            ifNoTests: ifNoTests
        ]

        DefaultLogger listener = null
        ByteArrayOutputStream antLoggingBuffer = null
        if (logger.isInfoEnabled() == false) {
            // in info logging, ant already outputs info level, so we see everything
            // but on errors or when debugging, we want to see info level messages
            // because junit4 emits jvm output with ant logging
            if (testLoggingConfig.outputMode == TestLoggingConfiguration.OutputMode.ALWAYS) {
                // we want all output, so just stream directly
                listener = new DefaultLogger(
                        errorPrintStream: System.err,
                        outputPrintStream: System.out,
                        messageOutputLevel: org.apache.tools.ant.Project.MSG_INFO)
            } else {
                // we want to buffer the info, and emit it if the test fails
                antLoggingBuffer = new ByteArrayOutputStream()
                PrintStream stream = new PrintStream(antLoggingBuffer, true, "UTF-8")
                listener = new DefaultLogger(
                        errorPrintStream: stream,
                        outputPrintStream: stream,
                        messageOutputLevel: org.apache.tools.ant.Project.MSG_INFO)
            }
            project.ant.project.addBuildListener(listener)
        }

        NamespaceBuilderSupport junit4 = NamespaceBuilder.newInstance(ant, 'junit4')
        try {
            junit4.junit4(attributes) {
                classpath {
                    pathElement(path: classpath.asPath)
                }
                if (enableAssertions) {
                    jvmarg(value: '-ea')
                }
                if (enableSystemAssertions) {
                    jvmarg(value: '-esa')
                }
                for (String arg : jvmArgs) {
                    jvmarg(value: arg)
                }
                if (argLine != null) {
                    jvmarg(line: argLine)
                }
                fileset(dir: testClassesDir) {
                    for (String includePattern : patternSet.getIncludes()) {
                        include(name: includePattern)
                    }
                    for (String excludePattern : patternSet.getExcludes()) {
                        exclude(name: excludePattern)
                    }
                }
                for (Map.Entry<String, Object> prop : systemProperties) {
                    sysproperty key: prop.getKey(), value: prop.getValue().toString()
                }
                makeListeners()
            }
        } catch (BuildException e) {
            if (antLoggingBuffer != null) {
                logger.error('JUnit4 test failed, ant output was:')
                logger.error(antLoggingBuffer.toString('UTF-8'))
            }
            if (haltOnFailure) {
                throw e;
            }
        }

        if (listener != null) {
            // remove the listener we added so other ant tasks dont have verbose logging!
            project.ant.project.removeBuildListener(listener)
        }
    }

    static class ListenersElement extends UnknownElement {
        AggregatedEventListener[] listeners

        ListenersElement() {
            super('listeners')
            setNamespace('junit4')
            setQName('listeners')
        }

        public void handleChildren(Object realThing, RuntimeConfigurable wrapper) {
            assert realThing instanceof ListenersList
            ListenersList list = (ListenersList)realThing

            for (AggregatedEventListener listener : listeners) {
                list.addConfigured(listener)
            }
        }
    }

    /**
     * Makes an ant xml element for 'listeners' just as AntBuilder would, except configuring
     * the element adds the already created children.
     */
    def makeListeners() {
        def context = ant.getAntXmlContext()
        def parentWrapper = context.currentWrapper()
        def parent = parentWrapper.getProxy()
        UnknownElement element = new ListenersElement(listeners: listenersConfig.listeners)
        element.setProject(context.getProject())
        element.setRealThing(logger)
        ((UnknownElement)parent).addChild(element)
        RuntimeConfigurable wrapper = new RuntimeConfigurable(element, element.getQName())
        parentWrapper.addChild(wrapper)
        return wrapper.getProxy()
    }
}