Skip to content

Commit

Permalink
Use Chunk instead of List (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
reibitto authored Aug 5, 2020
1 parent f5bfd13 commit 1a664a5
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/overview/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ flat-mapping and calling methods on `Process`, there are convenience methods on

### List of lines

To obtain the output as a list of lines with the type `ZIO[Blocking, CommandError, List[String]]`
To obtain the output as a list of lines with the type `ZIO[Blocking, CommandError, Chunk[String]]`

```scala mdoc:silent
command.lines
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/zio/process/Command.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ sealed trait Command {
/**
* Runs the command returning the output as a list of lines (default encoding of UTF-8).
*/
def lines: ZIO[Blocking, CommandError, List[String]] =
def lines: ZIO[Blocking, CommandError, Chunk[String]] =
run.flatMap(_.stdout.lines)

/**
* Runs the command returning the output as a list of lines with the specified encoding.
*/
def lines(charset: Charset): ZIO[Blocking, CommandError, List[String]] =
def lines(charset: Charset): ZIO[Blocking, CommandError, Chunk[String]] =
run.flatMap(_.stdout.lines(charset))

/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/scala/zio/process/ProcessStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import java.nio.charset.{ Charset, StandardCharsets }

import zio.blocking.{ effectBlockingCancelable, Blocking }
import zio.stream.{ ZStream, ZTransducer }
import zio.{ UIO, ZIO, ZManaged }
import zio.{ Chunk, UIO, ZIO, ZManaged }

import scala.collection.mutable.ArrayBuffer

Expand All @@ -29,12 +29,12 @@ final case class ProcessStream(private val inputStream: InputStream) {
/**
* Return the output of this process as a list of lines (default encoding of UTF-8).
*/
def lines: ZIO[Blocking, CommandError, List[String]] = lines(StandardCharsets.UTF_8)
def lines: ZIO[Blocking, CommandError, Chunk[String]] = lines(StandardCharsets.UTF_8)

/**
* Return the output of this process as a list of lines with the specified encoding.
*/
def lines(charset: Charset): ZIO[Blocking, CommandError, List[String]] =
def lines(charset: Charset): ZIO[Blocking, CommandError, Chunk[String]] =
ZManaged
.fromAutoCloseable(UIO(new BufferedReader(new InputStreamReader(inputStream, charset))))
.use { reader =>
Expand All @@ -46,7 +46,7 @@ final case class ProcessStream(private val inputStream: InputStream) {
lines.append(line)
}

lines.toList
Chunk.fromArray(lines.toArray)
}(UIO(reader.close()))
}
.refineOrDie {
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/zio/process/CommandSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object CommandSpec extends ZIOProcessBaseSpec {
testM("convert stdout to list of lines") {
val zio = Command("echo", "-n", "1\n2\n3").lines

assertM(zio)(equalTo(List("1", "2", "3")))
assertM(zio)(equalTo(Chunk("1", "2", "3")))
},
testM("stream lines of output") {
assertM(Command("echo", "-n", "1\n2\n3").linesStream.runCollect)(equalTo(Chunk("1", "2", "3")))
Expand Down
5 changes: 3 additions & 2 deletions src/test/scala/zio/process/PipedCommandSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zio.process

import java.io.File

import zio.Chunk
import zio.test.Assertion._
import zio.test._

Expand All @@ -10,7 +11,7 @@ object PipedCommandSpec extends ZIOProcessBaseSpec {
testM("support piping") {
val zio = (Command("echo", "2\n1\n3") | Command("cat") | Command("sort")).lines

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

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

0 comments on commit 1a664a5

Please sign in to comment.