Skip to content

Commit

Permalink
Add isEmpty method for Folder (#119)
Browse files Browse the repository at this point in the history
* Add `isEmpty` method for `Folder`

Co-authored-by: Grzegorz Wikiera <[email protected]>
  • Loading branch information
gwikiera and Grzegorz Wikiera authored Oct 28, 2020
1 parent 671824c commit d273b5b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,19 @@ public extension Folder {
folders.includeHidden = includeHidden
try folders.delete()
}

func isEmpty(includingHidden includeHidden: Bool = false) -> Bool {
var files = self.files
files.includeHidden = includeHidden

if files.first != nil {
return false
}

var folders = subfolders
folders.includeHidden = includeHidden
return folders.first == nil
}
}

#if os(iOS) || os(tvOS) || os(macOS)
Expand Down
21 changes: 21 additions & 0 deletions Tests/FilesTests/FilesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,26 @@ class FilesTests: XCTestCase {
}
}

func testCheckingEmptyFolders() {
performTest {
let emptySubfolder = try folder.createSubfolder(named: "1")
XCTAssertTrue(emptySubfolder.isEmpty())

let subfolderWithFile = try folder.createSubfolder(named: "2")
try subfolderWithFile.createFile(named: "A")
XCTAssertFalse(subfolderWithFile.isEmpty())

let subfolderWithHiddenFile = try folder.createSubfolder(named: "3")
try subfolderWithHiddenFile.createFile(named: ".B")
XCTAssertTrue(subfolderWithHiddenFile.isEmpty())
XCTAssertFalse(subfolderWithHiddenFile.isEmpty(includingHidden: true))

let subfolderWithFolder = try folder.createSubfolder(named: "3")
try subfolderWithFolder.createSubfolder(named: "4")
XCTAssertFalse(subfolderWithFile.isEmpty())
}
}

func testMovingFiles() {
performTest {
try folder.createFile(named: "A")
Expand Down Expand Up @@ -873,6 +893,7 @@ class FilesTests: XCTestCase {
("testAccessingSubfolderByPath", testAccessingSubfolderByPath),
("testEmptyingFolder", testEmptyingFolder),
("testEmptyingFolderWithHiddenFiles", testEmptyingFolderWithHiddenFiles),
("testCheckingEmptyFolders", testCheckingEmptyFolders),
("testMovingFiles", testMovingFiles),
("testCopyingFiles", testCopyingFiles),
("testCopyingFolders", testCopyingFolders),
Expand Down

0 comments on commit d273b5b

Please sign in to comment.