Skip to content

Commit 3c4b16e

Browse files
committed
Factor out the new code to its own file
1 parent 0b81a36 commit 3c4b16e

File tree

5 files changed

+98
-73
lines changed

5 files changed

+98
-73
lines changed

Sources/Testing/ABI/Encoded/ABI.EncodedIssue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ extension Issue {
112112
// TODO: improve fidelity of issue kind reporting (especially those without associated values)
113113
.unconditional
114114
}
115-
let severity: Issue.Severity = switch issue._severity {
115+
let severity: Issue.Severity = switch issue.severity {
116116
case .warning:
117117
.warning
118118
case nil, .error:

Sources/Testing/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ add_library(Testing
2929
Attachments/Attachment.swift
3030
Events/Clock.swift
3131
Events/Event.swift
32+
Events/Event+FallbackHandler.swift
3233
Events/Recorder/Event.AdvancedConsoleOutputRecorder.swift
3334
Events/Recorder/Event.ConsoleOutputRecorder.swift
3435
Events/Recorder/Event.HumanReadableOutputRecorder.swift
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 _Testing_ExperimentalInfrastructure
12+
13+
extension Event {
14+
/// The implementation of ``fallbackEventHandler``.
15+
///
16+
/// - Parameters:
17+
/// - abi: The ABI version to use for decoding `recordJSON`.
18+
/// - recordJSON: The JSON encoding of an event record.
19+
///
20+
/// - Throws: Any error that prevented handling the encoded record.
21+
private static func _fallbackEventHandler<V>(_ abi: V.Type, _ recordJSON: UnsafeRawBufferPointer) throws where V: ABI.Version {
22+
let record = try JSON.decode(ABI.Record<ABI.CurrentVersion>.self, from: recordJSON)
23+
guard case let .event(event) = record.kind else {
24+
return
25+
}
26+
switch event.kind {
27+
case .issueRecorded:
28+
Issue(event)?.record()
29+
case .valueAttached:
30+
if let attachment = event.attachment {
31+
Attachment.record(attachment, sourceLocation: attachment._sourceLocation ?? .__here())
32+
}
33+
default:
34+
// Not handled here.
35+
break
36+
}
37+
}
38+
39+
/// The fallback event handler to set when Swift Testing is the active testing
40+
/// library.
41+
private static let _fallbackEventHandler: FallbackEventHandler = { recordJSONSchemaVersionNumber, recordJSONBaseAddress, recordJSONByteCount, _ in
42+
let abi = String(validatingCString: recordJSONSchemaVersionNumber)
43+
.flatMap(VersionNumber.init)
44+
.flatMap(ABI.version(forVersionNumber:))
45+
if let abi {
46+
let recordJSON = UnsafeRawBufferPointer(start: recordJSONBaseAddress, count: recordJSONByteCount)
47+
try! Self._fallbackEventHandler(abi, recordJSON)
48+
}
49+
}
50+
51+
/// The implementation of ``installFallbackEventHandler()``.
52+
private static let _installFallbackHandler: Bool = {
53+
_Testing_ExperimentalInfrastructure.installFallbackEventHandler(Self._fallbackEventHandler)
54+
}()
55+
56+
/// Install the testing library's fallback event handler.
57+
///
58+
/// - Returns: Whether or not the handler was installed.
59+
static func installFallbackHandler() -> Bool {
60+
_installFallbackHandler
61+
}
62+
63+
/// Post this event to the currently-installed fallback event handler.
64+
///
65+
/// - Parameters:
66+
/// - context: The context associated with this event.
67+
///
68+
/// - Returns: Whether or not the fallback event handler was invoked. If the
69+
/// currently-installed handler belongs to the testing library, returns
70+
/// `false`.
71+
borrowing func postToFallbackHandler(in context: borrowing Context) -> Bool {
72+
guard let fallbackEventHandler = _Testing_ExperimentalInfrastructure.fallbackEventHandler() else {
73+
// No fallback event handler is installed.
74+
return false
75+
}
76+
if castCFunction(fallbackEventHandler, to: UnsafeRawPointer.self) == castCFunction(Self._fallbackEventHandler, to: UnsafeRawPointer.self) {
77+
// The fallback event handler belongs to Swift Testing, so we don't want
78+
// to call it on our own behalf.
79+
return false
80+
}
81+
82+
// Encode the event as JSON and pass it to the handler.
83+
let encodeAndInvoke = ABI.CurrentVersion.eventHandler(encodeAsJSONLines: false) { recordJSON in
84+
fallbackEventHandler(
85+
String(describing: ABI.CurrentVersion.versionNumber),
86+
recordJSON.baseAddress!,
87+
recordJSON.count,
88+
nil
89+
)
90+
}
91+
encodeAndInvoke(self, context)
92+
return true
93+
}
94+
}

Sources/Testing/Events/Event.swift

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
99
//
1010

11-
internal import _Testing_ExperimentalInfrastructure
12-
1311
/// An event that occurred during testing.
1412
@_spi(ForToolsIntegrationOnly)
1513
public struct Event: Sendable {
@@ -272,60 +270,6 @@ extension Event {
272270
}
273271
}
274272

275-
/// The implementation of ``fallbackEventHandler``.
276-
///
277-
/// - Parameters:
278-
/// - abi: The ABI version to use for decoding `recordJSON`.
279-
/// - recordJSON: The JSON encoding of an event record.
280-
///
281-
/// - Throws: Any error that prevented handling the encoded record.
282-
private static func _fallbackEventHandler<V>(_ abi: V.Type, _ recordJSON: UnsafeRawBufferPointer) throws where V: ABI.Version {
283-
let record = try JSON.decode(ABI.Record<ABI.CurrentVersion>.self, from: recordJSON)
284-
guard case let .event(event) = record.kind else {
285-
return
286-
}
287-
switch event.kind {
288-
case .issueRecorded:
289-
Issue(event)?.record()
290-
case .valueAttached:
291-
if let attachment = event.attachment {
292-
Attachment.record(attachment, sourceLocation: attachment._sourceLocation ?? .__here())
293-
}
294-
default:
295-
// Not handled here.
296-
break
297-
}
298-
}
299-
300-
/// The fallback event handler to set when Swift Testing is the active testing
301-
/// library.
302-
///
303-
/// ## See Also
304-
///
305-
/// - `swift_testing_getFallbackEventHandler()`
306-
/// - `swift_testing_setFallbackEventHandler()`
307-
static let fallbackEventHandler: FallbackEventHandler = { recordJSONSchemaVersionNumber, recordJSONBaseAddress, recordJSONByteCount, _ in
308-
let abi = String(validatingCString: recordJSONSchemaVersionNumber)
309-
.flatMap(VersionNumber.init)
310-
.flatMap(ABI.version(forVersionNumber:))
311-
if let abi {
312-
let recordJSON = UnsafeRawBufferPointer(start: recordJSONBaseAddress, count: recordJSONByteCount)
313-
try! Self._fallbackEventHandler(abi, recordJSON)
314-
}
315-
}
316-
317-
/// The implementation of ``installFallbackEventHandler()``.
318-
private static let _installFallbackHandler: Bool = {
319-
_Testing_ExperimentalInfrastructure.installFallbackEventHandler(Self.fallbackEventHandler)
320-
}()
321-
322-
/// Install the testing library's fallback event handler.
323-
///
324-
/// - Returns: Whether or not the handler was installed.
325-
static func installFallbackHandler() -> Bool {
326-
_installFallbackHandler
327-
}
328-
329273
/// Post this event to the currently-installed event handler.
330274
///
331275
/// - Parameters:
@@ -348,19 +292,8 @@ extension Event {
348292
if configuration.eventHandlingOptions.shouldHandleEvent(self) {
349293
configuration.handleEvent(self, in: context)
350294
}
351-
} else if let fallbackEventHandler = _Testing_ExperimentalInfrastructure.fallbackEventHandler(),
352-
castCFunction(fallbackEventHandler, to: UnsafeRawPointer.self) != castCFunction(Self.fallbackEventHandler, to: UnsafeRawPointer.self) {
353-
// Some library other than Swift Testing has set a fallback event handler.
354-
// Encode the event as JSON and call it.
355-
let fallbackEventHandler = ABI.CurrentVersion.eventHandler(encodeAsJSONLines: false) { recordJSON in
356-
fallbackEventHandler(
357-
String(describing: ABI.CurrentVersion.versionNumber),
358-
recordJSON.baseAddress!,
359-
recordJSON.count,
360-
nil
361-
)
362-
}
363-
fallbackEventHandler(self, context)
295+
} else if postToFallbackHandler(in: context) {
296+
// The fallback event handler handled this event.
364297
} else {
365298
// The current task does NOT have an associated configuration. This event
366299
// will be lost! Post it to every registered event handler to avoid that.

Sources/_Testing_ExperimentalInfrastructure/FallbackEventHandler.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ private import _TestingInternals
1919
/// - recordJSONBaseAddress: A pointer to the first byte of the encoded event.
2020
/// - recordJSONByteCount: The size of the encoded event in bytes.
2121
/// - reserved: Reserved for future use.
22-
@usableFromInline
2322
package typealias FallbackEventHandler = @Sendable @convention(c) (
2423
_ recordJSONSchemaVersionNumber: UnsafePointer<CChar>,
2524
_ recordJSONBaseAddress: UnsafeRawPointer,
@@ -36,7 +35,6 @@ package typealias FallbackEventHandler = @Sendable @convention(c) (
3635
/// exchange the previous value with a new value, call
3736
/// ``setFallbackEventHandler(_:)`` and store its returned value.
3837
@_cdecl("swift_testing_getFallbackEventHandler")
39-
@usableFromInline
4038
package func fallbackEventHandler() -> FallbackEventHandler? {
4139
swt_loadFallbackEventHandler()
4240
}
@@ -52,7 +50,6 @@ package func fallbackEventHandler() -> FallbackEventHandler? {
5250
/// by the first testing library to run. If this function has already been
5351
/// called and the handler set, it does not replace the previous handler.
5452
@_cdecl("swift_testing_installFallbackEventHandler")
55-
@usableFromInline
5653
package func installFallbackEventHandler(_ handler: FallbackEventHandler) -> CBool {
5754
swt_installFallbackEventHandler(handler)
5855
}

0 commit comments

Comments
 (0)