Merge pull request #1399 from millenomi/pr/search-paths-for-preferences
diff --git a/Foundation/FileManager.swift b/Foundation/FileManager.swift
index 7537098..1e4f046 100644
--- a/Foundation/FileManager.swift
+++ b/Foundation/FileManager.swift
@@ -150,7 +150,7 @@
var isDir: ObjCBool = false
if !fileExists(atPath: path, isDirectory: &isDir) {
let parent = path._nsObject.deletingLastPathComponent
- if !fileExists(atPath: parent, isDirectory: &isDir) {
+ if !parent.isEmpty && !fileExists(atPath: parent, isDirectory: &isDir) {
try createDirectory(atPath: parent, withIntermediateDirectories: true, attributes: attributes)
}
if mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) != 0 {
diff --git a/Foundation/NSArray.swift b/Foundation/NSArray.swift
index b536336..d53b42e 100644
--- a/Foundation/NSArray.swift
+++ b/Foundation/NSArray.swift
@@ -653,7 +653,26 @@
}
open func pathsMatchingExtensions(_ filterTypes: [String]) -> [String] {
- NSUnimplemented()
+ guard filterTypes.count > 0 else {
+ return []
+ }
+
+ let extensions: [String] = filterTypes.map {
+ var ext = "."
+ ext.append($0)
+ return ext
+ }
+
+ return self.filter {
+ // The force unwrap will abort if the element is not a String but this behaviour matches Dawrin, which throws an exception.
+ let filename = $0 as! String
+ for ext in extensions {
+ if filename.hasSuffix(ext) && filename.count > ext.count {
+ return true
+ }
+ }
+ return false
+ } as! [String]
}
override open var _cfTypeID: CFTypeID {
diff --git a/TestFoundation/TestFileManager.swift b/TestFoundation/TestFileManager.swift
index bef3d27..266fb76 100644
--- a/TestFoundation/TestFileManager.swift
+++ b/TestFoundation/TestFileManager.swift
@@ -33,6 +33,7 @@
("test_copyItemAtPathToPath", test_copyItemAtPathToPath),
("test_homedirectoryForUser", test_homedirectoryForUser),
("test_temporaryDirectoryForUser", test_temporaryDirectoryForUser),
+ ("test_creatingDirectoryWithShortIntermediatePath", test_creatingDirectoryWithShortIntermediatePath),
]
}
@@ -111,6 +112,20 @@
}
}
+ func test_creatingDirectoryWithShortIntermediatePath() {
+ let fileManager = FileManager.default
+ fileManager.changeCurrentDirectoryPath(NSTemporaryDirectory())
+
+ let relativePath = NSUUID().uuidString
+
+ do {
+ try fileManager.createDirectory(atPath: relativePath, withIntermediateDirectories: true, attributes: nil)
+ try fileManager.removeItem(atPath: relativePath)
+ } catch {
+ XCTFail("Failed to create and clean up directory")
+ }
+ }
+
func test_moveFile() {
let fm = FileManager.default
let path = NSTemporaryDirectory() + "testfile\(NSUUID().uuidString)"
diff --git a/TestFoundation/TestNSArray.swift b/TestFoundation/TestNSArray.swift
index ce56a1d..69b8eb7 100644
--- a/TestFoundation/TestNSArray.swift
+++ b/TestFoundation/TestNSArray.swift
@@ -51,6 +51,7 @@
("test_insertObjectAtIndex", test_insertObjectAtIndex),
("test_insertObjectsAtIndexes", test_insertObjectsAtIndexes),
("test_replaceObjectsAtIndexesWithObjects", test_replaceObjectsAtIndexesWithObjects),
+ ("test_pathsMatchingExtensions", test_pathsMatchingExtensions),
]
}
@@ -746,6 +747,24 @@
XCTAssertEqual(a3, ["a", "b", "c", "d"])
}
+ func test_pathsMatchingExtensions() {
+ let paths = NSArray(arrayLiteral: "file1.txt", "/tmp/file2.txt", "file3.jpg", "file3.png", "/file4.png", "txt", ".txt")
+ let match1 = paths.pathsMatchingExtensions(["txt"])
+ XCTAssertEqual(match1, ["file1.txt", "/tmp/file2.txt"])
+
+ let match2 = paths.pathsMatchingExtensions([])
+ XCTAssertEqual(match2, [])
+
+ let match3 = paths.pathsMatchingExtensions([".txt", "png"])
+ XCTAssertEqual(match3, ["file3.png", "/file4.png"])
+
+ let match4 = paths.pathsMatchingExtensions(["", ".tx", "tx"])
+ XCTAssertEqual(match4, [])
+
+ let match5 = paths.pathsMatchingExtensions(["..txt"])
+ XCTAssertEqual(match5, [])
+ }
+
private func createTestFile(_ path: String, _contents: Data) -> String? {
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString + "/"
do {