@@ -72,12 +72,68 @@ class TestFileManager : TestFileManagerSuper {
72
72
expectTrue ( threw, " Should have thrown " )
73
73
74
74
}
75
+
76
+ func testDirectoryEnumerator_error( ) {
77
+ let fm = FileManager . default
78
+ let nonexistantURL = URL ( fileURLWithPath: " \( NSTemporaryDirectory ( ) ) /nonexistant " )
79
+
80
+ var invoked = false
81
+ let e = fm. enumerator ( at: nonexistantURL, includingPropertiesForKeys: [ ] ) { ( url, err) in
82
+ invoked = true
83
+ expectEqual ( nonexistantURL, url)
84
+ expectEqual ( ( err as NSError ) . code, NSFileReadNoSuchFileError)
85
+ return true
86
+ }
87
+
88
+ let url = e? . nextObject ( )
89
+ expectTrue ( invoked)
90
+ expectTrue ( url == nil )
91
+
92
+ }
93
+
94
+ func testDirectoryEnumerator_error_noHandler( ) {
95
+ let fm = FileManager . default
96
+ let nonexistantURL = URL ( fileURLWithPath: " \( NSTemporaryDirectory ( ) ) /nonexistant " )
97
+
98
+ let e = fm. enumerator ( at: nonexistantURL, includingPropertiesForKeys: [ ] )
99
+ let url = e? . nextObject ( )
100
+ expectTrue ( url == nil )
101
+
102
+ }
103
+
104
+ func testDirectoryEnumerator_simple( ) {
105
+ let fm = FileManager . default
106
+ let dirPath = ( NSTemporaryDirectory ( ) as NSString ) . appendingPathComponent ( NSUUID ( ) . uuidString)
107
+ try ! fm. createDirectory ( atPath: dirPath, withIntermediateDirectories: true , attributes: nil )
108
+ defer { try ! FileManager . default. removeItem ( atPath: dirPath) }
109
+
110
+ let item1 = URL ( fileURLWithPath: " \( dirPath) /1 " , isDirectory: false )
111
+ let item2 = URL ( fileURLWithPath: " \( dirPath) /2 " , isDirectory: false )
112
+
113
+ try ! Data ( ) . write ( to: item1)
114
+ try ! Data ( ) . write ( to: item2)
115
+
116
+ let e = fm. enumerator ( at: URL ( fileURLWithPath: dirPath, isDirectory: true ) , includingPropertiesForKeys: [ ] )
117
+ let result1 = e? . nextObject ( )
118
+ let result2 = e? . nextObject ( )
119
+ let result3 = e? . nextObject ( )
120
+
121
+ // Avoid potential symlink discrepancy between the result and the original URL
122
+ expectEqual ( ( result1! as! URL ) . lastPathComponent, item1. lastPathComponent)
123
+ expectEqual ( ( result2! as! URL ) . lastPathComponent, item2. lastPathComponent)
124
+ expectTrue ( result3 == nil )
125
+
126
+ }
127
+
75
128
}
76
129
77
130
#if !FOUNDATION_XCTEST
78
131
var FMTests = TestSuite ( " TestFileManager " )
79
132
FMTests . test ( " testReplaceItem " ) { TestFileManager ( ) . testReplaceItem ( ) }
80
133
FMTests . test ( " testReplaceItem_error " ) { TestFileManager ( ) . testReplaceItem_error ( ) }
134
+ FMTests . test ( " testDirectoryEnumerator_error " ) { TestFileManager ( ) . testDirectoryEnumerator_error ( ) }
135
+ FMTests . test ( " testDirectoryEnumerator_error_noHandler " ) { TestFileManager ( ) . testDirectoryEnumerator_error_noHandler ( ) }
136
+ FMTests . test ( " testDirectoryEnumerator_simple " ) { TestFileManager ( ) . testDirectoryEnumerator_simple ( ) }
81
137
82
138
runAllTests ( )
83
139
#endif
0 commit comments