Skip to content

Commit ff552e0

Browse files
authored
Fix looking up multipart form temporary files (#781)
2 parents 5bf57f8 + a7f9297 commit ff552e0

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

Tests/WordPressKitTests/Tests/Utilities/URLSessionHelperTests.swift

+10-17
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ class URLSessionHelperTests: XCTestCase {
269269
}
270270

271271
// Capture a list of files in temp dirs, before calling the upload function.
272-
let tempFilesBeforeUpload = existingTempFiles()
272+
let tempFilesBeforeUpload = try existingMultipartFormTempFiles()
273273

274274
// Perform upload HTTP request
275275
let builder = try HTTPRequestBuilder(url: URL(string: "https://wordpress.org/upload")!)
@@ -278,7 +278,7 @@ class URLSessionHelperTests: XCTestCase {
278278
let _ = await session.perform(request: builder, errorType: TestError.self)
279279

280280
// Capture a list of files in the temp dirs, after calling the upload function.
281-
let tempFilesAfterUpload = existingTempFiles()
281+
let tempFilesAfterUpload = try existingMultipartFormTempFiles()
282282

283283
// There should be no new files after the HTTP request returns. This assertion relies on an implementation detail
284284
// where the multipart form content is put into a file in temp dirs.
@@ -299,7 +299,7 @@ class URLSessionHelperTests: XCTestCase {
299299
}
300300

301301
// Capture a list of files in temp dirs, before calling the upload function.
302-
let tempFilesBeforeUpload = existingTempFiles()
302+
let tempFilesBeforeUpload = try existingMultipartFormTempFiles()
303303

304304
// Perform upload HTTP request
305305
let builder = try HTTPRequestBuilder(url: URL(string: "https://wordpress.org/upload")!)
@@ -308,28 +308,21 @@ class URLSessionHelperTests: XCTestCase {
308308
let _ = await session.perform(request: builder, errorType: TestError.self)
309309

310310
// Capture a list of files in the temp dirs, after calling the upload function.
311-
let tempFilesAfterUpload = existingTempFiles()
311+
let tempFilesAfterUpload = try existingMultipartFormTempFiles()
312312

313313
// There should be no new files after the HTTP request returns. This assertion relies on an implementation detail
314314
// where the multipart form content is put into a file in temp dirs.
315315
let newFiles = tempFilesAfterUpload.subtracting(tempFilesBeforeUpload)
316316
XCTAssertEqual(newFiles.count, 0)
317317
}
318318

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> {
320322
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)
333326
}
334327

335328
private func createLargeFile(megaBytes: Int) throws -> URL {

0 commit comments

Comments
 (0)