Skip to content

Commit

Permalink
Add Folder.createSubfolderIfNeeded()
Browse files Browse the repository at this point in the history
Just like its FileSystem equivalent, this method either creates or returns
an existing subfolder for a given name.
  • Loading branch information
JohnSundell committed Mar 7, 2017
1 parent 5353586 commit 09580a7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,21 @@ public final class Folder: FileSystem.Item, FileSystemIterable {
throw Error.creatingFolderFailed
}
}

/**
* Either return an existing subfolder, or create a new one, for a given name
*
* - parameter folderName: The name of the folder to either get or create
*
* - throws: `Folder.Error.creatingFolderFailed`
*/
public func createSubfolderIfNeeded(withName folderName: String) throws -> Folder {
if let existingFolder = try? subfolder(named: folderName) {
return existingFolder
}

return try createSubfolder(named: folderName)
}

/**
* Create a sequence containing the files that are contained within this folder
Expand Down
11 changes: 11 additions & 0 deletions Tests/FilesTests/FilesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,17 @@ class FilesTests: XCTestCase {
XCTAssertEqual(subfolderA.files.first, subfolderB.files.first)
}
}

func testCreateSubfolderIfNeeded() {
performTest {
let subfolderA = try folder.createSubfolderIfNeeded(withName: "folder")
try subfolderA.createFile(named: "file")
let subfolderB = try folder.createSubfolderIfNeeded(withName: "folder")
XCTAssertEqual(subfolderA, subfolderB)
XCTAssertEqual(subfolderA.files.count, subfolderB.files.count)
XCTAssertEqual(subfolderA.files.first, subfolderB.files.first)
}
}

func testUsingCustomFileManager() {
class FileManagerMock: FileManager {
Expand Down

0 comments on commit 09580a7

Please sign in to comment.