From 1724c3e200e432b8a2b4f105d55c401c6042cbee Mon Sep 17 00:00:00 2001 From: zainab-ali Date: Wed, 6 Dec 2023 16:08:53 +0000 Subject: [PATCH 1/3] Preserve chunking of input to `writeUtf8Lines` with `intersperse`. --- io/shared/src/main/scala/fs2/io/file/Files.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/io/shared/src/main/scala/fs2/io/file/Files.scala b/io/shared/src/main/scala/fs2/io/file/Files.scala index 09f25dd536..f0c2035927 100644 --- a/io/shared/src/main/scala/fs2/io/file/Files.scala +++ b/io/shared/src/main/scala/fs2/io/file/Files.scala @@ -443,7 +443,7 @@ 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.flatMap(s => Stream[F, String](s, lineSeparator)).through(writeUtf8(path, flags)) + in.intersperse(lineSeparator).through(writeUtf8(path, flags)) } private[fs2] trait FilesLowPriority { this: Files.type => From 43572fd472838bee752778e8c890c631a68e15b7 Mon Sep 17 00:00:00 2001 From: zainab-ali Date: Wed, 6 Dec 2023 16:29:40 +0000 Subject: [PATCH 2/3] Add an ending line separator. --- io/shared/src/main/scala/fs2/io/file/Files.scala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/io/shared/src/main/scala/fs2/io/file/Files.scala b/io/shared/src/main/scala/fs2/io/file/Files.scala index f0c2035927..a2136fa545 100644 --- a/io/shared/src/main/scala/fs2/io/file/Files.scala +++ b/io/shared/src/main/scala/fs2/io/file/Files.scala @@ -443,7 +443,9 @@ 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).through(writeUtf8(path, flags)) + in.intersperse(lineSeparator) + .append(Stream[F, String](lineSeparator)) + .through(writeUtf8(path, flags)) } private[fs2] trait FilesLowPriority { this: Files.type => From 9f9fc1efad9f8db0a79312e447b9291429c9975f Mon Sep 17 00:00:00 2001 From: zainab-ali Date: Thu, 7 Dec 2023 10:17:51 +0000 Subject: [PATCH 3/3] Do not append line separator on empty stream. --- io/shared/src/main/scala/fs2/io/file/Files.scala | 9 +++++++-- .../src/test/scala/fs2/io/file/FilesSuite.scala | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/io/shared/src/main/scala/fs2/io/file/Files.scala b/io/shared/src/main/scala/fs2/io/file/Files.scala index a2136fa545..61f3bb1c36 100644 --- a/io/shared/src/main/scala/fs2/io/file/Files.scala +++ b/io/shared/src/main/scala/fs2/io/file/Files.scala @@ -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)) } diff --git a/io/shared/src/test/scala/fs2/io/file/FilesSuite.scala b/io/shared/src/test/scala/fs2/io/file/FilesSuite.scala index 62a5a5ff32..60b2d61c4c 100644 --- a/io/shared/src/test/scala/fs2/io/file/FilesSuite.scala +++ b/io/shared/src/test/scala/fs2/io/file/FilesSuite.scala @@ -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") {