]> Dogcows Code - chaz/sbt-tap/blob - src/main/scala/SbtTapReporting.scala
1eb1b6b3d8ad9480085e6b6f45b95ab054af4bb4
[chaz/sbt-tap] / src / main / scala / SbtTapReporting.scala
1 import java.io.{PrintWriter, StringWriter, File, FileWriter}
2 import sbt._
3 import org.scalatools.testing.{Event => TEvent, Result => TResult}
4
5 import java.util.concurrent.atomic.AtomicInteger
6
7 object SbtTapReporting extends Plugin {
8 def apply() = new SbtTapListener
9 }
10
11 /**
12 * Listens to sbt test listener events and writes them to a tap compatible file. Results for all groups
13 * go to a single file although it might be desirable to generate one tap file per group.
14 * <p>
15 * sbt runs tests in parallel and the protocol does not seem to provide a way to match a group to a test event. It
16 * does look line one thread calls startGroup/testEvent/endGroup sequentially and using thread local to keep
17 * the current active group might be one way to go.
18 */
19 class SbtTapListener extends TestsListener {
20 var testId = 0
21 var fileWriter: FileWriter = _
22
23 override def doInit = {
24 val filename = scala.util.Properties.envOrElse("SBT_TAP_OUTPUT", "test-results/test.tap")
25 val file = new File(filename)
26 new File(file.getParent).mkdirs
27 fileWriter = new FileWriter(file)
28 writeTap("TAP", "version", 13)
29 }
30
31 def startGroup(name: String) =
32 writeTapDiag("start", name)
33
34 def endGroup(name: String, result: TestResult.Value) =
35 writeTapDiag("end", name, "with result", result.toString.toLowerCase)
36
37 def endGroup(name: String, t: Throwable) = {
38 writeTapDiag("end", name)
39 writeTapDiag(stackTraceForError(t))
40 }
41
42 def testEvent(event: TestEvent) = this.synchronized {
43 event.detail.foreach { e: TEvent => testId += 1
44 var modified = false
45 val description = (if (e.testName.contains("#")) {
46 modified = true
47 e.testName.replaceAll("#", "")
48 } else e.testName).replaceAll("\n", "")
49 e.result match {
50 case TResult.Success =>
51 writeTap("ok", testId, "-", description)
52 case TResult.Skipped =>
53 writeTap("ok", testId, "-", description, "# SKIP")
54 case TResult.Error | TResult.Failure =>
55 writeTap("not ok", testId, "-", description)
56 // TODO: It would be nice if we could report the exact line in the test where this happened.
57 writeTapDiag(stackTraceForError(e.error))
58 }
59 if (modified) writeTapDiag("warning: hash character(s) removed from test " + testId + " description")
60 }
61 }
62
63 override def doComplete(finalResult: TestResult.Value) = {
64 writeTap("1.." + testId)
65 fileWriter.close
66 }
67
68 private def writeTap(s: Any*) = {
69 fileWriter.write(s.mkString("", " ", "\n"))
70 fileWriter.flush
71 }
72
73 private def writeTapDiag(s: Any*) =
74 writeTap("#", s.mkString("", " ", "\n").trim.replaceAll("\\n", "\n# "))
75
76 private def stackTraceForError(t: Throwable): String = {
77 val sw = new StringWriter
78 val printWriter = new PrintWriter(sw)
79 t.printStackTrace(printWriter)
80 sw.toString
81 }
82 }
This page took 0.03523 seconds and 3 git commands to generate.