Skip to content

Commit 6afa19a

Browse files
saiHemakparkera
authored andcommitted
Implementing NSFileManager.homeDirectoryForCurrentUser and homeDirectory(forUser userName: ) (#860)
1 parent b5e006c commit 6afa19a

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Foundation/NSFileManager.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,9 +712,15 @@ extension FileManager {
712712
}
713713

714714
extension FileManager {
715-
open var homeDirectoryForCurrentUser: URL { NSUnimplemented() }
715+
open var homeDirectoryForCurrentUser: URL {
716+
return homeDirectory(forUser: CFCopyUserName().takeRetainedValue()._swiftObject)!
717+
}
716718
open var temporaryDirectory: URL { NSUnimplemented() }
717-
open func homeDirectory(forUser userName: String) -> URL? { NSUnimplemented() }
719+
open func homeDirectory(forUser userName: String) -> URL? {
720+
guard !userName.isEmpty else { return nil }
721+
guard let url = CFCopyHomeDirectoryURLForUser(userName._cfObject) else { return nil }
722+
return url.takeRetainedValue()._swiftObject
723+
}
718724
}
719725

720726
extension FileManager {

TestFoundation/TestNSFileManager.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class TestNSFileManager : XCTestCase {
2929
("test_pathEnumerator",test_pathEnumerator),
3030
("test_contentsOfDirectoryAtPath", test_contentsOfDirectoryAtPath),
3131
("test_subpathsOfDirectoryAtPath", test_subpathsOfDirectoryAtPath),
32-
("test_copyItemAtPathToPath", test_copyItemAtPathToPath)
32+
("test_copyItemAtPathToPath", test_copyItemAtPathToPath),
33+
("test_homedirectoryForUser", test_homedirectoryForUser),
3334
]
3435
}
3536

@@ -474,4 +475,11 @@ class TestNSFileManager : XCTestCase {
474475
}
475476
XCTFail("Copy overwrites a file/folder that already exists")
476477
}
478+
479+
func test_homedirectoryForUser() {
480+
let filemanger = FileManager.default
481+
XCTAssertNil(filemanger.homeDirectory(forUser: "someuser"))
482+
XCTAssertNil(filemanger.homeDirectory(forUser: ""))
483+
XCTAssertNotNil(filemanger.homeDirectoryForCurrentUser)
484+
}
477485
}

0 commit comments

Comments
 (0)