Skip to content

Commit 5536aa3

Browse files
committed
Common code between exit tests and the fallback handler
1 parent 4af44ac commit 5536aa3

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

Sources/Testing/Events/Event+FallbackHandler.swift

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,50 @@
1111
private import _Testing_ExperimentalInfrastructure
1212

1313
extension Event {
14-
/// The implementation of ``fallbackEventHandler``.
14+
/// Attempt to handle an event encoded as JSON as if it had been generated in
15+
/// the current testing context.
1516
///
1617
/// - Parameters:
17-
/// - abi: The ABI version to use for decoding `recordJSON`.
1818
/// - recordJSON: The JSON encoding of an event record.
19+
/// - abi: The ABI version to use for decoding `recordJSON`.
1920
///
2021
/// - 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)
22+
///
23+
/// - Important: This function only handles a subset of event kinds.
24+
static func handle<V>(_ recordJSON: UnsafeRawBufferPointer, encodedWith abi: V.Type) throws where V: ABI.Version {
25+
let record = try JSON.decode(ABI.Record<V>.self, from: recordJSON)
2326
guard case let .event(event) = record.kind else {
2427
return
2528
}
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
29+
30+
lazy var comments: [Comment] = event._comments?.map(Comment.init(rawValue:)) ?? []
31+
lazy var sourceContext = SourceContext(
32+
backtrace: nil, // A backtrace from the child process will have the wrong address space.
33+
sourceLocation: event._sourceLocation
34+
)
35+
lazy var skipInfo = SkipInfo(comment: comments.first, sourceContext: sourceContext)
36+
if let issue = Issue(event) {
37+
issue.record()
38+
} else if let attachment = event.attachment {
39+
Attachment.record(attachment, sourceLocation: event._sourceLocation!)
40+
} else if case .testCancelled = event.kind {
41+
_ = try? Test.cancel(with: skipInfo)
42+
} else if case .testCaseCancelled = event.kind {
43+
_ = try? Test.Case.cancel(with: skipInfo)
3644
}
3745
}
3846

47+
/// The implementation of ``fallbackEventHandler``.
48+
///
49+
/// - Parameters:
50+
/// - abi: The ABI version to use for decoding `recordJSON`.
51+
/// - recordJSON: The JSON encoding of an event record.
52+
///
53+
/// - Throws: Any error that prevented handling the encoded record.
54+
private static func _fallbackEventHandler<V>(_ abi: V.Type, _ recordJSON: UnsafeRawBufferPointer) throws where V: ABI.Version {
55+
try handle(recordJSON, encodedWith: abi)
56+
}
57+
3958
/// The fallback event handler to set when Swift Testing is the active testing
4059
/// library.
4160
private static let _fallbackEventHandler: FallbackEventHandler = { recordJSONSchemaVersionNumber, recordJSONBaseAddress, recordJSONByteCount, _ in

Sources/Testing/ExitTests/ExitTest.swift

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,26 +1031,7 @@ extension ExitTest {
10311031
///
10321032
/// - Throws: Any error encountered attempting to decode or process the JSON.
10331033
private static func _processRecord(_ recordJSON: UnsafeRawBufferPointer, fromBackChannel backChannel: borrowing FileHandle) throws {
1034-
let record = try JSON.decode(ABI.Record<ABI.BackChannelVersion>.self, from: recordJSON)
1035-
guard case let .event(event) = record.kind else {
1036-
return
1037-
}
1038-
1039-
lazy var comments: [Comment] = event._comments?.map(Comment.init(rawValue:)) ?? []
1040-
lazy var sourceContext = SourceContext(
1041-
backtrace: nil, // A backtrace from the child process will have the wrong address space.
1042-
sourceLocation: event._sourceLocation
1043-
)
1044-
lazy var skipInfo = SkipInfo(comment: comments.first, sourceContext: sourceContext)
1045-
if let issue = Issue(event) {
1046-
issue.record()
1047-
} else if let attachment = event.attachment {
1048-
Attachment.record(attachment, sourceLocation: event._sourceLocation!)
1049-
} else if case .testCancelled = event.kind {
1050-
_ = try? Test.cancel(with: skipInfo)
1051-
} else if case .testCaseCancelled = event.kind {
1052-
_ = try? Test.Case.cancel(with: skipInfo)
1053-
}
1034+
try Event.handle(recordJSON, encodedWith: ABI.BackChannelVersion.self)
10541035
}
10551036

10561037
/// Decode this exit test's captured values and update its ``capturedValues``

0 commit comments

Comments
 (0)