File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -530,13 +530,21 @@ object Files extends FilesCompanionPlatform with FilesLowPriority {
530530 })
531531
532532 def writeAll (
533- path : Path ,
534- flags : Flags
535- ): Pipe [F , Byte , Nothing ] =
533+ path : Path ,
534+ flags : Flags
535+ ): Pipe [F , Byte , Nothing ] =
536536 in =>
537- Stream
538- .resource(writeCursor(path, flags))
539- .flatMap(_.writeAll(in).void.stream)
537+ in.pull.stepLeg.flatMap {
538+ case None => Pull .done
539+ case Some (leg) =>
540+ Stream
541+ .resource(writeCursor(path, flags))
542+ .flatMap { cursor =>
543+ cursor.writeAll(leg.stream.cons(leg.head)).void.stream
544+ }
545+ .pull
546+ .echo
547+ }.stream
540548
541549 def writeCursor (
542550 path : Path ,
Original file line number Diff line number Diff line change @@ -113,8 +113,8 @@ class FilesSuite extends Fs2Suite with BaseFileSuite {
113113 val src = Stream (" Hello" , " world!" ).covary[IO ].through(text.utf8.encode)
114114 src.through(Files [IO ].writeAll(path)) ++
115115 src.through(Files [IO ].writeAll(path, Flags .Append )) ++ Files [IO ]
116- .readAll(path)
117- .through(text.utf8.decode)
116+ .readAll(path)
117+ .through(text.utf8.decode)
118118 }
119119 .compile
120120 .foldMonoid
@@ -165,9 +165,10 @@ class FilesSuite extends Fs2Suite with BaseFileSuite {
165165 }
166166 .compile
167167 .foldMonoid
168- .assertEquals(""" |foo
169- |bar
170- |""" .stripMargin)
168+ .assertEquals(
169+ """ |foo
170+ |bar
171+ |""" .stripMargin)
171172 }
172173
173174 test(" writeUtf8Lines - side effect" ) {
@@ -200,6 +201,32 @@ class FilesSuite extends Fs2Suite with BaseFileSuite {
200201 .foldMonoid
201202 .assertEquals(" " )
202203 }
204+ test(" empty stream does not create file" ) {
205+ Files [IO ].tempDirectory.use { dir =>
206+ val path = dir / " should-not-exist.txt"
207+ Stream .empty
208+ .covary[IO ]
209+ .through(Files [IO ].writeAll(path))
210+ .compile
211+ .drain
212+ .flatMap(_ => Files [IO ].exists(path))
213+ .assertEquals(false )
214+ }
215+ }
216+
217+ test(" error stream does not create file" ) {
218+ Files [IO ].tempDirectory.use { dir =>
219+ val path = dir / " should-not-exist.txt"
220+ Stream
221+ .raiseError[IO ](new RuntimeException (" boom" ))
222+ .through(Files [IO ].writeAll(path))
223+ .compile
224+ .drain
225+ .attempt
226+ .flatMap(_ => Files [IO ].exists(path))
227+ .assertEquals(false )
228+ }
229+ }
203230 }
204231
205232 group(" tail" ) {
You can’t perform that action at this time.
0 commit comments