@@ -211,6 +211,129 @@ class TestFileManager : XCTestCase {
211
211
try ? fm. removeItem ( atPath: tmpDir. path)
212
212
}
213
213
214
+ func test_isReadableFile( ) {
215
+ let fm = FileManager . default
216
+ let path = NSTemporaryDirectory ( ) + " test_isReadableFile \( NSUUID ( ) . uuidString) "
217
+ defer {
218
+ try ? fm. removeItem ( atPath: path)
219
+ }
220
+
221
+ do {
222
+ // create test file
223
+ XCTAssertTrue ( fm. createFile ( atPath: path, contents: Data ( ) ) )
224
+
225
+ // test unReadable if file has no permissions
226
+ try fm. setAttributes ( [ . posixPermissions : NSNumber ( value: Int16 ( 0o0000 ) ) ] , ofItemAtPath: path)
227
+ #if os(Windows)
228
+ // Files are always readable on Windows
229
+ XCTAssertTrue ( fm. isReadableFile ( atPath: path) )
230
+ #else
231
+ XCTAssertFalse ( fm. isReadableFile ( atPath: path) )
232
+ #endif
233
+
234
+ // test readable if file has read permissions
235
+ try fm. setAttributes ( [ . posixPermissions : NSNumber ( value: Int16 ( 0o0400 ) ) ] , ofItemAtPath: path)
236
+ XCTAssertTrue ( fm. isReadableFile ( atPath: path) )
237
+ } catch let e {
238
+ XCTFail ( " \( e) " )
239
+ }
240
+ try ? fm. removeItem ( atPath: path)
241
+ }
242
+
243
+ func test_isWritableFile( ) {
244
+ let fm = FileManager . default
245
+ let path = NSTemporaryDirectory ( ) + " test_isWritableFile \( NSUUID ( ) . uuidString) "
246
+ defer {
247
+ try ? fm. removeItem ( atPath: path)
248
+ }
249
+
250
+ do {
251
+ // create test file
252
+ XCTAssertTrue ( fm. createFile ( atPath: path, contents: Data ( ) ) )
253
+
254
+ // test unWritable if file has no permissions
255
+ try fm. setAttributes ( [ . posixPermissions : NSNumber ( value: Int16 ( 0o0000 ) ) ] , ofItemAtPath: path)
256
+ XCTAssertFalse ( fm. isWritableFile ( atPath: path) )
257
+
258
+ // test writable if file has write permissions
259
+ try fm. setAttributes ( [ . posixPermissions : NSNumber ( value: Int16 ( 0o0200 ) ) ] , ofItemAtPath: path)
260
+ XCTAssertTrue ( fm. isWritableFile ( atPath: path) )
261
+ } catch let e {
262
+ XCTFail ( " \( e) " )
263
+ }
264
+ try ? fm. removeItem ( atPath: path)
265
+ }
266
+
267
+ func test_isExecutableFile( ) {
268
+ let fm = FileManager . default
269
+ let path = NSTemporaryDirectory ( ) + " test_isExecutableFile \( NSUUID ( ) . uuidString) "
270
+ let exePath = path + " .exe "
271
+ defer {
272
+ try ? fm. removeItem ( atPath: path)
273
+ try ? fm. removeItem ( atPath: exePath)
274
+ }
275
+
276
+ do {
277
+ // create test file
278
+ XCTAssertTrue ( fm. createFile ( atPath: path, contents: Data ( ) ) )
279
+
280
+ // test unExecutable if file has no permissions
281
+ try fm. setAttributes ( [ . posixPermissions : NSNumber ( value: Int16 ( 0o0000 ) ) ] , ofItemAtPath: path)
282
+ XCTAssertFalse ( fm. isExecutableFile ( atPath: path) )
283
+
284
+ #if os(Windows)
285
+ // test unExecutable even if file has an `exe` extension
286
+ try fm. copyItem ( atPath: path, toPath: exePath)
287
+ XCTAssertFalse ( fm. isExecutableFile ( atPath: exePath) )
288
+ #else
289
+ // test executable if file has execute permissions
290
+ try fm. setAttributes ( [ . posixPermissions : NSNumber ( value: Int16 ( 0o0100 ) ) ] , ofItemAtPath: path)
291
+ XCTAssertTrue ( fm. isExecutableFile ( atPath: path) )
292
+ #endif
293
+
294
+ // test against the test bundle itself
295
+ let testFoundationBinary = try XCTUnwrap ( testBundle ( ) . path ( forAuxiliaryExecutable: " TestFoundation " ) )
296
+ XCTAssertTrue ( fm. isExecutableFile ( atPath: testFoundationBinary) )
297
+ } catch let e {
298
+ XCTFail ( " \( e) " )
299
+ }
300
+ try ? fm. removeItem ( atPath: path)
301
+ }
302
+
303
+ func test_isDeletableFile( ) {
304
+ let fm = FileManager . default
305
+
306
+ do {
307
+ let dir_path = NSTemporaryDirectory ( ) + " /test_isDeletableFile_dir/ "
308
+ defer {
309
+ try ? fm. removeItem ( atPath: dir_path)
310
+ }
311
+ let file_path = dir_path + " test_isDeletableFile \( NSUUID ( ) . uuidString) "
312
+ // create test directory
313
+ try fm. createDirectory ( atPath: dir_path, withIntermediateDirectories: true )
314
+ // create test file
315
+ XCTAssertTrue ( fm. createFile ( atPath: file_path, contents: Data ( ) ) )
316
+
317
+ // test undeletable if parent directory has no permissions
318
+ try fm. setAttributes ( [ . posixPermissions : NSNumber ( value: Int16 ( 0o0000 ) ) ] , ofItemAtPath: dir_path)
319
+ XCTAssertFalse ( fm. isDeletableFile ( atPath: file_path) )
320
+
321
+ // test deletable if parent directory has all necessary permissions
322
+ try fm. setAttributes ( [ . posixPermissions : NSNumber ( value: Int16 ( 0o0755 ) ) ] , ofItemAtPath: dir_path)
323
+ XCTAssertTrue ( fm. isDeletableFile ( atPath: file_path) )
324
+
325
+ try ? fm. removeItem ( atPath: dir_path)
326
+ }
327
+ catch { XCTFail ( " \( error) " ) }
328
+
329
+ // test against known undeletable file
330
+ #if os(Windows)
331
+ XCTAssertFalse ( fm. isDeletableFile ( atPath: " NUL " ) )
332
+ #else
333
+ XCTAssertFalse ( fm. isDeletableFile ( atPath: " /dev/null " ) )
334
+ #endif
335
+ }
336
+
214
337
func test_fileAttributes( ) throws {
215
338
let fm = FileManager . default
216
339
let path = NSTemporaryDirectory ( ) + " test_fileAttributes \( NSUUID ( ) . uuidString) "
@@ -435,6 +558,7 @@ class TestFileManager : XCTestCase {
435
558
} else {
436
559
XCTFail ( )
437
560
}
561
+ try ? fm. removeItem ( atPath: basePath)
438
562
}
439
563
440
564
func test_directoryEnumerator( ) {
@@ -800,6 +924,7 @@ class TestFileManager : XCTestCase {
800
924
} catch {
801
925
// ignore
802
926
}
927
+ cleanup ( )
803
928
}
804
929
805
930
func test_linkItemAtPathToPath( ) {
0 commit comments