-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathAbstractScalaTestMojo.java
670 lines (585 loc) · 21 KB
/
AbstractScalaTestMojo.java
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
package org.scalatest.tools.maven;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.cli.*;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import static java.util.Collections.singletonList;
import static java.util.Collections.unmodifiableList;
import static org.scalatest.tools.maven.MojoUtils.*;
/**
* Provides the base for all mojos.
*
* @author Jon-Anders Teigen
* @author Sean Griffin
* @author Mike Pilquist
* @author Bill Venners
*
* @requiresDependencyResolution test
*/
abstract class AbstractScalaTestMojo extends AbstractMojo {
/**
* Injected by Maven so that forked process can be
* launched from the working directory of current maven project in a multi-module build. Should not be user facing.
* @parameter default-value="${project}"
* @required
* @readonly
*/
MavenProject project;
/**
* Injected by Maven so that it can be included in the run path. Should not be user facing.
* @parameter property="project.build.testOutputDirectory"
* @required
* @readOnly
*/
File testOutputDirectory;
/**
* Injected by Maven so that it can be included in the run path. Should not be user facing.
* @parameter property="project.build.outputDirectory"
* @required
* @readOnly
*/
File outputDirectory;
/**
* Comma separated list of additional elements to be added
* to the ScalaTest runpath. <code>${project.build.outputDirectory}</code> and
* <code>${project.build.testOutputDirectory}</code> are included by default
* @parameter property="runpath"
*/
String runpath;
/**
* Comma separated list of suites to be executed.
* @parameter property="suites"
*/
String suites;
/**
* Comma separated list of tests to be executed
* @parameter property="tests"
*/
String tests;
/**
* Regex of suffixes to filter discovered suites
* @parameter property="suffixes"
*/
String suffixes;
/**
* Comma separated list of tags to include
* @parameter property="tagsToInclude"
*/
String tagsToInclude;
/**
* Comma separated list of tags to exclude
* @parameter property="tagsToExclude"
*/
String tagsToExclude;
/**
* Comma separated list of configuration parameters to pass to ScalaTest.
* The parameters must be of the format <key>=<value>. E.g <code>foo=bar,monkey=donkey</code>
* @parameter property="config"
*/
String config;
/**
* Set to true to run suites concurrently
* @parameter property="parallel"
*/
boolean parallel;
/**
* Set to true to enable suite sorting, this only takes effect if parallel is set to true.
* @parameter property="suiteSorting"
*/
boolean suiteSorting;
/**
* Set the thread count for parallel tests execution, this only takes effect if parallel is set to true.
* @parameter property="threadCount"
*/
int threadCount;
/**
* Comma separated list of packages containing suites to execute
* @parameter property="membersOnlySuites"
*/
String membersOnlySuites;
// TODO: Change this to wildcard and membersOnly
/**
* Comma separated list of wildcard suite names to execute
* @parameter property="wildcardSuites"
*/
String wildcardSuites;
/**
* Comma separated list of testNG xml files to execute
* @parameter property="testNGXMLFiles"
*/
String testNGConfigFiles;
/**
* Comma separated list of files to store names of failed and
* canceled tests into.
* @parameter property="memoryFiles"
*/
String memoryFiles;
/**
* Comma separated list of files to store names of failed and
* canceled tests into.
* @parameter property="testsFiles"
*/
String testsFiles;
/**
* Comma separated list of JUnit suites/tests to execute
* @parameter property="junitClasses"
*/
String jUnitClasses;
/**
* Option to specify the forking mode. Can be "never" or "once". "always", which would
* fork for each test-class, may be supported later.
*
* @parameter property="forkMode" default-value="once"
*/
String forkMode;
/**
* Option to specify additional JVM options to pass to the forked process.
*
* @parameter property="argLine"
*/
String argLine;
/**
* Additional environment variables to pass to the forked process.
*
* @parameter
*/
Map<String, String> environmentVariables;
/**
* Additional system properties to pass to the forked process.
*
* @parameter
*/
Map<String, String> systemProperties;
/**
* Option to specify whether the forked process should wait at startup for a remote debugger to attach.
*
* <p>If set to <code>true</code>, the forked process will suspend at startup and wait for a remote
* debugger to attach to the configured port.</p>
*
* @parameter property="debugForkedProcess" default-value="false"
*/
boolean debugForkedProcess;
/**
* JVM options to pass to the forked process when <code>debugForkedProcess</code> is true.
*
* <p>If set to a non-empty value, the standard debug arguments are replaced by the specified arguments.
* This allows customization of how remote debugging is done, without having to reconfigure the JVM
* options in <code>argLine</code>.
*
* @parameter property="debugArgLine"
*/
String debugArgLine;
/**
* Port to listen on when debugging the forked process.
*
* @parameter property="debuggerPort" default-value="5005"
*/
int debuggerPort = 5005;
/**
* Timeout in seconds to allow the forked process to run before killing it and failing the test run.
*
* <p>If set to 0, process never times out.</p>
*
* @parameter property="timeout" default-value="0"
*/
int forkedProcessTimeoutInSeconds = 0;
/**
* Whether or not to log the command used to launch the forked process.
*
* @parameter property="logForkedProcessCommand" default-value="false"
*/
boolean logForkedProcessCommand;
/**
* Span scale factor.
*
* @parameter property="spanScaleFactor" default-value="1.0"
*/
double spanScaleFactor = 1.0;
/**
* The current working directory for forked process. Optional. If not specified, basedir will be used.
*
* @parameter property="workingDirectory"
*/
String workingDirectory;
/**
* Option to specify an alternative path to JVM (or path to the java executable) to use with
* the forked process.
*
* @parameter property="jvm"
*/
String jvm;
// runScalaTest is called by the concrete mojo subclasses TODO: make it protected and others too
// Returns true if all tests pass
boolean runScalaTest(String[] args) throws MojoFailureException {
getLog().debug(Arrays.toString(args));
if (forkMode.equals("never")) {
return runWithoutForking(args);
}
else {
if (!forkMode.equals("once")) {
getLog().error("Invalid forkMode: \"" + forkMode + "\"; Using once instead.");
}
return runForkingOnce(args);
}
}
// Returns true if all tests pass
private boolean runWithoutForking(String[] args) {
try {
return (Boolean) run().invoke(null, new Object[]{args});
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
} catch (InvocationTargetException e) {
Throwable target = e.getTargetException();
if(target instanceof RuntimeException){
throw (RuntimeException)target;
} else {
throw new IllegalArgumentException(target);
}
}
}
// Returns true if all tests pass
private boolean runForkingOnce(String[] args) throws MojoFailureException {
final Commandline cli = new Commandline();
if ((this.workingDirectory == null || this.workingDirectory.isEmpty())) {
cli.setWorkingDirectory(project.getBasedir());
} else {
cli.setWorkingDirectory(workingDirectory);
}
if (this.jvm == null || this.jvm.isEmpty()) {
cli.setExecutable(getJvm());
} else {
cli.setExecutable(this.jvm);
}
// Set up environment
if (environmentVariables != null) {
for (final Map.Entry<String, String> entry : environmentVariables.entrySet()) {
cli.addEnvironment(entry.getKey(), entry.getValue());
}
}
cli.addEnvironment("CLASSPATH", buildClassPathEnvironment());
// Set up system properties
if (systemProperties != null) {
for (final Map.Entry<String, String> entry : systemProperties.entrySet()) {
cli.createArg().setValue(String.format("-D%s=%s", entry.getKey(), entry.getValue()));
}
}
cli.createArg().setValue(String.format("-Dbasedir=%s", project.getBasedir().getAbsolutePath()));
// Set user specified JVM arguments
if (argLine != null) {
cli.createArg().setLine(stripNewLines(argLine));
}
// Set debugging JVM arguments if debugging is enabled
if (debugForkedProcess) {
cli.createArg().setLine(stripNewLines(forkedProcessDebuggingArguments()));
}
// Set ScalaTest arguments
cli.createArg().setValue("org.scalatest.tools.Runner");
for (final String arg : args) {
cli.createArg().setValue(arg);
}
// Log command string
final String commandLogStatement = "Forking ScalaTest via: " + cli;
if (logForkedProcessCommand) {
getLog().info(commandLogStatement);
} else {
getLog().debug(commandLogStatement);
}
try (
final Writer outputWriter = getOutputWriter()
) {
final StreamConsumer outputConsumer = new WriterStreamConsumer(outputWriter);
final int result = CommandLineUtils.executeCommandLine(cli, outputConsumer, outputConsumer,
forkedProcessTimeoutInSeconds);
return result == 0;
}
catch (final CommandLineTimeOutException e) {
throw new MojoFailureException(String.format("Timed out after %d seconds waiting for forked process to complete.", forkedProcessTimeoutInSeconds), e);
}
catch (final CommandLineException e) {
throw new MojoFailureException("Exception while executing forked process.", e);
}
catch (IOException e) {
throw new MojoFailureException("Unable to close the output writer ", e);
}
}
protected Writer getOutputWriter() throws MojoFailureException {
return new PrintWriter(System.out) {
@Override
public void close() {
out = null; // System.out should stay open
}
};
}
private String buildClassPathEnvironment() {
StringBuffer buf = new StringBuffer();
boolean first = true;
for (String e : testClasspathElements()) {
if (first) {
first = false;
}
else {
buf.append(File.pathSeparator);
}
buf.append(e);
}
return buf.toString();
}
private String forkedProcessDebuggingArguments() {
if (debugArgLine == null) {
return String.format("-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=%s", debuggerPort);
} else {
return debugArgLine;
}
}
protected boolean isScalaTestAvailable() {
try {
classLoader().loadClass("org.scalatest.tools.Runner");
return true;
}
catch (ClassNotFoundException e) {
return false;
}
}
// This is just used by runScalaTest to get the method to invoke
private Method run() {
try {
Class<?> runner = classLoader().loadClass("org.scalatest.tools.Runner");
return runner.getMethod("run", String[].class);
} catch (NoSuchMethodException e) {
throw new IllegalStateException(e);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("scalatest is missing from classpath");
}
}
// This is just used by run to get a class loader from which to load ScalaTest
private ClassLoader classLoader() {
try {
List<URL> urls = new ArrayList<URL>();
for (String element : testClasspathElements()) {
File file = new File(element);
if (file.isFile()) {
urls.add(file.toURI().toURL());
}
}
URL[] u = urls.toArray(new URL[urls.size()]);
return new URLClassLoader(u);
} catch (MalformedURLException e) {
throw new IllegalStateException(e);
}
}
// Have to use the programmatic way of getting the classpath elements
// instead of the field-level injection since that apparently doesn't work
// for ReporterMojos in maven-2.2 (it does work in maven-3)
private List<String> testClasspathElements() {
try {
return (List<String>) project.getTestClasspathElements();
}
catch (DependencyResolutionRequiredException e) {
// There's really no known way this exception can happen since
// the @requiresDependencyResolution at the top of the class
// defines test-scoped resolution.
throw new IllegalStateException("Dependency resolution should be test-scoped.", e);
}
}
// This is the configuration parameters shared by all concrete Mojo subclasses
List<String> sharedConfiguration() {
return unmodifiableList(
new ArrayList<String>() {{
addAll(runpath());
addAll(config());
addAll(tagsToInclude());
addAll(tagsToExclude());
addAll(parallel());
addAll(tests());
addAll(suites());
addAll(suffixes());
addAll(membersOnlySuites());
addAll(wildcardSuites());
addAll(testNGConfigFiles());
addAll(memoryFiles());
addAll(testsFiles());
addAll(junitClasses());
addAll(spanScaleFactor());
}});
}
private List<String> config() {
List<String> c = new ArrayList<String>();
for(String pair : splitOnComma(config)){
c.add("-D"+pair);
}
return unmodifiableList(c);
}
private List<String> runpath() {
checkRunpathArgument("Output", outputDirectory);
checkRunpathArgument("Test output", testOutputDirectory);
String outputPath = outputDirectory.getAbsolutePath();
if(outputPath.contains(" ")) {
outputPath = outputPath.replaceAll(" ","\\\\ ");
getLog().debug(String.format("Escaped output directory path: %s", outputPath));
}
String testOutputPath = testOutputDirectory.getAbsolutePath();
if(testOutputPath.contains(" ")) {
testOutputPath = testOutputPath.replaceAll(" ","\\\\ ");
getLog().debug(String.format("Escaped test output directory path: %s", testOutputPath));
}
return compoundArg("-R",
outputPath,
testOutputPath,
runpath);
}
private void checkRunpathArgument(String directoryName, File directory) {
if(!directory.exists()) {
getLog().warn(String.format("%s directory does not exist: %s", directoryName, directory.getAbsolutePath()));
} else if(!directory.isDirectory()) {
getLog().warn(String.format("%s is not a directory: %s", directoryName, directory.getAbsolutePath()));
} else if(!directory.canRead()) {
getLog().warn(String.format("%s directory is not readable: %s", directoryName, directory.getAbsolutePath()));
}
}
private List<String> tagsToInclude() {
return compoundArg("-n", tagsToInclude);
}
private List<String> tagsToExclude() {
return compoundArg("-l", tagsToExclude);
}
private List<String> parallel() {
if (parallel) {
String useSuiteSorting = suiteSorting ? "S" : "";
String useThreadCount = threadCount == 0 ? "" : ("" + threadCount);
return unmodifiableList(singletonList("-P" + useSuiteSorting + useThreadCount));
}
else
return Collections.<String>emptyList();
}
//
// Generates a -s argument for each suite in comma-delimited list
// 'suites', with optionally a -z or -t argument for a test name
// if one follows the suite name.
//
// Test names follow suite names after whitespace, and may be prefixed
// by an '@' sign to indicate they are an exact test name instead of
// a substring. A -t argument is used for tests preceded by an '@'
// sign, and -z is used for others.
//
private List<String> suites() {
List<String> list = new ArrayList<String>();
for (String suite: splitOnComma(suites)) {
SuiteTestPair pair = new SuiteTestPair(suite);
if (pair.suite != null) {
list.add("-s");
list.add(pair.suite);
if (pair.test != null) {
addTest(list, pair.test);
}
}
}
return unmodifiableList(list);
}
//
// Parses a string containing a Suite name followed
// optionally by a test name.
//
// E.g. "HelloSuite hello there" would produce suite "HelloSuite"
// and test "hello there".
//
static private class SuiteTestPair {
String suite;
String test;
SuiteTestPair(String str) {
if (str != null) {
String trimStr = str.trim();
if (trimStr.length() > 0) {
String[] splits = trimStr.split("(?s)\\s", 2);
if (splits.length > 1) {
suite = splits[0];
test = splits[1].trim();
}
else {
suite = trimStr;
}
}
}
}
}
//
// Adds a -t or -z arg for specified test name. Uses -t if name is
// prefixed by an '@' sign, or -z otherwise.
//
private void addTest(List list, String testParm) {
if (testParm != null) {
String test = testParm.trim();
if (test.length() > 0) {
if (test.charAt(0) == '@') {
String atTest = test.substring(1).trim();
if (atTest.length() > 0) {
list.add("-t");
list.add(atTest);
}
}
else {
list.add("-z");
list.add(test);
}
}
}
}
//
// Generates a -z or -t argument for each name in comma-delimited
// 'tests' list, with -t used for those names prefixed by '@'.
//
private List<String> tests() {
List<String> list = new ArrayList<String>();
for (String test: splitOnComma(tests)) {
addTest(list, test);
}
return unmodifiableList(list);
}
private List<String> spanScaleFactor() {
List<String> list = new ArrayList<String>();
if (spanScaleFactor != 1.0) {
list.add("-F");
list.add(spanScaleFactor + "");
}
return unmodifiableList(list);
}
private List<String> suffixes() {
return compoundArg("-q", suffixes);
}
private List<String> membersOnlySuites() {
return suiteArg("-m", membersOnlySuites);
}
private List<String> wildcardSuites() {
return suiteArg("-w", wildcardSuites);
}
private List<String> testNGConfigFiles() {
return suiteArg("-b", testNGConfigFiles);
}
private List<String> memoryFiles() {
return suiteArg("-M", memoryFiles);
}
private List<String> testsFiles() {
List<String> list = new ArrayList<String>();
for (String param : splitOnComma(testsFiles)) {
File file = new File(param);
if (file.exists()) {
list.add("-A");
list.add(param);
}
}
return unmodifiableList(list);
}
private List<String> junitClasses() {
return suiteArg("-j", jUnitClasses);
}
}