@@ -210,14 +210,17 @@ class TestFileManager : XCTestCase {
210
210
let fm = FileManager . default
211
211
let path = NSTemporaryDirectory ( ) + " test_isReadableFile \( NSUUID ( ) . uuidString) "
212
212
213
- XCTAssertTrue ( fm. createFile ( atPath: path, contents: Data ( ) ) )
214
-
215
213
do {
216
- let attrs = try fm. attributesOfItem ( atPath: path)
217
- let permissions = attrs [ FileAttributeKey . posixPermissions] as! UInt16
218
- let fileIsReadableFile = ( permissions & S_IRUSR == S_IRUSR)
219
- let fmIsReadableFile = fm. isReadableFile ( atPath: path)
220
- XCTAssertTrue ( fileIsReadableFile == fmIsReadableFile)
214
+ // create test file
215
+ XCTAssertTrue ( fm. createFile ( atPath: path, contents: Data ( ) ) )
216
+
217
+ // test unReadable if file has no permissions
218
+ try fm. setAttributes ( [ . posixPermissions : NSNumber ( value: Int16 ( 0o0000 ) ) ] , ofItemAtPath: path)
219
+ XCTAssertFalse ( fm. isReadableFile ( atPath: path) )
220
+
221
+ // test readable if file has read permissions
222
+ try fm. setAttributes ( [ . posixPermissions : NSNumber ( value: Int16 ( 0o0400 ) ) ] , ofItemAtPath: path)
223
+ XCTAssertTrue ( fm. isReadableFile ( atPath: path) )
221
224
} catch let e {
222
225
XCTFail ( " \( e) " )
223
226
}
@@ -227,14 +230,17 @@ class TestFileManager : XCTestCase {
227
230
let fm = FileManager . default
228
231
let path = NSTemporaryDirectory ( ) + " test_isWritableFile \( NSUUID ( ) . uuidString) "
229
232
230
- XCTAssertTrue ( fm. createFile ( atPath: path, contents: Data ( ) ) )
231
-
232
233
do {
233
- let attrs = try fm. attributesOfItem ( atPath: path)
234
- let permissions = attrs [ FileAttributeKey . posixPermissions] as! UInt16
235
- let fileIsWritableFile = ( permissions & S_IWUSR == S_IWUSR)
236
- let fmIsWritableFile = fm. isWritableFile ( atPath: path)
237
- XCTAssertTrue ( fileIsWritableFile == fmIsWritableFile)
234
+ // create test file
235
+ XCTAssertTrue ( fm. createFile ( atPath: path, contents: Data ( ) ) )
236
+
237
+ // test unWritable if file has no permissions
238
+ try fm. setAttributes ( [ . posixPermissions : NSNumber ( value: Int16 ( 0o0000 ) ) ] , ofItemAtPath: path)
239
+ XCTAssertFalse ( fm. isWritableFile ( atPath: path) )
240
+
241
+ // test writable if file has write permissions
242
+ try fm. setAttributes ( [ . posixPermissions : NSNumber ( value: Int16 ( 0o0200 ) ) ] , ofItemAtPath: path)
243
+ XCTAssertTrue ( fm. isWritableFile ( atPath: path) )
238
244
} catch let e {
239
245
XCTFail ( " \( e) " )
240
246
}
@@ -244,14 +250,17 @@ class TestFileManager : XCTestCase {
244
250
let fm = FileManager . default
245
251
let path = NSTemporaryDirectory ( ) + " test_isExecutableFile \( NSUUID ( ) . uuidString) "
246
252
247
- XCTAssertTrue ( fm. createFile ( atPath: path, contents: Data ( ) ) )
248
-
249
253
do {
250
- let attrs = try fm. attributesOfItem ( atPath: path)
251
- let permissions = attrs [ FileAttributeKey . posixPermissions] as! UInt16
252
- let fileIsExecutableFile = ( permissions & S_IXUSR == S_IXUSR)
253
- let fmIsExecutableFile = fm. isExecutableFile ( atPath: path)
254
- XCTAssertTrue ( fileIsExecutableFile == fmIsExecutableFile)
254
+ // create test file
255
+ XCTAssertTrue ( fm. createFile ( atPath: path, contents: Data ( ) ) )
256
+
257
+ // test unExecutable if file has no permissions
258
+ try fm. setAttributes ( [ . posixPermissions : NSNumber ( value: Int16 ( 0o0000 ) ) ] , ofItemAtPath: path)
259
+ XCTAssertFalse ( fm. isExecutableFile ( atPath: path) )
260
+
261
+ // test executable if file has execute permissions
262
+ try fm. setAttributes ( [ . posixPermissions : NSNumber ( value: Int16 ( 0o0100 ) ) ] , ofItemAtPath: path)
263
+ XCTAssertTrue ( fm. isExecutableFile ( atPath: path) )
255
264
} catch let e {
256
265
XCTFail ( " \( e) " )
257
266
}
0 commit comments