-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathJUnitXmlReporter.scala
522 lines (462 loc) · 16.6 KB
/
JUnitXmlReporter.scala
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
/*
* Copyright 2001-2013 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.bazel.rules.scala
import org.scalatest._
import org.scalatest.events._
import java.io.{ PrintWriter, StringWriter }
import java.net.InetAddress
import java.net.UnknownHostException
import java.text.SimpleDateFormat
import java.util.Properties
import scala.collection.mutable.ListBuffer
import scala.xml
/**
* A <code>Reporter</code> that writes test status information in XML format
* using the same format as is generated by the xml formatting option of the
* ant <junit> task.
*
* A separate file is written for each test suite, named TEST-[classname].xml,
* to the directory specified.
*
* @exception IOException if unable to open the file for writing
*
* @author George Berger
* @author P. Oscar Boykin (modifications for bazel)
*/
class JUnitXmlReporter extends Reporter {
private var events = Set.empty[Event]
private var testSuites = Set.empty[Testsuite]
private val propertiesXml = genPropertiesXml
//
// Records events in 'events' set. Generates xml from events upon receipt
// of SuiteCompleted or SuiteAborted events.
//
def apply(event: Event): Unit = {
events += event
event match {
case e: SuiteCompleted =>
testSuites += getTestsuite(e, e.suiteId)
case e: SuiteAborted =>
testSuites += getTestsuite(e, e.suiteId)
case _: RunCompleted =>
write(testSuites)
testSuites = Set.empty
case _: RunStopped =>
write(testSuites)
testSuites = Set.empty
case _: RunAborted =>
write(testSuites)
testSuites = Set.empty
case _ => ()
}
}
//
// Writes the xml file for a single test suite. Removes processed
// events from the events Set as they are used.
//
private def write(suites: Set[Testsuite]): Unit =
Option(System.getenv.get("XML_OUTPUT_FILE"))
.foreach { filespec =>
val out = new PrintWriter(filespec, "UTF-8")
try {
val xml = xmlify(suites.toList.sortBy(_.name))
out.print(prettyXml(xml))
}
finally {
out.close()
}
}
//
// Constructs a Testsuite object corresponding to a specified
// SuiteCompleted or SuiteAborted event.
//
// Scans events reported so far and builds the Testsuite from events
// associated with the specified suite. Removes events from
// the class's events Set as they are consumed.
//
// Only looks at events that have the same ordinal prefix as the
// end event being processed (where an event's ordinal prefix is its
// ordinal list with last element removed). Events with the same
// prefix get processed sequentially, so filtering this way eliminates
// events from any nested suites being processed concurrently
// that have not yet completed when the parent's SuiteCompleted or
// SuiteAborted event is processed.
//
private def getTestsuite(endEvent: Event, suiteId: String): Testsuite = {
require(endEvent.isInstanceOf[SuiteCompleted] ||
endEvent.isInstanceOf[SuiteAborted])
val orderedEvents = events.toList.filter { e =>
e match {
case e: TestStarting => e.suiteId == suiteId
case e: TestSucceeded => e.suiteId == suiteId
case e: TestIgnored => e.suiteId == suiteId
case e: TestFailed => e.suiteId == suiteId
case e: TestPending => e.suiteId == suiteId
case e: TestCanceled => e.suiteId == suiteId
case e: InfoProvided =>
e.nameInfo match {
case Some(nameInfo) =>
nameInfo.suiteId == suiteId
case None => false
}
case e: AlertProvided =>
e.nameInfo match {
case Some(nameInfo) =>
nameInfo.suiteId == suiteId
case None => false
}
case e: NoteProvided =>
e.nameInfo match {
case Some(nameInfo) =>
nameInfo.suiteId == suiteId
case None => false
}
case e: MarkupProvided =>
e.nameInfo match {
case Some(nameInfo) =>
nameInfo.suiteId == suiteId
case None => false
}
case e: ScopeOpened => e.nameInfo.suiteId == suiteId
case e: ScopeClosed => e.nameInfo.suiteId == suiteId
case e: SuiteStarting => e.suiteId == suiteId
case e: SuiteAborted => e.suiteId == suiteId
case e: SuiteCompleted => e.suiteId == suiteId
case _ => false
}
}.sortWith((a, b) => a < b).toArray
val (startIndex, endIndex) = locateSuite(orderedEvents, endEvent)
val startEvent = orderedEvents(startIndex).asInstanceOf[SuiteStarting]
events -= startEvent
val name =
startEvent.suiteClassName match {
case Some(className) => className
case None => startEvent.suiteName
}
val testsuite = Testsuite(name, startEvent.timeStamp)
var idx = startIndex + 1
while (idx <= endIndex) {
val event = orderedEvents(idx)
events -= event
event match {
case e: TestStarting =>
val (testEndIndex, testcase) = processTest(orderedEvents, e, idx)
testsuite.testcases += testcase
if (testcase.failure != None) testsuite.failures += 1
idx = testEndIndex + 1
case e: SuiteAborted =>
assert(endIndex == idx)
testsuite.errors += 1
testsuite.time = e.timeStamp - testsuite.timeStamp
idx += 1
case e: SuiteCompleted =>
assert(endIndex == idx)
testsuite.time = e.timeStamp - testsuite.timeStamp
idx += 1
case e: TestIgnored =>
val testcase = Testcase(e.testName, e.suiteClassName, e.timeStamp)
testcase.ignored = true
testsuite.testcases += testcase
idx += 1
case _: InfoProvided => idx += 1
case _: AlertProvided => idx += 1
case _: NoteProvided => idx += 1
case _: MarkupProvided => idx += 1
case _: ScopeOpened => idx += 1
case _: ScopeClosed => idx += 1
case _: ScopePending => idx += 1
case e: TestPending => unexpected(e)
case e: TestCanceled => unexpected(e)
case e: RunStarting => unexpected(e)
case e: RunCompleted => unexpected(e)
case e: RunStopped => unexpected(e)
case e: RunAborted => unexpected(e)
case e: TestSucceeded => unexpected(e)
case e: TestFailed => unexpected(e)
case e: SuiteStarting => unexpected(e)
case e: DiscoveryStarting => unexpected(e)
case e: DiscoveryCompleted => unexpected(e)
}
}
testsuite
}
//
// Finds the indexes for the SuiteStarted and SuiteCompleted or
// SuiteAborted endpoints of a test suite within an ordered array of
// events, given the terminating SuiteCompleted or SuiteAborted event.
//
// Searches sequentially through the array to find the specified
// SuiteCompleted event and its preceding SuiteStarting event.
//
// (The orderedEvents array does not contain any SuiteStarting events
// from nested suites running concurrently because of the ordinal-prefix
// filtering performed in getTestsuite(). It does not contain any from
// nested suites running sequentially because those get removed when they
// are processed upon occurrence of their corresponding SuiteCompleted
// events.)
//
private def locateSuite(orderedEvents: Array[Event],
endEvent: Event):
(Int, Int) = {
require(orderedEvents.size > 0)
require(endEvent.isInstanceOf[SuiteCompleted] ||
endEvent.isInstanceOf[SuiteAborted])
var startIndex = 0
var endIndex = 0
var idx = 0
while ((idx < orderedEvents.size) && (endIndex == 0)) {
val event = orderedEvents(idx)
event match {
case _: SuiteStarting =>
startIndex = idx
case e: SuiteCompleted =>
if (event == endEvent) {
endIndex = idx
assert(
e.suiteName ==
orderedEvents(startIndex).asInstanceOf[SuiteStarting].
suiteName)
}
case e: SuiteAborted =>
if (event == endEvent) {
endIndex = idx
assert(
e.suiteName ==
orderedEvents(startIndex).asInstanceOf[SuiteStarting].
suiteName)
}
case _ =>
}
idx += 1
}
assert(endIndex > 0)
assert(orderedEvents(startIndex).isInstanceOf[SuiteStarting])
(startIndex, endIndex)
}
private def idxAdjustmentForRecordedEvents(recordedEvents: collection.immutable.IndexedSeq[RecordableEvent]) =
recordedEvents.filter(e => e.isInstanceOf[InfoProvided] || e.isInstanceOf[MarkupProvided]).size
//
// Constructs a Testcase object from events in orderedEvents array.
//
// Accepts a TestStarting event and its index within orderedEvents.
// Returns a Testcase object plus the index to its corresponding
// test completion event. Removes events from class's events Set
// as they are processed.
//
private def processTest(orderedEvents: Array[Event],
startEvent: TestStarting, startIndex: Int):
(Int, Testcase) = {
val testcase = Testcase(startEvent.testName, startEvent.suiteClassName,
startEvent.timeStamp)
var endIndex = 0
var idx = startIndex + 1
while ((idx < orderedEvents.size) && (endIndex == 0)) {
val event = orderedEvents(idx)
events -= event
event match {
case e: TestSucceeded =>
endIndex = idx
testcase.time = e.timeStamp - testcase.timeStamp
idx += idxAdjustmentForRecordedEvents(e.recordedEvents)
case e: TestFailed =>
endIndex = idx
testcase.failure = Some(e)
testcase.time = e.timeStamp - testcase.timeStamp
idx += idxAdjustmentForRecordedEvents(e.recordedEvents)
case e: TestPending =>
endIndex = idx
testcase.pending = true
idx += idxAdjustmentForRecordedEvents(e.recordedEvents)
case e: TestCanceled =>
endIndex = idx
testcase.canceled = true
idx += idxAdjustmentForRecordedEvents(e.recordedEvents)
case _: ScopeOpened => idx += 1
case _: ScopeClosed => idx += 1
case _: ScopePending => idx += 1
case _: InfoProvided => idx += 1
case _: MarkupProvided => idx += 1
case _: AlertProvided => idx += 1
case _: NoteProvided => idx += 1
case e: SuiteCompleted => unexpected(e)
case e: TestStarting => unexpected(e)
case e: TestIgnored => unexpected(e)
case e: SuiteStarting => unexpected(e)
case e: RunStarting => unexpected(e)
case e: RunCompleted => unexpected(e)
case e: RunStopped => unexpected(e)
case e: RunAborted => unexpected(e)
case e: SuiteAborted => unexpected(e)
case e: DiscoveryStarting => unexpected(e)
case e: DiscoveryCompleted => unexpected(e)
}
}
(endIndex, testcase)
}
def prettyXml(xmlVal: scala.xml.Elem): String = {
val prettified = (new xml.PrettyPrinter(76, 2)).format(xmlVal)
// scala xml strips out the <![CDATA[]]> elements, so restore them here
val withCDATA =
prettified.
replace("<system-out></system-out>",
"<system-out><![CDATA[]]></system-out>").
replace("<system-err></system-err>",
"<system-err><![CDATA[]]></system-err>")
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + withCDATA
}
private def xmlify(suites: Iterable[Testsuite]): scala.xml.Elem =
<testsuites> { suites.map(xmlify) } </testsuites>
private def xmlify(testsuite: Testsuite): scala.xml.Elem =
<testsuite
errors = { "" + testsuite.errors }
failures = { "" + testsuite.failures }
hostname = { "" + hostname }
name = { "" + testsuite.name }
tests = { "" + testsuite.testcases.size }
time = { "" + testsuite.time / 1000.0 }
timestamp = { "" + formatTimeStamp(testsuite.timeStamp) }>
{ propertiesXml }
{
for (testcase <- testsuite.testcases) yield {
<testcase
name = { "" + testcase.name }
classname = { "" + strVal(testcase.className) }
time = { "" + testcase.time / 1000.0 }
>
{
if (testcase.ignored || testcase.pending || testcase.canceled)
<skipped/>
else
failureXml(testcase.failure)
}
</testcase>
}
}
<system-out><![CDATA[]]></system-out>
<system-err><![CDATA[]]></system-err>
</testsuite>
//
// Returns string representation of stack trace for specified Throwable,
// including any nested exceptions.
//
def getStackTrace(throwable: Throwable): String = {
val stringWriter = new StringWriter
val printWriter = new PrintWriter(stringWriter)
throwable.printStackTrace(printWriter)
printWriter.flush()
stringWriter.toString
}
//
// Generates <failure> xml for TestFailed event, if specified Option
// contains one.
//
private def failureXml(failureOption: Option[TestFailed]): xml.NodeSeq = {
failureOption match {
case None =>
xml.NodeSeq.Empty
case Some(failure) =>
val (throwableType, throwableText) =
failure.throwable match {
case None => ("", "")
case Some(throwable) =>
val throwableType = "" + throwable.getClass
val throwableText = getStackTrace(throwable)
(throwableType, throwableText)
}
<failure message = { failure.message.replaceAll("\n", "
") }
type = { throwableType } >
{ throwableText }
</failure>
}
}
//
// Returns toString value of option contents if Some, or empty string if
// None.
//
private def strVal(option: Option[Any]): String = {
option match {
case Some(x) => "" + x
case None => ""
}
}
//
// Determines hostname of local machine.
//
lazy val hostname: String =
try {
val localMachine = InetAddress.getLocalHost();
localMachine.getHostName
} catch {
case _: UnknownHostException => "unknown"
}
//
// Generates <properties> element of xml.
//
private def genPropertiesXml: xml.Elem = {
val sysprops = System.getProperties
<properties> {
for (name <- propertyNames(sysprops))
yield
<property name={ name } value = { sysprops.getProperty(name) }>
</property>
}
</properties>
}
//
// Returns a list of the names of properties in a Properties object.
//
private def propertyNames(props: Properties): List[String] = {
var listBuf = List[String]()
val enumeration = props.propertyNames
while (enumeration.hasMoreElements)
listBuf = enumeration.nextElement.toString :: listBuf
listBuf.reverse
}
//
// Formats timestamp into a string for display, e.g. "2009-08-31T14:59:37"
//
private def formatTimeStamp(timeStamp: Long): String = {
val dateFmt = new SimpleDateFormat("yyyy-MM-dd")
val timeFmt = new SimpleDateFormat("HH:mm:ss")
dateFmt.format(timeStamp) + "T" + timeFmt.format(timeStamp)
}
//
// Throws an exception if an unexpected Event is encountered.
//
def unexpected(event: Event): Unit = {
throw new RuntimeException("unexpected event [" + event + "]")
}
//
// Class to hold information about an execution of a test suite.
//
private case class Testsuite(name: String, timeStamp: Long) {
var errors = 0
var failures = 0
var time = 0L
val testcases = new ListBuffer[Testcase]
}
//
// Class to hold information about an execution of a testcase.
//
private case class Testcase(name: String, className: Option[String],
timeStamp: Long) {
var time = 0L
var pending = false
var canceled = false
var ignored = false
var failure: Option[TestFailed] = None
}
}