Skip to content

Implemented FileManager.temporaryDirectory #1234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,11 @@ extension FileManager {
open var homeDirectoryForCurrentUser: URL {
return homeDirectory(forUser: CFCopyUserName().takeRetainedValue()._swiftObject)!
}
open var temporaryDirectory: URL { NSUnimplemented() }

open var temporaryDirectory: URL {
return URL(fileURLWithPath: NSTemporaryDirectory())
}

open func homeDirectory(forUser userName: String) -> URL? {
guard !userName.isEmpty else { return nil }
guard let url = CFCopyHomeDirectoryURLForUser(userName._cfObject) else { return nil }
Expand Down
22 changes: 22 additions & 0 deletions TestFoundation/TestFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class TestFileManager : XCTestCase {
("test_subpathsOfDirectoryAtPath", test_subpathsOfDirectoryAtPath),
("test_copyItemAtPathToPath", test_copyItemAtPathToPath),
("test_homedirectoryForUser", test_homedirectoryForUser),
("test_temporaryDirectoryForUser", test_temporaryDirectoryForUser),
]
}

Expand Down Expand Up @@ -525,4 +526,25 @@ class TestFileManager : XCTestCase {
XCTAssertNil(filemanger.homeDirectory(forUser: ""))
XCTAssertNotNil(filemanger.homeDirectoryForCurrentUser)
}

func test_temporaryDirectoryForUser() {
let filemanger = FileManager.default
let tmpDir = filemanger.temporaryDirectory
XCTAssertNotNil(tmpDir)
let tmpFileUrl = tmpDir.appendingPathComponent("test.bin")
let tmpFilePath = tmpFileUrl.path

do {
if filemanger.fileExists(atPath: tmpFilePath) {
try filemanger.removeItem(at: tmpFileUrl)
}

try "hello world".write(to: tmpFileUrl, atomically: false, encoding: .utf8)
XCTAssert(filemanger.fileExists(atPath: tmpFilePath))

try filemanger.removeItem(at: tmpFileUrl)
} catch {
XCTFail("Unable to write a file to the temporary directory: \(tmpDir), err: \(error)")
}
}
}