Skip to content

Commit 90666a4

Browse files
committed
Create helper to post test events to fallback event handler
As part of the strategy for enabling interoperability, unhandled issues can be turned into events and sent to the fallback event handler. Also link against the _TestingInterop framework/library (depending on the platform), which provides a function to lookup the currently installed fallback event handler.
1 parent 00c0b1b commit 90666a4

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

Package.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,20 @@ let package = Package(
142142
exclude: ["CMakeLists.txt", "Testing.swiftcrossimport"],
143143
cxxSettings: .packageSettings,
144144
swiftSettings: .packageSettings + .enableLibraryEvolution(),
145-
linkerSettings: [
146-
.linkedLibrary("execinfo", .when(platforms: [.custom("freebsd"), .openbsd]))
147-
]
145+
linkerSettings: {
146+
var result = [LinkerSetting]()
147+
result += [
148+
.linkedLibrary("execinfo", .when(platforms: [.custom("freebsd"), .openbsd]))
149+
]
150+
#if compiler(>=6.3)
151+
result += [
152+
.linkedFramework("_TestingInterop", .whenApple()),
153+
.linkedLibrary("_TestingInterop", .whenApple(false)),
154+
]
155+
#endif
156+
157+
return result
158+
}()
148159
),
149160
.testTarget(
150161
name: "TestingTests",

Sources/Testing/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ add_library(Testing
3131
Attachments/Attachment.swift
3232
Events/Clock.swift
3333
Events/Event.swift
34+
Events/Event+FallbackHandler.swift
3435
Events/Recorder/Event.AdvancedConsoleOutputRecorder.swift
3536
Events/Recorder/Event.ConsoleOutputRecorder.swift
3637
Events/Recorder/Event.HumanReadableOutputRecorder.swift
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// This source file is part of the Swift.org open source project
3+
//
4+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
5+
// Licensed under Apache License v2.0 with Runtime Library Exception
6+
//
7+
// See https://swift.org/LICENSE.txt for license information
8+
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
//
10+
11+
private import _TestingInternals
12+
13+
extension Event {
14+
/// Post this event to the currently-installed fallback event handler.
15+
///
16+
/// - Parameters:
17+
/// - context: The context associated with this event.
18+
///
19+
/// - Returns: Whether or not the fallback event handler was invoked. If the
20+
/// currently-installed handler belongs to the testing library, returns
21+
/// `false`.
22+
borrowing func postToFallbackHandler(in context: borrowing Context) -> Bool {
23+
#if canImport(_TestingInterop)
24+
guard let fallbackEventHandler = _swift_testing_getFallbackEventHandler() else {
25+
return false
26+
}
27+
28+
// Encode the event as JSON and pass it to the handler.
29+
let encodeAndInvoke = ABI.CurrentVersion.eventHandler(encodeAsJSONLines: false) {
30+
recordJSON in
31+
fallbackEventHandler(
32+
String(describing: ABI.CurrentVersion.versionNumber),
33+
recordJSON.baseAddress!,
34+
recordJSON.count,
35+
nil
36+
)
37+
}
38+
encodeAndInvoke(self, context)
39+
return true
40+
#else
41+
return false
42+
#endif
43+
}
44+
}

Sources/_TestingInternals/include/Stubs.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,17 @@ static int swt_setfdflags(int fd, int flags) {
180180
}
181181
#endif
182182

183+
#if !SWT_NO_INTEROP
184+
185+
typedef void (*FallbackEventHandler)(const char *recordJSONSchemaVersionNumber,
186+
const void *recordJSONBaseAddress,
187+
long recordJSONByteCount,
188+
const void *_Nullable reserved);
189+
190+
FallbackEventHandler _Nullable _swift_testing_getFallbackEventHandler();
191+
192+
#endif
193+
183194
SWT_ASSUME_NONNULL_END
184195

185196
#endif

0 commit comments

Comments
 (0)