Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make os.remove behave more like Files.deleteIfExists, add checkExists flag to fall back to old behavior #89

Merged
merged 1 commit into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
.
  • Loading branch information
lihaoyi committed Dec 9, 2021
commit e48778eb4543d0aab3ee83f023a72eff42e6b49a
14 changes: 11 additions & 3 deletions os/src/FileOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
sake92 marked this conversation as resolved.
Show resolved Hide resolved
if (checkExists) {
Files.delete(target.wrapped)
true
}else{
Files.deleteIfExists(target.wrapped)
}
}

object all extends Function1[Path, Unit]{
def apply(target: Path) = {
Expand All @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion os/test/src/OpTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down