From e48778eb4543d0aab3ee83f023a72eff42e6b49a Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Thu, 9 Dec 2021 22:43:20 +0800 Subject: [PATCH] . --- os/src/FileOps.scala | 14 +++++++++++--- os/test/src/OpTests.scala | 5 ++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/os/src/FileOps.scala b/os/src/FileOps.scala index 2a2ad9f7..27a53d8e 100644 --- a/os/src/FileOps.scala +++ b/os/src/FileOps.scala @@ -280,8 +280,16 @@ object copy { * any files or folders in the target path, or * does nothing if there aren't any */ -object remove extends Function1[Path, Unit]{ - def apply(target: Path): Unit = Files.delete(target.wrapped) +object remove extends Function1[Path, Boolean]{ + def apply(target: Path): Boolean = apply(target, false) + def apply(target: Path, checkExists: Boolean = false): Boolean = { + if (checkExists) { + Files.delete(target.wrapped) + true + }else{ + Files.deleteIfExists(target.wrapped) + } + } object all extends Function1[Path, Unit]{ def apply(target: Path) = { @@ -290,7 +298,7 @@ object remove extends Function1[Path, Unit]{ val nioTarget = target.wrapped if (Files.exists(nioTarget, LinkOption.NOFOLLOW_LINKS)) { if (Files.isDirectory(nioTarget, LinkOption.NOFOLLOW_LINKS)) { - walk.stream(target, preOrder = false).foreach(remove) + walk.stream(target, preOrder = false).foreach(remove(_)) } Files.delete(nioTarget) } diff --git a/os/test/src/OpTests.scala b/os/test/src/OpTests.scala index fd88b612..2e26ec81 100644 --- a/os/test/src/OpTests.scala +++ b/os/test/src/OpTests.scala @@ -27,9 +27,12 @@ object OpTests extends TestSuite{ test("rm"){ // shouldn't crash os.remove.all(os.pwd/"out"/"scratch"/"nonexistent") + // shouldn't crash + os.remove(os.pwd/"out"/"scratch"/"nonexistent") ==> false + // should crash intercept[NoSuchFileException]{ - os.remove(os.pwd/"out"/"scratch"/"nonexistent") + os.remove(os.pwd/"out"/"scratch"/"nonexistent", checkExists = true) } } }