From f0d4272b93d1a0322ea5f49f1fb12e33a6ab3ec3 Mon Sep 17 00:00:00 2001 From: Iurii Malchenko Date: Sun, 19 Dec 2021 10:03:41 +0200 Subject: [PATCH 1/3] fsIO - realPath: re-throw javascript exception as NoSuchFileException --- .../scala/fs2/io/file/FilesPlatform.scala | 5 +++- .../test/scala/fs2/io/file/FilesSuite.scala | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/io/js/src/main/scala/fs2/io/file/FilesPlatform.scala b/io/js/src/main/scala/fs2/io/file/FilesPlatform.scala index 2bd0b980da..42ad16933f 100644 --- a/io/js/src/main/scala/fs2/io/file/FilesPlatform.scala +++ b/io/js/src/main/scala/fs2/io/file/FilesPlatform.scala @@ -321,7 +321,10 @@ private[fs2] trait FilesCompanionPlatform { readStream(path, chunkSize, Flags.Read)(_.setStart(start.toDouble).setEnd((end - 1).toDouble)) def realPath(path: Path): F[Path] = - F.fromPromise(F.delay(fsPromisesMod.realpath(path.toString))).map(Path(_)) + F.fromPromise(F.delay(fsPromisesMod.realpath(path.toString))).map(Path(_)).handleErrorWith { + case NoSuchFileException(e) => + F.raiseError(e) + } override def setFileTimes( path: Path, 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 ba075ffa4b..25d20cca72 100644 --- a/io/shared/src/test/scala/fs2/io/file/FilesSuite.scala +++ b/io/shared/src/test/scala/fs2/io/file/FilesSuite.scala @@ -728,4 +728,29 @@ class FilesSuite extends Fs2Suite with BaseFileSuite { .assertEquals(false) } } + + group("realPath") { + + test("doesn't fail if the path is for a file") { + tempFile + .use(Files[IO].realPath(_)) + .void + .assert + } + + test("doesn't fail if the path is for a directory") { + tempDirectory + .use(Files[IO].realPath(_)) + .void + .assert + } + + test("fails with NoSuchFileException if the path does not exist") { + tempDirectory + .map(_.resolve("non-existent-file")) + .use(Files[IO].realPath(_)) + .intercept[NoSuchFileException] + } + } + } From 613f3a3d12f6689476d52701645b201a73b0dc8c Mon Sep 17 00:00:00 2001 From: Iurii Malchenko Date: Sun, 19 Dec 2021 10:12:24 +0200 Subject: [PATCH 2/3] recoverWith (oops) --- io/js/src/main/scala/fs2/io/file/FilesPlatform.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/io/js/src/main/scala/fs2/io/file/FilesPlatform.scala b/io/js/src/main/scala/fs2/io/file/FilesPlatform.scala index 42ad16933f..c6f4b78877 100644 --- a/io/js/src/main/scala/fs2/io/file/FilesPlatform.scala +++ b/io/js/src/main/scala/fs2/io/file/FilesPlatform.scala @@ -321,7 +321,7 @@ private[fs2] trait FilesCompanionPlatform { readStream(path, chunkSize, Flags.Read)(_.setStart(start.toDouble).setEnd((end - 1).toDouble)) def realPath(path: Path): F[Path] = - F.fromPromise(F.delay(fsPromisesMod.realpath(path.toString))).map(Path(_)).handleErrorWith { + F.fromPromise(F.delay(fsPromisesMod.realpath(path.toString))).map(Path(_)).recoverWith { case NoSuchFileException(e) => F.raiseError(e) } From 12f740c15ed2625121aca944543f4a8e5ce3ed4c Mon Sep 17 00:00:00 2001 From: Iurii Malchenko Date: Sun, 19 Dec 2021 21:13:47 +0200 Subject: [PATCH 3/3] adaptError instead of recoverWith->raiseError --- io/js/src/main/scala/fs2/io/file/FilesPlatform.scala | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/io/js/src/main/scala/fs2/io/file/FilesPlatform.scala b/io/js/src/main/scala/fs2/io/file/FilesPlatform.scala index c6f4b78877..9047e15284 100644 --- a/io/js/src/main/scala/fs2/io/file/FilesPlatform.scala +++ b/io/js/src/main/scala/fs2/io/file/FilesPlatform.scala @@ -321,9 +321,8 @@ private[fs2] trait FilesCompanionPlatform { readStream(path, chunkSize, Flags.Read)(_.setStart(start.toDouble).setEnd((end - 1).toDouble)) def realPath(path: Path): F[Path] = - F.fromPromise(F.delay(fsPromisesMod.realpath(path.toString))).map(Path(_)).recoverWith { - case NoSuchFileException(e) => - F.raiseError(e) + F.fromPromise(F.delay(fsPromisesMod.realpath(path.toString))).map(Path(_)).adaptError { + case NoSuchFileException(e) => e } override def setFileTimes(