From d273b5b7025d386feef79ef6bad7de762e106eaf Mon Sep 17 00:00:00 2001 From: Grzegorz Wikiera Date: Wed, 28 Oct 2020 20:56:51 +0100 Subject: [PATCH] Add `isEmpty` method for `Folder` (#119) * Add `isEmpty` method for `Folder` Co-authored-by: Grzegorz Wikiera --- Sources/Files.swift | 13 +++++++++++++ Tests/FilesTests/FilesTests.swift | 21 +++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/Sources/Files.swift b/Sources/Files.swift index cdb8d57..830f1ba 100644 --- a/Sources/Files.swift +++ b/Sources/Files.swift @@ -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) diff --git a/Tests/FilesTests/FilesTests.swift b/Tests/FilesTests/FilesTests.swift index 19c5d1e..040f60e 100644 --- a/Tests/FilesTests/FilesTests.swift +++ b/Tests/FilesTests/FilesTests.swift @@ -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") @@ -873,6 +893,7 @@ class FilesTests: XCTestCase { ("testAccessingSubfolderByPath", testAccessingSubfolderByPath), ("testEmptyingFolder", testEmptyingFolder), ("testEmptyingFolderWithHiddenFiles", testEmptyingFolderWithHiddenFiles), + ("testCheckingEmptyFolders", testCheckingEmptyFolders), ("testMovingFiles", testMovingFiles), ("testCopyingFiles", testCopyingFiles), ("testCopyingFolders", testCopyingFolders),