Skip to content

Commit ea1712d

Browse files
Wrote 2 test cases, modified writeAll function to stop inwanted file creation
1 parent 5fd4317 commit ea1712d

2 files changed

Lines changed: 46 additions & 11 deletions

File tree

io/shared/src/main/scala/fs2/io/file/Files.scala

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff 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,

io/shared/src/test/scala/fs2/io/file/FilesSuite.scala

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff 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") {

0 commit comments

Comments
 (0)