@@ -16,6 +16,10 @@ class TestFileManager : XCTestCase {
16
16
( " test_moveFile " , test_moveFile) ,
17
17
( " test_fileSystemRepresentation " , test_fileSystemRepresentation) ,
18
18
( " test_fileExists " , test_fileExists) ,
19
+ ( " test_isReadableFile " , test_isReadableFile) ,
20
+ ( " test_isWritableFile " , test_isWritableFile) ,
21
+ ( " test_isExecutableFile " , test_isExecutableFile) ,
22
+ ( " test_isDeletableFile " , test_isDeletableFile) ,
19
23
( " test_fileAttributes " , test_fileAttributes) ,
20
24
( " test_fileSystemAttributes " , test_fileSystemAttributes) ,
21
25
( " test_setFileAttributes " , test_setFileAttributes) ,
@@ -202,6 +206,91 @@ class TestFileManager : XCTestCase {
202
206
ignoreError { try fm. removeItem ( atPath: tmpDir. path) }
203
207
}
204
208
209
+ func test_isReadableFile( ) {
210
+ let fm = FileManager . default
211
+ let path = NSTemporaryDirectory ( ) + " test_isReadableFile \( NSUUID ( ) . uuidString) "
212
+
213
+ do {
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) )
224
+ } catch let e {
225
+ XCTFail ( " \( e) " )
226
+ }
227
+ }
228
+
229
+ func test_isWritableFile( ) {
230
+ let fm = FileManager . default
231
+ let path = NSTemporaryDirectory ( ) + " test_isWritableFile \( NSUUID ( ) . uuidString) "
232
+
233
+ do {
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) )
244
+ } catch let e {
245
+ XCTFail ( " \( e) " )
246
+ }
247
+ }
248
+
249
+ func test_isExecutableFile( ) {
250
+ let fm = FileManager . default
251
+ let path = NSTemporaryDirectory ( ) + " test_isExecutableFile \( NSUUID ( ) . uuidString) "
252
+
253
+ do {
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) )
264
+ } catch let e {
265
+ XCTFail ( " \( e) " )
266
+ }
267
+ }
268
+
269
+ func test_isDeletableFile( ) {
270
+ let fm = FileManager . default
271
+
272
+ do {
273
+ let dir_path = NSTemporaryDirectory ( ) + " /test_isDeletableFile_dir/ "
274
+ let file_path = dir_path + " test_isDeletableFile \( NSUUID ( ) . uuidString) "
275
+ // create test directory
276
+ try fm. createDirectory ( atPath: dir_path, withIntermediateDirectories: true )
277
+ // create test file
278
+ XCTAssertTrue ( fm. createFile ( atPath: file_path, contents: Data ( ) ) )
279
+
280
+ // test undeletable if parent directory has no permissions
281
+ try fm. setAttributes ( [ . posixPermissions : NSNumber ( value: Int16 ( 0o0000 ) ) ] , ofItemAtPath: dir_path)
282
+ XCTAssertFalse ( fm. isDeletableFile ( atPath: file_path) )
283
+
284
+ // test deletable if parent directory has all necessary permissions
285
+ try fm. setAttributes ( [ . posixPermissions : NSNumber ( value: Int16 ( 0o0755 ) ) ] , ofItemAtPath: dir_path)
286
+ XCTAssertTrue ( fm. isDeletableFile ( atPath: file_path) )
287
+ }
288
+ catch { XCTFail ( " \( error) " ) }
289
+
290
+ // test against known undeletable file
291
+ XCTAssertFalse ( fm. isDeletableFile ( atPath: " /dev/null " ) )
292
+ }
293
+
205
294
func test_fileAttributes( ) {
206
295
let fm = FileManager . default
207
296
let path = NSTemporaryDirectory ( ) + " test_fileAttributes \( NSUUID ( ) . uuidString) "
0 commit comments