]> Dogcows Code - chaz/sbt-tap/blobdiff - src/main/scala/SbtTapReporting.scala
flush the output
[chaz/sbt-tap] / src / main / scala / SbtTapReporting.scala
index 20d8b7c9d1cedb479bd825fd5e9a4cb0652ae65a..e828c2d6b977a360c0f53c6ff52c09612d3c5f5a 100644 (file)
@@ -1,11 +1,11 @@
+import java.io.{PrintWriter, StringWriter, File, FileWriter}
 import sbt._
 import org.scalatools.testing.{Event => TEvent, Result => TResult}
 
 import java.util.concurrent.atomic.AtomicInteger
-import java.io.{File, FileWriter}
 
 object SbtTapReporting extends Plugin {
-  lazy val tapListener = new SbtTapListener
+  def apply() = new SbtTapListener
 }
 
 /**
@@ -21,10 +21,11 @@ class SbtTapListener extends TestsListener {
   var fileWriter: FileWriter = _
 
   override def doInit {
-    println("doInit called in sbt tap plugin")
     new File("test-results").mkdirs()
 
-    fileWriter = new FileWriter("test-results/test.tap")
+    fileWriter = new FileWriter(
+      scala.util.Properties.envOrElse("SBT_TAP_OUTPUT", "test-results/test.tap")
+    )
   }
 
   def startGroup(name: String) {}
@@ -35,10 +36,13 @@ class SbtTapListener extends TestsListener {
         case TResult.Success => writeTapFields("ok", testId.incrementAndGet(), "-", e.testName())
         case TResult.Error | TResult.Failure =>
           writeTapFields("not ok", testId.incrementAndGet(), "-", e.testName())
-          // TODO: for exceptions, write stack trace to tap file.
+          // According to the TAP spec, as long as there is any kind of whitespace, this output should belong to the
+          // the test that failed and it should get displayed in the UI.
+          // TODO:It would be nice if we could report the exact line in the test where this happened.
+          writeTapFields(" ", stackTraceForError(e.error()))
         case TResult.Skipped =>
           // it doesn't look like this framework distinguishes between pending and ignored.
-          writeTapFields("ok", testId.incrementAndGet(), "#", "skip", e.testName())
+          writeTapFields("ok", testId.incrementAndGet(), e.testName(), "#", "skip", e.testName())
       }
     }
   }
@@ -48,8 +52,17 @@ class SbtTapListener extends TestsListener {
     fileWriter.close()
   }
 
-  private def writeTapFields(s: Any*) { fileWriter.write(s.mkString("",  " ", "\n")) }
+  private def writeTapFields(s: Any*) {
+    fileWriter.write(s.mkString("",  " ", "\n"))
+    fileWriter.flush()
+  }
 
+  private def stackTraceForError(t: Throwable): String = {
+    val sw = new StringWriter()
+    val printWriter = new PrintWriter(sw)
+    t.printStackTrace(printWriter)
+    sw.toString
+  }
   def endGroup(name: String, t: Throwable) { }
 
   def endGroup(name: String, result: TestResult.Value) { }
This page took 0.022777 seconds and 4 git commands to generate.