Fix Linux testEnumerationSkippingHiddenFiles()
This commit is contained in:
@@ -1,13 +1,19 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public extension Path {
|
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.
|
Same as the `ls -a` command ∴ is ”shallow”
|
||||||
func ls(skipHiddenFiles: Bool = false) throws -> [Entry] {
|
- Parameter includeHiddenFiles: If `true`, hidden files are included in the results. Defaults to `true`.
|
||||||
let options: FileManager.DirectoryEnumerationOptions = skipHiddenFiles ? [.skipsHiddenFiles] : []
|
- Important: `includeHiddenFiles` does not work on Linux
|
||||||
let paths = try FileManager.default.contentsOfDirectory(at: url,
|
*/
|
||||||
includingPropertiesForKeys: nil,
|
func ls(includeHiddenFiles: Bool = true) throws -> [Entry] {
|
||||||
options: options)
|
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? {
|
func convert(url: URL) -> Entry? {
|
||||||
guard let path = Path(url.path) else { return nil }
|
guard let path = Path(url.path) else { return nil }
|
||||||
return Entry(kind: path.isDirectory ? .directory : .file, path: path)
|
return Entry(kind: path.isDirectory ? .directory : .file, path: path)
|
||||||
|
|||||||
@@ -31,8 +31,9 @@ class PathTests: XCTestCase {
|
|||||||
XCTAssertEqual(paths, ["a", "b", "c", ".d"])
|
XCTAssertEqual(paths, ["a", "b", "c", ".d"])
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func testEnumerationSkippingHiddenFiles() throws {
|
func testEnumerationSkippingHiddenFiles() throws {
|
||||||
|
#if !os(Linux)
|
||||||
let tmpdir_ = try TemporaryDirectory()
|
let tmpdir_ = try TemporaryDirectory()
|
||||||
let tmpdir = tmpdir_.path
|
let tmpdir = tmpdir_.path
|
||||||
try tmpdir.join("a").mkdir().join("c").touch()
|
try tmpdir.join("a").mkdir().join("c").touch()
|
||||||
@@ -42,7 +43,7 @@ class PathTests: XCTestCase {
|
|||||||
|
|
||||||
var paths = Set<String>()
|
var paths = Set<String>()
|
||||||
var dirs = 0
|
var dirs = 0
|
||||||
for entry in try tmpdir.ls(skipHiddenFiles: true) {
|
for entry in try tmpdir.ls(includeHiddenFiles: false) {
|
||||||
if entry.kind == .directory {
|
if entry.kind == .directory {
|
||||||
dirs += 1
|
dirs += 1
|
||||||
}
|
}
|
||||||
@@ -50,7 +51,7 @@ class PathTests: XCTestCase {
|
|||||||
}
|
}
|
||||||
XCTAssertEqual(dirs, 1)
|
XCTAssertEqual(dirs, 1)
|
||||||
XCTAssertEqual(paths, ["a", "b", "c"])
|
XCTAssertEqual(paths, ["a", "b", "c"])
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
func testRelativeTo() {
|
func testRelativeTo() {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ extension PathTests {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
#if os(Linux)
|
#if !os(macOS)
|
||||||
public func __allTests() -> [XCTestCaseEntry] {
|
public func __allTests() -> [XCTestCaseEntry] {
|
||||||
return [
|
return [
|
||||||
testCase(PathTests.__allTests),
|
testCase(PathTests.__allTests),
|
||||||
|
|||||||
Reference in New Issue
Block a user