Skip to content

Commit 9728639

Browse files
committed
Verify contents of the file attributes dictionary
Experiment tag added to attributesOfItemAtPath(path: String) function
1 parent 7d77b31 commit 9728639

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

Foundation/NSFileManager.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,13 @@ public class NSFileManager : NSObject {
211211

212212
This method replaces fileAttributesAtPath:traverseLink:.
213213
*/
214-
public func attributesOfItemAtPath(path: String) throws -> [String : AnyObject] {
214+
/// - Experiment: Note that the return type of this function is different than on Darwin Foundation (Any instead of AnyObject). This is likely to change once we have a more complete story for bridging in place.
215+
public func attributesOfItemAtPath(path: String) throws -> [String : Any] {
215216
var s = stat()
216217
guard lstat(path, &s) == 0 else {
217218
throw _NSErrorWithErrno(errno, reading: true, path: path)
218219
}
219-
var result = [String : AnyObject]()
220+
var result = [String : Any]()
220221
result[NSFileSize] = NSNumber(unsignedLongLong: UInt64(s.st_size))
221222

222223
#if os(OSX) || os(iOS)
@@ -233,14 +234,14 @@ public class NSFileManager : NSObject {
233234

234235
let pwd = getpwuid(s.st_uid)
235236
if pwd != nil && pwd.memory.pw_name != nil {
236-
if let name = NSString(bytes: pwd.memory.pw_name, length: Int(strlen(pwd.memory.pw_name)), encoding: NSUTF8StringEncoding) {
237+
if let name = String.fromCString(pwd.memory.pw_name) {
237238
result[NSFileOwnerAccountName] = name
238239
}
239240
}
240241

241242
let grd = getgrgid(s.st_gid)
242243
if grd != nil && grd.memory.gr_name != nil {
243-
if let name = NSString(bytes: grd.memory.gr_name, length: Int(strlen(grd.memory.gr_name)), encoding: NSUTF8StringEncoding) {
244+
if let name = String.fromCString(grd.memory.gr_name) {
244245
result[NSFileGroupOwnerAccountID] = name
245246
}
246247
}
@@ -255,7 +256,7 @@ public class NSFileManager : NSObject {
255256
case S_IFSOCK: type = NSFileTypeSocket
256257
default: type = NSFileTypeUnknown
257258
}
258-
result[NSFileType] = NSString(type)
259+
result[NSFileType] = type
259260

260261
if type == NSFileTypeBlockSpecial || type == NSFileTypeCharacterSpecial {
261262
result[NSFileDeviceIdentifier] = NSNumber(unsignedLongLong: UInt64(s.st_rdev))

TestFoundation/TestNSFileManager.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,33 @@ class TestNSFileManger : XCTestCase {
9696

9797
do {
9898
let attrs = try fm.attributesOfItemAtPath(path)
99-
// TODO: Actually verify the contents of the dictionary.
99+
100100
XCTAssertTrue(attrs.count > 0)
101+
102+
let fileSize = attrs[NSFileSize] as? NSNumber
103+
XCTAssertEqual(fileSize!.longLongValue, 0)
104+
105+
let fileModificationDate = attrs[NSFileModificationDate] as? NSDate
106+
XCTAssertGreaterThan(NSDate().timeIntervalSince1970, fileModificationDate!.timeIntervalSince1970)
107+
108+
let filePosixPermissions = attrs[NSFilePosixPermissions] as? NSNumber
109+
XCTAssertNotEqual(filePosixPermissions!.longLongValue, 0)
110+
111+
let fileReferenceCount = attrs[NSFileReferenceCount] as? NSNumber
112+
XCTAssertEqual(fileReferenceCount!.longLongValue, 1)
113+
114+
let fileSystemNumber = attrs[NSFileSystemNumber] as? NSNumber
115+
XCTAssertNotEqual(fileSystemNumber!.longLongValue, 0)
116+
117+
let fileSystemFileNumber = attrs[NSFileSystemFileNumber] as? NSNumber
118+
XCTAssertNotEqual(fileSystemFileNumber!.longLongValue, 0)
119+
120+
let fileType = attrs[NSFileType] as? String
121+
XCTAssertEqual(fileType!, NSFileTypeRegular)
122+
123+
let fileOwnerAccountID = attrs[NSFileOwnerAccountID] as? NSNumber
124+
XCTAssertNotEqual(fileOwnerAccountID!.longLongValue, 0)
125+
101126
} catch let err {
102127
XCTFail("\(err)")
103128
}

0 commit comments

Comments
 (0)