Skip to content

Commit 2a1ad03

Browse files
author
Dietmar Planitzer
committed
Implemented FileManager.temporaryDirectory
1 parent 0e784e9 commit 2a1ad03

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Foundation/FileManager.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,11 @@ extension FileManager {
718718
open var homeDirectoryForCurrentUser: URL {
719719
return homeDirectory(forUser: CFCopyUserName().takeRetainedValue()._swiftObject)!
720720
}
721-
open var temporaryDirectory: URL { NSUnimplemented() }
721+
722+
open var temporaryDirectory: URL {
723+
return URL(fileURLWithPath: NSTemporaryDirectory())
724+
}
725+
722726
open func homeDirectory(forUser userName: String) -> URL? {
723727
guard !userName.isEmpty else { return nil }
724728
guard let url = CFCopyHomeDirectoryURLForUser(userName._cfObject) else { return nil }

TestFoundation/TestFileManager.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class TestFileManager : XCTestCase {
3131
("test_subpathsOfDirectoryAtPath", test_subpathsOfDirectoryAtPath),
3232
("test_copyItemAtPathToPath", test_copyItemAtPathToPath),
3333
("test_homedirectoryForUser", test_homedirectoryForUser),
34+
("test_temporaryDirectoryForUser", test_temporaryDirectoryForUser),
3435
]
3536
}
3637

@@ -525,4 +526,25 @@ class TestFileManager : XCTestCase {
525526
XCTAssertNil(filemanger.homeDirectory(forUser: ""))
526527
XCTAssertNotNil(filemanger.homeDirectoryForCurrentUser)
527528
}
529+
530+
func test_temporaryDirectoryForUser() {
531+
let filemanger = FileManager.default
532+
let tmpDir = filemanger.temporaryDirectory
533+
XCTAssertNotNil(tmpDir)
534+
let tmpFileUrl = tmpDir.appendingPathComponent("test.bin")
535+
let tmpFilePath = tmpFileUrl.path
536+
537+
do {
538+
if filemanger.fileExists(atPath: tmpFilePath) {
539+
try filemanger.removeItem(at: tmpFileUrl)
540+
}
541+
542+
try "hello world".write(to: tmpFileUrl, atomically: false, encoding: .utf8)
543+
XCTAssert(filemanger.fileExists(atPath: tmpFilePath))
544+
545+
try filemanger.removeItem(at: tmpFileUrl)
546+
} catch {
547+
XCTFail("Unable to write a file to the temporary directory: \(tmpDir), err: \(error)")
548+
}
549+
}
528550
}

0 commit comments

Comments
 (0)