Skip to content

Commit

Permalink
Do not append line separator on empty stream.
Browse files Browse the repository at this point in the history
  • Loading branch information
zainab-ali committed Dec 7, 2023
1 parent 43572fd commit 9f9fc1e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
9 changes: 7 additions & 2 deletions io/shared/src/main/scala/fs2/io/file/Files.scala
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,13 @@ sealed trait Files[F[_]] extends FilesPlatform[F] {
* using the specified flags to open the file.
*/
def writeUtf8Lines(path: Path, flags: Flags): Pipe[F, String, Nothing] = in =>
in.intersperse(lineSeparator)
.append(Stream[F, String](lineSeparator))
in.pull.uncons
.flatMap {
case Some(_) =>
in.intersperse(lineSeparator).append(Stream[F, String](lineSeparator)).underlying
case None => Pull.done
}
.stream
.through(writeUtf8(path, flags))
}

Expand Down
14 changes: 14 additions & 0 deletions io/shared/src/test/scala/fs2/io/file/FilesSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ class FilesSuite extends Fs2IoSuite with BaseFileSuite {
|bar
|""".stripMargin)
}

test("writeUtf8Lines - empty stream") {
Stream
.resource(tempFile)
.flatMap { path =>
Stream.empty
.covary[IO]
.through(Files[IO].writeUtf8Lines(path)) ++ Files[IO]
.readUtf8(path)
}
.compile
.foldMonoid
.assertEquals("")
}
}

group("tail") {
Expand Down

0 comments on commit 9f9fc1e

Please sign in to comment.