Skip to content

Commit 988b54d

Browse files
committed
Interop: send events to fallback event handler if active configuration not present
WIP, needs some additional work to get build working locally. Although the `_swift_testing_getFallbackEventHandler` is forward declared, I don't think it will be present at link time since we're not linking against _TestingInterop.
1 parent 00c0b1b commit 988b54d

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

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)