Skip to content

Commit

Permalink
ZIO 2.0.0-RC6 (#258)
Browse files Browse the repository at this point in the history
* ZIO 2.0.0-RC6

* mapError

* Fix
  • Loading branch information
vigoo authored May 19, 2022
1 parent 8e94275 commit 2ea531c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ usefulTasks := Seq(
UsefulTask("", "testOnly *.YourSpec -- -t \"YourLabel\"", "Only runs tests with matching term")
)

val zioVersion = "2.0.0-RC5"
val zioVersion = "2.0.0-RC6"

libraryDependencies ++= Seq(
"dev.zio" %% "zio" % zioVersion,
Expand Down
6 changes: 5 additions & 1 deletion src/main/scala/zio/process/ProcessStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ final case class ProcessStream(private val inputStream: InputStream) {
* Return the output of this process as a stream of lines (default encoding of UTF-8).
*/
def linesStream: ZStream[Any, CommandError, String] =
stream.via(ZPipeline.utf8Decode).via(ZPipeline.splitLines)
stream
.via(
ZPipeline.fromChannel(ZPipeline.utf8Decode.channel.mapError(CommandError.IOError.apply))
)
.via(ZPipeline.splitLines)

/**
* Return the output of this process as a chunked stream of bytes.
Expand Down
30 changes: 15 additions & 15 deletions src/test/scala/zio/process/CommandSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,33 @@ object CommandSpec extends ZIOProcessBaseSpec {
} yield assertTrue(lines == Chunk("1", "2", "3"))
},
test("stream lines of output") {
assertM(Command("echo", "-n", "1\n2\n3").linesStream.runCollect)(equalTo(Chunk("1", "2", "3")))
assertZIO(Command("echo", "-n", "1\n2\n3").linesStream.runCollect)(equalTo(Chunk("1", "2", "3")))
},
test("work with stream directly") {
val zio = Command("echo", "-n", "1\n2\n3").stream.via(ZPipeline.utf8Decode).via(ZPipeline.splitLines).runCollect

assertM(zio)(equalTo(Chunk("1", "2", "3")))
assertZIO(zio)(equalTo(Chunk("1", "2", "3")))
},
test("fail trying to run a command that doesn't exist") {
val zio = Command("some-invalid-command", "test").string

assertM(zio.exit)(fails(isSubtype[CommandError.ProgramNotFound](anything)))
assertZIO(zio.exit)(fails(isSubtype[CommandError.ProgramNotFound](anything)))
},
test("pass environment variables") {
val zio = Command("bash", "-c", "echo -n \"var = $VAR\"").env(Map("VAR" -> "value")).string

assertM(zio)(equalTo("var = value"))
assertZIO(zio)(equalTo("var = value"))
},
test("accept streaming stdin") {
val stream = Command("echo", "-n", "a", "b", "c").stream
val zio = Command("cat").stdin(ProcessInput.fromStream(stream, flushChunksEagerly = false)).string

assertM(zio)(equalTo("a b c"))
assertZIO(zio)(equalTo("a b c"))
},
test("accept string stdin") {
val zio = Command("cat").stdin(ProcessInput.fromUTF8String("piped in")).string

assertM(zio)(equalTo("piped in"))
assertZIO(zio)(equalTo("piped in"))
},
test("accept file stdin") {
for {
Expand All @@ -63,19 +63,19 @@ object CommandSpec extends ZIOProcessBaseSpec {
.stdin(ProcessInput.fromString("piped in", StandardCharsets.UTF_16))
.string(StandardCharsets.UTF_16)

assertM(zio)(equalTo("piped in"))
assertZIO(zio)(equalTo("piped in"))
},
test("set workingDirectory") {
val zio = Command("ls").workingDirectory(new File("src/main/scala/zio/process")).lines

assertM(zio)(contains("Command.scala"))
assertZIO(zio)(contains("Command.scala"))
},
test("be able to fallback to a different program using typed error channel") {
val zio = Command("custom-echo", "-n", "test").string.catchSome { case CommandError.ProgramNotFound(_) =>
Command("echo", "-n", "test").string
}

assertM(zio)(equalTo("test"))
assertZIO(zio)(equalTo("test"))
},
test("interrupt a process manually") {
val zio = for {
Expand All @@ -84,7 +84,7 @@ object CommandSpec extends ZIOProcessBaseSpec {
result <- fiber.join
} yield result

assertM(zio.exit)(isInterrupted)
assertZIO(zio.exit)(isInterrupted)
},
test("interrupt a process due to timeout") {
val zio = for {
Expand All @@ -95,7 +95,7 @@ object CommandSpec extends ZIOProcessBaseSpec {
result <- fiber.join
} yield result

assertM(zio)(isNone)
assertZIO(zio)(isNone)
} @@ TestAspect.ignore, // TODO: Until https://github.com/zio/zio/issues/3840 is fixed or there is a workaround
test("capture stdout and stderr separately") {
val zio = for {
Expand All @@ -104,22 +104,22 @@ object CommandSpec extends ZIOProcessBaseSpec {
stderr <- process.stderr.string
} yield (stdout, stderr)

assertM(zio)(equalTo(("stdout1\nstdout2\n", "stderr1\nstderr2\n")))
assertZIO(zio)(equalTo(("stdout1\nstdout2\n", "stderr1\nstderr2\n")))
},
test("return non-zero exit code in success channel") {
val zio = Command("ls", "--non-existent-flag").exitCode

assertM(zio)(not(equalTo(ExitCode.success)))
assertZIO(zio)(not(equalTo(ExitCode.success)))
},
test("absolve non-zero exit code") {
val zio = Command("ls", "--non-existent-flag").successfulExitCode

assertM(zio.exit)(fails(isSubtype[CommandError.NonZeroErrorCode](anything)))
assertZIO(zio.exit)(fails(isSubtype[CommandError.NonZeroErrorCode](anything)))
},
test("permission denied is a typed error") {
val zio = Command("src/test/bash/no-permissions.sh").string

assertM(zio.exit)(fails(isSubtype[CommandError.PermissionDenied](anything)))
assertZIO(zio.exit)(fails(isSubtype[CommandError.PermissionDenied](anything)))
},
test("redirectErrorStream should merge stderr into stdout") {
for {
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/zio/process/PipedCommandSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object PipedCommandSpec extends ZIOProcessBaseSpec {
test("support piping") {
val zio = (Command("echo", "2\n1\n3") | Command("cat") | Command("sort")).lines

assertM(zio)(equalTo(Chunk("1", "2", "3")))
assertZIO(zio)(equalTo(Chunk("1", "2", "3")))
},
test("piping is associative") {
for {
Expand All @@ -24,7 +24,7 @@ object PipedCommandSpec extends ZIOProcessBaseSpec {
.stdin(ProcessInput.fromUTF8String("2\n1\n3"))
.lines

assertM(zio)(equalTo(Chunk("1", "2")))
assertZIO(zio)(equalTo(Chunk("1", "2")))
},
test("env delegate to all commands") {
val env = Map("key" -> "value")
Expand Down

0 comments on commit 2ea531c

Please sign in to comment.