Scala Process - Capture Standard Out and Exit Code

scala

Solution

You can use `ProcessIO`. I needed something like that in a Specs2 Test, where I had to check the exit value as well as the output of a process depending on the input on `stdin` (`in` and `out` are of type `String`):

"the operation" should {
  f"return '$out' on input '$in'" in {
    var res = ""
    val io = new ProcessIO(
      stdin  => { stdin.write(in.getBytes)
                  stdin.close() }, 
      stdout => { res = convertStreamToString(stdout)
                  stdout.close() },
      stderr => { stderr.close() })
    val proc = f"$operation $file".run(io)
    proc.exitValue() must be_==(0)
    res must be_==(out)
  }
}

I figured that might help you. In the example I am ignoring what ever comes from `stderr`.

Problem

I'm working with the Scala `scala.sys.process` library. I know that I can capture the exit code with `!` and the output with `!!` but what if I want to capture both? I've seen this answer https://stackoverflow.com/a/6013932/416338 which looks promising, but I'm wondering if there is a one liner and I'm missing something.

Original source

Related problems