Skip to content

Commit e722d1a

Browse files
weichselphausler
authored andcommitted
Use the file attribute dictionary passed to createFile (#1112)
* Use file attribute dictionary passed to createFile * Test file creation with an attribute dictionary
1 parent 680225c commit e722d1a

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Foundation/FileManager.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,9 @@ open class FileManager : NSObject {
627627
open func createFile(atPath path: String, contents data: Data?, attributes attr: [FileAttributeKey : Any]? = nil) -> Bool {
628628
do {
629629
try (data ?? Data()).write(to: URL(fileURLWithPath: path), options: .atomic)
630+
if let attr = attr {
631+
try self.setAttributes(attr, ofItemAtPath: path)
632+
}
630633
return true
631634
} catch _ {
632635
return false

TestFoundation/TestFileManager.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,29 @@ class TestFileManager : XCTestCase {
8484
} catch {
8585
XCTFail("Failed to clean up file")
8686
}
87+
88+
let permissions = NSNumber(value: Int16(0o753))
89+
let attributes = [FileAttributeKey.posixPermissions: permissions]
90+
XCTAssertTrue(fm.createFile(atPath: path, contents: Data(),
91+
attributes: attributes))
92+
guard let retrievedAtributes = try? fm.attributesOfItem(atPath: path) else {
93+
XCTFail("Failed to retrieve file attributes from created file")
94+
return
95+
}
96+
97+
XCTAssertTrue(retrievedAtributes.contains(where: { (attribute) -> Bool in
98+
guard let attributeValue = attribute.value as? NSNumber else {
99+
return false
100+
}
101+
return (attribute.key == .posixPermissions)
102+
&& (attributeValue == permissions)
103+
}))
104+
105+
do {
106+
try fm.removeItem(atPath: path)
107+
} catch {
108+
XCTFail("Failed to clean up file")
109+
}
87110
}
88111

89112
func test_moveFile() {

0 commit comments

Comments
 (0)