From 920f007660b111b6ee2a8b4020452245b6fabbc3 Mon Sep 17 00:00:00 2001 From: Max Howell Date: Sun, 20 Jan 2019 17:32:37 -0500 Subject: [PATCH] Fix Linux testEnumerationSkippingHiddenFiles() --- Sources/Path+ls.swift | 20 +++++++++++++------- Tests/PathTests/PathTests.swift | 7 ++++--- Tests/PathTests/XCTestManifests.swift | 2 +- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/Sources/Path+ls.swift b/Sources/Path+ls.swift index 08d42eb..13bfa6e 100644 --- a/Sources/Path+ls.swift +++ b/Sources/Path+ls.swift @@ -1,13 +1,19 @@ import Foundation public extension Path { - /// Same as the `ls` command ∴ is ”shallow” - /// - Parameter skipHiddenFiles: Same as the `ls -a` if false. Otherwise returns only the non hidden files. Default is false. - func ls(skipHiddenFiles: Bool = false) throws -> [Entry] { - let options: FileManager.DirectoryEnumerationOptions = skipHiddenFiles ? [.skipsHiddenFiles] : [] - let paths = try FileManager.default.contentsOfDirectory(at: url, - includingPropertiesForKeys: nil, - options: options) + /** + Same as the `ls -a` command ∴ is ”shallow” + - Parameter includeHiddenFiles: If `true`, hidden files are included in the results. Defaults to `true`. + - Important: `includeHiddenFiles` does not work on Linux + */ + func ls(includeHiddenFiles: Bool = true) throws -> [Entry] { + var opts = FileManager.DirectoryEnumerationOptions() + #if !os(Linux) + if !includeHiddenFiles { + opts.insert(.skipsHiddenFiles) + } + #endif + let paths = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: opts) func convert(url: URL) -> Entry? { guard let path = Path(url.path) else { return nil } return Entry(kind: path.isDirectory ? .directory : .file, path: path) diff --git a/Tests/PathTests/PathTests.swift b/Tests/PathTests/PathTests.swift index c5a9e9f..8048f4e 100644 --- a/Tests/PathTests/PathTests.swift +++ b/Tests/PathTests/PathTests.swift @@ -31,8 +31,9 @@ class PathTests: XCTestCase { XCTAssertEqual(paths, ["a", "b", "c", ".d"]) } - + func testEnumerationSkippingHiddenFiles() throws { + #if !os(Linux) let tmpdir_ = try TemporaryDirectory() let tmpdir = tmpdir_.path try tmpdir.join("a").mkdir().join("c").touch() @@ -42,7 +43,7 @@ class PathTests: XCTestCase { var paths = Set() var dirs = 0 - for entry in try tmpdir.ls(skipHiddenFiles: true) { + for entry in try tmpdir.ls(includeHiddenFiles: false) { if entry.kind == .directory { dirs += 1 } @@ -50,7 +51,7 @@ class PathTests: XCTestCase { } XCTAssertEqual(dirs, 1) XCTAssertEqual(paths, ["a", "b", "c"]) - + #endif } func testRelativeTo() { diff --git a/Tests/PathTests/XCTestManifests.swift b/Tests/PathTests/XCTestManifests.swift index ce48de0..9104303 100644 --- a/Tests/PathTests/XCTestManifests.swift +++ b/Tests/PathTests/XCTestManifests.swift @@ -17,7 +17,7 @@ extension PathTests { ] } -#if os(Linux) +#if !os(macOS) public func __allTests() -> [XCTestCaseEntry] { return [ testCase(PathTests.__allTests),