From b39c67c5fc3db1e6e82fc8ea4bf57bd9429f0c08 Mon Sep 17 00:00:00 2001 From: Viktor Podzigun Date: Tue, 7 Jun 2022 18:57:22 +0200 Subject: [PATCH] Fixed handling of stderr when SubProcess.wrap --- core/src/main/scala/scommons/nodejs/util/SubProcess.scala | 2 +- .../src/test/scala/scommons/nodejs/ChildProcessSpec.scala | 3 +++ .../test/scala/scommons/nodejs/util/SubProcessSpec.scala | 6 ++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/core/src/main/scala/scommons/nodejs/util/SubProcess.scala b/core/src/main/scala/scommons/nodejs/util/SubProcess.scala index 08e21d7..bd06976 100644 --- a/core/src/main/scala/scommons/nodejs/util/SubProcess.scala +++ b/core/src/main/scala/scommons/nodejs/util/SubProcess.scala @@ -16,12 +16,12 @@ object SubProcess { def wrap(childProcess: raw.ChildProcess): Future[SubProcess] = { val stdout = new StreamReader(childProcess.stdout) + val stderr = new StreamReader(childProcess.stderr) val exitPromise = Promise[Unit]() childProcess.once("exit", { code: Int => if (code == 0) exitPromise.success(()) else { - val stderr = new StreamReader(childProcess.stderr) stderr.readNextBytes(8096).recover { case NonFatal(_) => None }.foreach { content => diff --git a/showcase/src/test/scala/scommons/nodejs/ChildProcessSpec.scala b/showcase/src/test/scala/scommons/nodejs/ChildProcessSpec.scala index e361365..9822547 100644 --- a/showcase/src/test/scala/scommons/nodejs/ChildProcessSpec.scala +++ b/showcase/src/test/scala/scommons/nodejs/ChildProcessSpec.scala @@ -2,6 +2,7 @@ package scommons.nodejs import org.scalatest.Succeeded import scommons.nodejs.ChildProcess._ +import scommons.nodejs.stream.Readable import scommons.nodejs.test.AsyncTestSpec import scommons.nodejs.util.StreamReader @@ -133,6 +134,7 @@ class ChildProcessSpec extends AsyncTestSpec { val onceMock = mockFunction[String, js.Function, raw.EventEmitter] val rawProcess = literal( "stdout" -> stdoutStream, + "stderr" -> Readable.from(Buffer.from("")), "once" -> onceMock ).asInstanceOf[raw.ChildProcess] @@ -201,6 +203,7 @@ class ChildProcessSpec extends AsyncTestSpec { val onceMock = mockFunction[String, js.Function, raw.EventEmitter] val rawProcess = literal( "stdout" -> stdoutStream, + "stderr" -> Readable.from(Buffer.from("")), "once" -> onceMock ).asInstanceOf[raw.ChildProcess] diff --git a/showcase/src/test/scala/scommons/nodejs/util/SubProcessSpec.scala b/showcase/src/test/scala/scommons/nodejs/util/SubProcessSpec.scala index ac6f7cd..a3ec3a8 100644 --- a/showcase/src/test/scala/scommons/nodejs/util/SubProcessSpec.scala +++ b/showcase/src/test/scala/scommons/nodejs/util/SubProcessSpec.scala @@ -17,6 +17,7 @@ class SubProcessSpec extends AsyncTestSpec { val onceMock = mockFunction[String, js.Function, raw.EventEmitter] val childProcess = literal( "stdout" -> stdoutStream, + "stderr" -> Readable.from(Buffer.from("")), "once" -> onceMock ).asInstanceOf[raw.ChildProcess] @@ -49,6 +50,7 @@ class SubProcessSpec extends AsyncTestSpec { val onceMock = mockFunction[String, js.Function, raw.EventEmitter] val childProcess = literal( "stdout" -> stdoutStream, + "stderr" -> Readable.from(Buffer.from("")), "once" -> onceMock ).asInstanceOf[raw.ChildProcess] @@ -166,7 +168,8 @@ class SubProcessSpec extends AsyncTestSpec { //given val expectedOutput = "test content" val stdoutStream = Readable.from(Buffer.from(expectedOutput)) - val stderrStream = Readable.from(Buffer.from("test error")) + val stderrStream = Readable.from(Buffer.from("")) + stderrStream.destroy(js.Error("test stream error")) val onceMock = mockFunction[String, js.Function, raw.EventEmitter] val childProcess = literal( "spawnargs" -> js.Array("app", "arg1", "arg2"), @@ -189,7 +192,6 @@ class SubProcessSpec extends AsyncTestSpec { (for { result <- resultF output <- loop(result.stdout, "") - _ = stderrStream.destroy(js.Error("test stream error")) _ = exitCallback(1) exitError <- result.exitF.failed } yield {