24 lines
697 B
Swift
24 lines
697 B
Swift
import Foundation
|
|
|
|
// swiftlint:disable force_try
|
|
|
|
/// Helper to create a temporary directory for running tests in.
|
|
///
|
|
/// The temporary directory will be removed after the operation has ran.
|
|
///
|
|
/// - Parameters:
|
|
/// - operation: The operation to run with the temporary directory.
|
|
public func withTemporaryDirectory(
|
|
_ operation: (URL) async throws -> Void
|
|
) async rethrows {
|
|
let tempUrl = FileManager.default
|
|
.temporaryDirectory
|
|
.appendingPathComponent(UUID().uuidString)
|
|
|
|
try! FileManager.default.createDirectory(at: tempUrl, withIntermediateDirectories: false)
|
|
try await operation(tempUrl)
|
|
try! FileManager.default.removeItem(at: tempUrl)
|
|
}
|
|
|
|
// swiftlint:enable force_try
|