@@ -269,7 +269,7 @@ class URLSessionHelperTests: XCTestCase {
269
269
}
270
270
271
271
// Capture a list of files in temp dirs, before calling the upload function.
272
- let tempFilesBeforeUpload = existingTempFiles ( )
272
+ let tempFilesBeforeUpload = try existingMultipartFormTempFiles ( )
273
273
274
274
// Perform upload HTTP request
275
275
let builder = try HTTPRequestBuilder ( url: URL ( string: " https://wordpress.org/upload " ) !)
@@ -278,7 +278,7 @@ class URLSessionHelperTests: XCTestCase {
278
278
let _ = await session. perform ( request: builder, errorType: TestError . self)
279
279
280
280
// Capture a list of files in the temp dirs, after calling the upload function.
281
- let tempFilesAfterUpload = existingTempFiles ( )
281
+ let tempFilesAfterUpload = try existingMultipartFormTempFiles ( )
282
282
283
283
// There should be no new files after the HTTP request returns. This assertion relies on an implementation detail
284
284
// where the multipart form content is put into a file in temp dirs.
@@ -299,7 +299,7 @@ class URLSessionHelperTests: XCTestCase {
299
299
}
300
300
301
301
// Capture a list of files in temp dirs, before calling the upload function.
302
- let tempFilesBeforeUpload = existingTempFiles ( )
302
+ let tempFilesBeforeUpload = try existingMultipartFormTempFiles ( )
303
303
304
304
// Perform upload HTTP request
305
305
let builder = try HTTPRequestBuilder ( url: URL ( string: " https://wordpress.org/upload " ) !)
@@ -308,28 +308,21 @@ class URLSessionHelperTests: XCTestCase {
308
308
let _ = await session. perform ( request: builder, errorType: TestError . self)
309
309
310
310
// Capture a list of files in the temp dirs, after calling the upload function.
311
- let tempFilesAfterUpload = existingTempFiles ( )
311
+ let tempFilesAfterUpload = try existingMultipartFormTempFiles ( )
312
312
313
313
// There should be no new files after the HTTP request returns. This assertion relies on an implementation detail
314
314
// where the multipart form content is put into a file in temp dirs.
315
315
let newFiles = tempFilesAfterUpload. subtracting ( tempFilesBeforeUpload)
316
316
XCTAssertEqual ( newFiles. count, 0 )
317
317
}
318
318
319
- private func existingTempFiles( ) -> Set < String > {
319
+ // This functions finds temp files that are used for uploading multipart form.
320
+ // The implementation relies on an internal implementation detail of building multipart form content.
321
+ private func existingMultipartFormTempFiles( ) throws -> Set < String > {
320
322
let fm = FileManager . default
321
- let enumerators = [
322
- fm. enumerator ( atPath: NSTemporaryDirectory ( ) ) ,
323
- fm. enumerator ( atPath: fm. temporaryDirectory. path)
324
- ] . compactMap { $0 }
325
-
326
- var result : Set < String > = [ ]
327
- for enumerator in enumerators {
328
- while let file = enumerator. nextObject ( ) as? String {
329
- result. insert ( file)
330
- }
331
- }
332
- return result
323
+ let files = try fm. contentsOfDirectory ( atPath: fm. temporaryDirectory. path)
324
+ . filter { UUID ( uuidString: $0) != nil }
325
+ return Set ( files)
333
326
}
334
327
335
328
private func createLargeFile( megaBytes: Int ) throws -> URL {
0 commit comments