Skip to content

Commit

Permalink
Merge pull request JohnSundell#64 from clayellis/contains-file
Browse files Browse the repository at this point in the history
Folder contains file/subfolder
  • Loading branch information
JohnSundell authored Mar 12, 2019
2 parents 5661117 + a7c2e78 commit 073242f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
18 changes: 18 additions & 0 deletions Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,15 @@ public final class Folder: FileSystem.Item, FileSystemIterable {
public func containsFile(named fileName: String) -> Bool {
return (try? file(named: fileName)) != nil
}

/**
* Return whether this folder contains a given file
*
* - parameter file: The file to check for
*/
public func contains(_ file: File) -> Bool {
return containsFile(named: file.name)
}

/**
* Return a folder with a given name that is contained in this folder
Expand Down Expand Up @@ -716,6 +725,15 @@ public final class Folder: FileSystem.Item, FileSystemIterable {
public func containsSubfolder(named folderName: String) -> Bool {
return (try? subfolder(named: folderName)) != nil
}

/**
* Return whether this folder contains a given subfolder
*
* - parameter subfolder: The folder to check for
*/
public func contains(_ subfolder: Folder) -> Bool {
return containsSubfolder(named: subfolder.name)
}

/**
* Create a file in this folder and return it
Expand Down
26 changes: 25 additions & 1 deletion Tests/FilesTests/FilesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,28 @@ class FilesTests: XCTestCase {
try assert(subfolder.file(named: "file"), throwsError: File.PathError.invalid(file.path))
}
}

func testFolderContainsFile() {
performTest {
let subfolder = try folder.createSubfolder(named: "subfolder")
let fileA = try subfolder.createFile(named: "A")
XCTAssertFalse(folder.contains(fileA))

let fileB = try folder.createFile(named: "B")
XCTAssertTrue(folder.contains(fileB))
}
}

func testFolderContainsSubfolder() {
performTest {
let subfolder = try folder.createSubfolder(named: "subfolder")
let subfolderA = try subfolder.createSubfolder(named: "A")
XCTAssertFalse(folder.contains(subfolderA))

let subfolderB = try folder.createSubfolder(named: "B")
XCTAssertTrue(folder.contains(subfolderB))
}
}

// MARK: - Utilities

Expand Down Expand Up @@ -775,7 +797,9 @@ class FilesTests: XCTestCase {
("testCreateFolderIfNeeded", testCreateFolderIfNeeded),
("testCreateSubfolderIfNeeded", testCreateSubfolderIfNeeded),
("testCreatingFileWithString", testCreatingFileWithString),
("testUsingCustomFileManager", testUsingCustomFileManager)
("testUsingCustomFileManager", testUsingCustomFileManager),
("testFolderContainsFile", testFolderContainsFile),
("testFolderContainsSubfolder", testFolderContainsSubfolder)
]
}

Expand Down

0 comments on commit 073242f

Please sign in to comment.