Skip to content

Commit

Permalink
Make sequences exclude hidden files per default (+ add override)
Browse files Browse the repository at this point in the history
With this change, sequences of files & folders per default won’t contain
hidden (dot) files, unless the API user explicitly asks for them using
`includeHidden: true`.
  • Loading branch information
JohnSundell committed Jan 20, 2017
1 parent 170d3cb commit 1aa3bfc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
30 changes: 21 additions & 9 deletions Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -487,22 +487,24 @@ public final class Folder: FileSystem.Item, FileSystemIterable {
* Create a sequence containing the files that are contained within this folder
*
* - parameter recursive: Whether the files contained in all subfolders of this folder should also be included
* - parameter includeHidden: Whether hidden (dot) files should be included in the sequence (default: false)
*
* If `recursive = true` the folder tree will be traversed breath-first
*/
public func makeFileSequence(recursive: Bool = false) -> FileSystemSequence<File> {
return FileSystemSequence(path: path, recursive: recursive, using: fileManager)
public func makeFileSequence(recursive: Bool = false, includeHidden: Bool = false) -> FileSystemSequence<File> {
return FileSystemSequence(path: path, recursive: recursive, includeHidden: includeHidden, using: fileManager)
}

/**
* Create a sequence containing the folders that are subfolders of this folder
*
* - parameter recursive: Whether the entire folder tree contained under this folder should also be included
* - parameter includeHidden: Whether hidden (dot) files should be included in the sequence (default: false)
*
* If `recursive = true` the folder tree will be traversed breath-first
*/
public func makeSubfolderSequence(recursive: Bool = false) -> FileSystemSequence<Folder> {
return FileSystemSequence(path: path, recursive: recursive, using: fileManager)
public func makeSubfolderSequence(recursive: Bool = false, includeHidden: Bool = false) -> FileSystemSequence<Folder> {
return FileSystemSequence(path: path, recursive: recursive, includeHidden: includeHidden, using: fileManager)
}

/**
Expand Down Expand Up @@ -555,17 +557,19 @@ public class FileSystemSequence<T: FileSystem.Item>: Sequence where T: FileSyste

private let path: String
private let recursive: Bool
private let includeHidden: Bool
private let fileManager: FileManager

fileprivate init(path: String, recursive: Bool, using fileManager: FileManager) {
fileprivate init(path: String, recursive: Bool, includeHidden: Bool, using fileManager: FileManager) {
self.path = path
self.recursive = recursive
self.includeHidden = includeHidden
self.fileManager = fileManager
}

/// Create an iterator to use to iterate over the sequence
public func makeIterator() -> FileSystemIterator<T> {
return FileSystemIterator(path: path, recursive: recursive, using: fileManager)
return FileSystemIterator(path: path, recursive: recursive, includeHidden: includeHidden, using: fileManager)
}

/// Move all the items in this sequence to a new folder. See `FileSystem.Item.move(to:)` for more info.
Expand All @@ -578,14 +582,16 @@ public class FileSystemSequence<T: FileSystem.Item>: Sequence where T: FileSyste
public class FileSystemIterator<T: FileSystem.Item>: IteratorProtocol where T: FileSystemIterable {
private let path: String
private let recursive: Bool
private let includeHidden: Bool
private let fileManager: FileManager
private var itemNames: [String]
private lazy var childIteratorQueue = [FileSystemIterator]()
private var currentChildIterator: FileSystemIterator?

fileprivate init(path: String, recursive: Bool, using fileManager: FileManager) {
fileprivate init(path: String, recursive: Bool, includeHidden: Bool, using fileManager: FileManager) {
self.path = path
self.recursive = recursive
self.includeHidden = includeHidden
self.fileManager = fileManager
self.itemNames = fileManager.itemNames(inFolderAtPath: path)
}
Expand All @@ -608,12 +614,18 @@ public class FileSystemIterator<T: FileSystem.Item>: IteratorProtocol where T: F
}

let nextItemName = itemNames.removeFirst()

guard includeHidden || !nextItemName.hasPrefix(".") else {
return next()
}

let nextItemPath = path + nextItemName
let nextItem = try? T(path: nextItemPath, using: fileManager)

if recursive && nextItem?.kind != .file {
let childIterator = FileSystemIterator(path: nextItemPath + "/", recursive: true, using: fileManager)
childIteratorQueue.append(childIterator)
let childPath = nextItemPath + "/"
let child = FileSystemIterator(path: childPath, recursive: true, includeHidden: includeHidden, using: fileManager)
childIteratorQueue.append(child)
}

return nextItem ?? next()
Expand Down
14 changes: 14 additions & 0 deletions Tests/FilesTests/FilesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,25 @@ class FilesTests: XCTestCase {
try folder.createFile(named: "2")
try folder.createFile(named: "3")

// Hidden files should be excluded by default
try folder.createFile(named: ".hidden")

XCTAssertEqual(folder.files.names, ["1", "2", "3"])
XCTAssertEqual(folder.files.count, 3)
}
}

func testEnumeratingFilesIncludingHidden() {
performTest {
try folder.createFile(named: ".hidden")
try folder.createFile(named: "visible")

let files = folder.makeFileSequence(includeHidden: true)
XCTAssertEqual(files.names, [".hidden", "visible"])
XCTAssertEqual(files.count, 2)
}
}

func testEnumeratingFilesRecursively() {
performTest {
let subfolder1 = try folder.createSubfolder(named: "1")
Expand Down

0 comments on commit 1aa3bfc

Please sign in to comment.