Skip to content

Commit

Permalink
Added FileListApi.delete implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-podzigun committed Nov 22, 2019
1 parent 617bda9 commit 370c604
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/src/main/scala/farclone/api/filelist/FileListApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ import scala.concurrent.Future
trait FileListApi {

def readDir(parent: Option[String], dir: String): Future[FileListDir]

def delete(parent: String, items: Seq[FileListItem]): Future[Unit]
}
27 changes: 27 additions & 0 deletions app/src/main/scala/farclone/app/filelist/FileListApiImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@ class FileListApiImpl extends FileListApi {
)
}
}

def delete(parent: String, items: Seq[FileListItem]): Future[Unit] = {

def delDirItems(parent: String, items: Seq[(String, Boolean)]): Future[Unit] = {
items.foldLeft(Future.successful(())) { case (res, (name, isDir)) =>
res.flatMap { _ =>
if (isDir) {
val dir = path.join(parent, name)
fs.readdir(dir).flatMap { files =>
val items = files.map { name =>
val stats = fs.lstatSync(path.join(dir, name))
(name, stats.isDirectory())
}
delDirItems(dir, items).map { _ =>
fs.rmdirSync(dir)
}
}
}
else Future.successful {
fs.unlinkSync(path.join(parent, name))
}
}
}
}

delDirItems(parent, items.map(i => (i.name, i.isDir)))
}

private[filelist] def getPermissions(mode: Int): String = {

Expand Down
56 changes: 56 additions & 0 deletions app/src/test/scala/farclone/app/filelist/FileListApiImplSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,62 @@ class FileListApiImplSpec extends AsyncTestSpec {
}
}

it should "delete items when delete" in {
//given
val tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "farclone-test-"))
fs.existsSync(tmpDir) shouldBe true

def create(parent: String, name: String, isDir: Boolean): (String, String) = {
val fullPath = path.join(parent, name)
if (isDir) fs.mkdirSync(fullPath)
else fs.writeFileSync(fullPath, s"file: $fullPath")
(fullPath, name)
}

val (d1, d1Name) = create(tmpDir, "dir1", isDir = true)
val (f1, f1Name) = create(tmpDir, "file1.txt", isDir = false)
val (d2, _) = create(d1, "dir2", isDir = true)
val (f2, _) = create(d1, "file2.txt", isDir = false)
val (d3, _) = create(d2, "dir3", isDir = true)
val (f3, _) = create(d2, "file3.txt", isDir = false)
val items = List(
FileListItem(d1Name, isDir = true),
FileListItem(f1Name)
)

//when
val resultF = apiImp.delete(tmpDir, items)

//then
val resCheckF = resultF.map { _ =>
fs.existsSync(d1) shouldBe false
fs.existsSync(f1) shouldBe false
fs.existsSync(d2) shouldBe false
fs.existsSync(f2) shouldBe false
fs.existsSync(d3) shouldBe false
fs.existsSync(f3) shouldBe false
}

//cleanup
resCheckF.onComplete { _ =>
def del(path: String, isDir: Boolean): Unit = {
if (fs.existsSync(path)) {
if (isDir) fs.rmdirSync(path)
else fs.unlinkSync(path)
}
}

del(f3, isDir = false)
del(f2, isDir = false)
del(f1, isDir = false)
del(d3, isDir = true)
del(d2, isDir = true)
del(d1, isDir = true)
del(tmpDir, isDir = true)
}
resCheckF
}

it should "return file permissions" in {
//given
def flag(s: Char, c: Char, f: Int): Int = {
Expand Down

0 comments on commit 370c604

Please sign in to comment.