Scala Process - Capture Standard Out and Exit Code

后端 未结 6 2250
心在旅途
心在旅途 2020-12-13 06:21

I\'m working with the Scala scala.sys.process library.

I know that I can capture the exit code with ! and the output with !!

6条回答
  •  隐瞒了意图╮
    2020-12-13 06:58

    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.

提交回复
热议问题