From 6f30bdba373bbd7fbfe241dddd732651f2fbd1e2 Mon Sep 17 00:00:00 2001 From: Brandon Williams <135203+mbrandonw@users.noreply.github.com> Date: Fri, 5 Apr 2024 12:21:21 -0700 Subject: [PATCH] Fix XCTFail. (#81) --- Sources/XCTestDynamicOverlay/XCTFail.swift | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Sources/XCTestDynamicOverlay/XCTFail.swift b/Sources/XCTestDynamicOverlay/XCTFail.swift index e4602e6a..5cb8a8d5 100644 --- a/Sources/XCTestDynamicOverlay/XCTFail.swift +++ b/Sources/XCTestDynamicOverlay/XCTFail.swift @@ -71,16 +71,25 @@ public struct XCTFailContext: Sendable { public func XCTFail(_ message: String = "", file: StaticString, line: UInt) { var message = message attachHostApplicationWarningIfNeeded(&message) - _XCTFailureHandler(nil, true, "\(file)", line, "\(message.isEmpty ? "failed" : message)", nil) + guard let handler = _XCTFailureHandler + else { + runtimeWarn(message) + return + } + handler(nil, true, "\(file)", line, "\(message.isEmpty ? "failed" : message)", nil) } private typealias XCTFailureHandler = @convention(c) ( AnyObject?, Bool, UnsafePointer, UInt, String, String? ) -> Void - private let _XCTFailureHandler = unsafeBitCast( - dlsym(dlopen(nil, RTLD_LAZY), "_XCTFailureHandler"), - to: XCTFailureHandler.self - ) + private let _XCTFailureHandler: XCTFailureHandler? = { + dlsym(dlopen(nil, RTLD_LAZY), "_XCTFailureHandler").map { pointer in + unsafeBitCast( + pointer, + to: XCTFailureHandler.self + ) + } + }() private func attachHostApplicationWarningIfNeeded(_ message: inout String) { guard