@@ -343,93 +343,153 @@ class TestFileManager : XCTestCase {
343
343
try fm. createDirectory ( atPath: basePath, withIntermediateDirectories: false , attributes: nil )
344
344
try fm. createDirectory ( atPath: basePath2, withIntermediateDirectories: false , attributes: nil )
345
345
346
- let _ = fm. createFile ( atPath: itemPath, contents: Data ( ) , attributes: nil )
347
- let _ = fm. createFile ( atPath: itemPath2, contents: Data ( ) , attributes: nil )
346
+ let _ = fm. createFile ( atPath: itemPath, contents: Data ( count : 123 ) , attributes: nil )
347
+ let _ = fm. createFile ( atPath: itemPath2, contents: Data ( count : 456 ) , attributes: nil )
348
348
349
349
} catch _ {
350
350
XCTFail ( )
351
351
}
352
-
352
+
353
+ var item1FileAttributes : [ FileAttributeKey : Any ] !
354
+ var item2FileAttributes : [ FileAttributeKey : Any ] !
353
355
if let e = FileManager . default. enumerator ( atPath: basePath) {
356
+ let attrs = e. directoryAttributes
357
+ XCTAssertNotNil ( attrs)
358
+ XCTAssertEqual ( attrs ? [ . type] as? FileAttributeType , . typeDirectory)
359
+
354
360
var foundItems = Set < String > ( )
355
361
while let item = e. nextObject ( ) as? String {
356
362
foundItems. insert ( item)
363
+ if item == " item " {
364
+ item1FileAttributes = e. fileAttributes
365
+ } else if item == " path2/item " {
366
+ item2FileAttributes = e. fileAttributes
367
+ }
357
368
}
358
369
XCTAssertEqual ( foundItems, Set ( [ " item " , " path2 " , " path2/item " ] ) )
359
370
} else {
360
371
XCTFail ( )
361
372
}
362
373
374
+ XCTAssertNotNil ( item1FileAttributes)
375
+ if let size = item1FileAttributes [ . size] as? NSNumber {
376
+ XCTAssertEqual ( size. int64Value, 123 )
377
+ } else {
378
+ XCTFail ( " Cant get file size for 'item' " )
379
+ }
380
+
381
+ XCTAssertNotNil ( item2FileAttributes)
382
+ if let size = item2FileAttributes [ . size] as? NSNumber {
383
+ XCTAssertEqual ( size. int64Value, 456 )
384
+ } else {
385
+ XCTFail ( " Cant get file size for 'path2/item' " )
386
+ }
387
+
388
+ if let e2 = FileManager . default. enumerator ( atPath: basePath) {
389
+ var foundItems = Set < String > ( )
390
+ while let item = e2. nextObject ( ) as? String {
391
+ foundItems. insert ( item)
392
+ if item == " path2 " {
393
+ e2. skipDescendants ( )
394
+ XCTAssertEqual ( e2. level, 1 )
395
+ XCTAssertNotNil ( e2. fileAttributes)
396
+ }
397
+ }
398
+ XCTAssertEqual ( foundItems, Set ( [ " item " , " path2 " ] ) )
399
+ } else {
400
+ XCTFail ( )
401
+ }
363
402
}
364
403
365
404
func test_directoryEnumerator( ) {
366
405
let fm = FileManager . default
367
- let testDirName = " testdir \( NSUUID ( ) . uuidString) "
368
- let path = NSTemporaryDirectory ( ) + " \( testDirName) "
369
- let itemPath = NSTemporaryDirectory ( ) + " \( testDirName) /item "
370
-
371
- ignoreError { try fm. removeItem ( atPath: path) }
372
-
373
- do {
374
- try fm. createDirectory ( atPath: path, withIntermediateDirectories: false , attributes: nil )
375
- let _ = fm. createFile ( atPath: itemPath, contents: Data ( ) , attributes: nil )
376
- } catch _ {
377
- XCTFail ( )
406
+ let basePath = NSTemporaryDirectory ( ) + " testdir \( NSUUID ( ) . uuidString) / "
407
+ let subDirs1 = basePath + " subdir1/subdir2/.hiddenDir/subdir3/ "
408
+ let subDirs2 = basePath + " subdir1/subdir2/subdir4.app/subdir5./.subdir6.ext/subdir7.ext./ "
409
+ let itemPath1 = basePath + " itemFile1 "
410
+ let itemPath2 = subDirs1 + " itemFile2. "
411
+ let itemPath3 = subDirs1 + " itemFile3.ext. "
412
+ let hiddenItem1 = basePath + " .hiddenFile1 "
413
+ let hiddenItem2 = subDirs1 + " .hiddenFile2 "
414
+ let hiddenItem3 = subDirs2 + " .hiddenFile3 "
415
+ let hiddenItem4 = subDirs2 + " .hiddenFile4.ext "
416
+
417
+ var fileLevels : [ String : Int ] = [
418
+ " itemFile1 " : 1 ,
419
+ " .hiddenFile1 " : 1 ,
420
+ " subdir1 " : 1 ,
421
+ " subdir2 " : 2 ,
422
+ " subdir4.app " : 3 ,
423
+ " subdir5. " : 4 ,
424
+ " .subdir6.ext " : 5 ,
425
+ " subdir7.ext. " : 6 ,
426
+ " .hiddenFile4.ext " : 7 ,
427
+ " .hiddenFile3 " : 7 ,
428
+ " .hiddenDir " : 3 ,
429
+ " subdir3 " : 4 ,
430
+ " itemFile3.ext. " : 5 ,
431
+ " itemFile2. " : 5 ,
432
+ " .hiddenFile2 " : 5
433
+ ]
434
+
435
+ func directoryItems( options: FileManager . DirectoryEnumerationOptions ) -> [ String : Int ] ? {
436
+ if let e = FileManager . default. enumerator ( at: URL ( fileURLWithPath: basePath) , includingPropertiesForKeys: nil , options: options, errorHandler: nil ) {
437
+ var foundItems = [ String: Int] ( )
438
+ while let item = e. nextObject ( ) as? URL {
439
+ foundItems [ item. lastPathComponent] = e. level
440
+ }
441
+ return foundItems
442
+ } else {
443
+ return nil
444
+ }
378
445
}
379
446
380
- if let e = FileManager . default. enumerator ( at: URL ( fileURLWithPath: path) , includingPropertiesForKeys: nil , options: [ ] , errorHandler: nil ) {
381
- var foundItems = [ String: Int] ( )
382
- while let item = e. nextObject ( ) as? URL {
383
- foundItems [ item. path] = e. level
447
+ ignoreError { try fm. removeItem ( atPath: basePath) }
448
+ defer { ignoreError { try fm. removeItem ( atPath: basePath) } }
449
+
450
+ XCTAssertNotNil ( try ? fm. createDirectory ( atPath: subDirs1, withIntermediateDirectories: true , attributes: nil ) )
451
+ XCTAssertNotNil ( try ? fm. createDirectory ( atPath: subDirs2, withIntermediateDirectories: true , attributes: nil ) )
452
+ for filename in [ itemPath1, itemPath2, itemPath3, hiddenItem1, hiddenItem2, hiddenItem3, hiddenItem4] {
453
+ XCTAssertTrue ( fm. createFile ( atPath: filename, contents: Data ( ) , attributes: nil ) , " Cant create file ' \( filename) ' " )
454
+ }
455
+
456
+ if let foundItems = directoryItems ( options: [ ] ) {
457
+ XCTAssertEqual ( foundItems. count, fileLevels. count)
458
+ for (name, level) in foundItems {
459
+ XCTAssertEqual ( fileLevels [ name] , level, " File level for \( name) is wrong " )
384
460
}
385
- XCTAssertEqual ( foundItems [ itemPath] , 1 )
386
461
} else {
387
- XCTFail ( )
462
+ XCTFail ( " Cant enumerate directory at \( basePath ) with options: [] " )
388
463
}
389
-
390
- let subDirPath = NSTemporaryDirectory ( ) + " \( testDirName) /testdir2 "
391
- let subDirItemPath = NSTemporaryDirectory ( ) + " \( testDirName) /testdir2/item "
392
- do {
393
- try fm. createDirectory ( atPath: subDirPath, withIntermediateDirectories: false , attributes: nil )
394
- let _ = fm. createFile ( atPath: subDirItemPath, contents: Data ( ) , attributes: nil )
395
- } catch _ {
396
- XCTFail ( )
464
+
465
+ if let foundItems = directoryItems ( options: [ . skipsHiddenFiles] ) {
466
+ XCTAssertEqual ( foundItems. count, 5 )
467
+ } else {
468
+ XCTFail ( " Cant enumerate directory at \( basePath) with options: [.skipsHiddenFiles] " )
397
469
}
398
-
399
- if let e = FileManager . default. enumerator ( at: URL ( fileURLWithPath: path) , includingPropertiesForKeys: nil , options: [ ] , errorHandler: nil ) {
400
- var foundItems = [ String: Int] ( )
401
- while let item = e. nextObject ( ) as? URL {
402
- foundItems [ item. path] = e. level
403
- }
404
- XCTAssertEqual ( foundItems [ itemPath] , 1 )
405
- XCTAssertEqual ( foundItems [ subDirPath] , 1 )
406
- XCTAssertEqual ( foundItems [ subDirItemPath] , 2 )
470
+
471
+ if let foundItems = directoryItems ( options: [ . skipsSubdirectoryDescendants] ) {
472
+ XCTAssertEqual ( foundItems. count, 3 )
407
473
} else {
408
- XCTFail ( )
474
+ XCTFail ( " Cant enumerate directory at \( basePath ) with options: [.skipsSubdirectoryDescendants] " )
409
475
}
410
-
411
- if let e = FileManager . default. enumerator ( at: URL ( fileURLWithPath: path) , includingPropertiesForKeys: nil , options: [ . skipsSubdirectoryDescendants] , errorHandler: nil ) {
412
- var foundItems = [ String: Int] ( )
413
- while let item = e. nextObject ( ) as? URL {
414
- foundItems [ item. path] = e. level
415
- }
416
- XCTAssertEqual ( foundItems [ itemPath] , 1 )
417
- XCTAssertEqual ( foundItems [ subDirPath] , 1 )
476
+
477
+ if let foundItems = directoryItems ( options: [ . skipsHiddenFiles, . skipsSubdirectoryDescendants] ) {
478
+ XCTAssertEqual ( foundItems. count, 2 )
418
479
} else {
419
- XCTFail ( )
480
+ XCTFail ( " Cant enumerate directory at \( basePath ) with options: [.skipsHiddenFiles, .skipsSubdirectoryDescendants] " )
420
481
}
421
-
422
- if let e = FileManager . default. enumerator ( at: URL ( fileURLWithPath: path) , includingPropertiesForKeys: nil , options: [ ] , errorHandler: nil ) {
423
- var foundItems = [ String: Int] ( )
424
- while let item = e. nextObject ( ) as? URL {
425
- foundItems [ item. path] = e. level
426
- }
427
- XCTAssertEqual ( foundItems [ itemPath] , 1 )
428
- XCTAssertEqual ( foundItems [ subDirPath] , 1 )
482
+
483
+ if let foundItems = directoryItems ( options: [ . skipsPackageDescendants] ) {
484
+ #if DARWIN_COMPATIBILITY_TESTS
485
+ XCTAssertEqual ( foundItems. count, 10 ) // Only native Foundation does not gnore .skipsPackageDescendants
486
+ #else
487
+ XCTAssertEqual ( foundItems. count, 15 )
488
+ #endif
429
489
} else {
430
- XCTFail ( )
490
+ XCTFail ( " Cant enumerate directory at \( basePath ) with options: [.skipsHiddenFiles, .skipsSubdirectoryDescendants] " )
431
491
}
432
-
492
+
433
493
var didGetError = false
434
494
let handler : ( URL , Error ) -> Bool = { ( URL, Error) in
435
495
didGetError = true
@@ -443,21 +503,13 @@ class TestFileManager : XCTestCase {
443
503
XCTAssertTrue ( didGetError)
444
504
445
505
do {
446
- let contents = try FileManager . default. contentsOfDirectory ( at: URL ( fileURLWithPath: path ) , includingPropertiesForKeys: nil , options: [ ] ) . map {
506
+ let contents = try FileManager . default. contentsOfDirectory ( at: URL ( fileURLWithPath: basePath ) , includingPropertiesForKeys: nil , options: [ ] ) . map {
447
507
return $0. path
448
508
}
449
- XCTAssertEqual ( contents. count, 2 )
450
- XCTAssertTrue ( contents. contains ( itemPath) )
451
- XCTAssertTrue ( contents. contains ( subDirPath) )
509
+ XCTAssertEqual ( contents. count, 3 )
452
510
} catch {
453
511
XCTFail ( )
454
512
}
455
-
456
- do {
457
- try fm. removeItem ( atPath: path)
458
- } catch {
459
- XCTFail ( " Failed to clean up files " )
460
- }
461
513
}
462
514
463
515
func test_contentsOfDirectoryAtPath( ) {
0 commit comments