Skip to content

Commit

Permalink
fix: Treat non-existing directory as an empty sequence. (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonbreedt authored and kylef committed Feb 17, 2017
1 parent da7b559 commit f3e8fa6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Sources/PathKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -651,16 +651,16 @@ extension Path : Sequence {
public typealias Element = Path

let path: Path
let directoryEnumerator: FileManager.DirectoryEnumerator
let directoryEnumerator: FileManager.DirectoryEnumerator?

init(path: Path, options mask: DirectoryEnumerationOptions = []) {
let options = FileManager.DirectoryEnumerationOptions(rawValue: mask.rawValue)
self.path = path
self.directoryEnumerator = Path.fileManager.enumerator(at: path.url, includingPropertiesForKeys: nil, options: options)!
self.directoryEnumerator = Path.fileManager.enumerator(at: path.url, includingPropertiesForKeys: nil, options: options)
}

public func next() -> Path? {
let next = directoryEnumerator.nextObject()
let next = directoryEnumerator?.nextObject()

if let next = next as? URL {
return Path(next.path)
Expand All @@ -670,7 +670,7 @@ extension Path : Sequence {

/// Skip recursion into the most recently obtained subdirectory.
public func skipDescendants() {
directoryEnumerator.skipDescendants()
directoryEnumerator?.skipDescendants()
}
}

Expand Down
2 changes: 2 additions & 0 deletions Tests/PathKitTests/PathKitSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ describe("PathKit") {
}

try expect(children.isEmpty).to.beTrue()
try expect(Path("/non/existing/directory/path").makeIterator().next()).to.beNil()
#endif
}

Expand All @@ -446,6 +447,7 @@ describe("PathKit") {
}

try expect(children.isEmpty).to.beTrue()
try expect(Path("/non/existing/directory/path").makeIterator().next()).to.beNil()
#endif
}
}
Expand Down

0 comments on commit f3e8fa6

Please sign in to comment.