Skip to content

Commit 558c1d5

Browse files
authored
FileManager: improve compatibility for setAttributes
`setAttributes(_:ofItemAtPath:)` and friends should not `fatalError` when encountering an attribute which cannot be set. Instead, we should ignore those to match the behaviour of the reference implementation. This should allow us to properly enable some test cases in swift-driver.
1 parent e9fbfdd commit 558c1d5

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Sources/Foundation/FileManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ open class FileManager : NSObject {
450450
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteUnknown.rawValue)
451451

452452
default:
453-
fatalError("This attribute is unknown or cannot be set: \(attribute)")
453+
break
454454
}
455455
}
456456

Tests/Foundation/Tests/TestFileManager.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,24 @@ class TestFileManager : XCTestCase {
12591259
XCTAssertFalse(fm.contentsEqual(atPath: testDir1.path, andPath: testDir2.path))
12601260
}
12611261

1262+
func test_setInvalidFileAttributes() throws {
1263+
let path = "\(NSTemporaryDirectory())test_setInvalidFileAttributes\(NSUUID().uuidString)"
1264+
let FM = FileManager.default
1265+
1266+
try? FM.removeItem(atPath: path)
1267+
XCTAssertTrue(FM.createFile(atPath: path, contents: Data(), attributes: nil))
1268+
1269+
do {
1270+
try FM.setAttributes([
1271+
.size: NSNumber(value: Int16(16)),
1272+
.systemNumber: NSNumber(value: Int16(32)),
1273+
.systemFileNumber: NSNumber(value: Int16(48)),
1274+
], ofItemAtPath: path)
1275+
} catch {
1276+
XCTFail("\(error)")
1277+
}
1278+
}
1279+
12621280
func test_copyItemsPermissions() throws {
12631281
let fm = FileManager.default
12641282
let tmpDir = fm.temporaryDirectory.appendingPathComponent("test_copyItemsPermissions")
@@ -2024,6 +2042,7 @@ VIDEOS=StopgapVideos
20242042
("test_displayNames", test_displayNames),
20252043
("test_getItemReplacementDirectory", test_getItemReplacementDirectory),
20262044
("test_contentsEqual", test_contentsEqual),
2045+
("test_setInvalidFileAttributes", test_setInvalidFileAttributes),
20272046
/* ⚠️ */ ("test_replacement", testExpectedToFail(test_replacement,
20282047
/* ⚠️ */ "<https://bugs.swift.org/browse/SR-10819> Re-enable Foundation test TestFileManager.test_replacement")),
20292048
/* ⚠️ */("test_concurrentGetItemReplacementDirectory", testExpectedToFail(test_concurrentGetItemReplacementDirectory, "Intermittent SEGFAULT: rdar://84519512")),

0 commit comments

Comments
 (0)