aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/scala/org/perl8/test/ExternalTest.scala
blob: 89d7bbcaa842722af81214c5d8567668095bc42f (plain) (blame)
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
package org.perl8.test

import scala.concurrent.Await
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.Future._
import scala.annotation.tailrec

class ExternalTest (cmdLine: String*) extends Test {
  def runTests (raw: Boolean): Int = {
    val processBuilder = new ProcessBuilder(cmdLine: _*)

    // Ensure that if stdout and stderr are both pointing to the same place (a
    // terminal or file or something) that they remain synchronized. This is
    // only possible if the endpoint they are streaming to is the same place
    // underneath, with no extra processing in between. (This is safe because
    // stdout is typically line-buffered, and we only ever output a line at a
    // time when writing TAP, so in theory, buffering of the underlying file
    // descriptor shouldn't make a difference here)
    if (Console.out eq System.out) {
      processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT)
    }
    if (Console.err eq System.err) {
      processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT)
    }

    val process = processBuilder.start

    val streams = Seq(
      Console.out -> process.getInputStream,
      Console.err -> process.getErrorStream
    )

    val listeners = streams.map { case (out, in) =>
      Future {
        val buf = new Array[Byte](1024)

        @tailrec
        def read {
          val bytes = in.read(buf)
          if (bytes >= 0) {
            out.print(new String(buf.take(bytes)))
            read
          }
        }

        read
        true
      }
    }

    val exitCode = process.waitFor
    Await.ready(Future.sequence(listeners), Duration.Inf)
    exitCode
  }
}