Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion Sources/Testing/Attachments/Attachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,26 @@ extension Attachment where AttachableValue: ~Copyable {
}
}

extension Runner {
/// Modify this runner's configured event handler so that it handles "value
/// attached" events and saves attachments where necessary.
mutating func configureAttachmentHandling() {
configuration.eventHandler = { [oldEventHandler = configuration.eventHandler] event, context in
#if !SWT_NO_FILE_IO
var event = copy event
if case .valueAttached = event.kind {
guard let configuration = context.configuration,
configuration.handleValueAttachedEvent(&event, in: context) else {
// The attachment could not be handled, so suppress this event.
return
}
}
oldEventHandler(event, context)
#endif
}
}
}

extension Configuration {
/// Handle the given "value attached" event.
///
Expand All @@ -517,7 +537,7 @@ extension Configuration {
/// not need to call it elsewhere. It automatically saves the attachment
/// associated with `event` and modifies `event` to include the path where the
/// attachment was saved.
func handleValueAttachedEvent(_ event: inout Event, in eventContext: borrowing Event.Context) -> Bool {
fileprivate func handleValueAttachedEvent(_ event: inout Event, in eventContext: borrowing Event.Context) -> Bool {
guard let attachmentsPath else {
// If there is no path to which attachments should be written, there's
// nothing to do here. The event handler may still want to handle it.
Expand Down
10 changes: 0 additions & 10 deletions Sources/Testing/Running/Runner.RuntimeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,6 @@ extension Runner {
}

configuration.eventHandler = { [oldEventHandler = configuration.eventHandler] event, context in
#if !SWT_NO_FILE_IO
var event = copy event
if case .valueAttached = event.kind {
guard let configuration = context.configuration,
configuration.handleValueAttachedEvent(&event, in: context) else {
// The attachment could not be handled, so suppress this event.
return
}
}
#endif
RuntimeState.$current.withValue(existingRuntimeState) {
oldEventHandler(event, context)
}
Expand Down
1 change: 1 addition & 0 deletions Sources/Testing/Running/Runner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ extension Runner {
private static func _run(_ runner: Self) async {
var runner = runner
runner.configureEventHandlerRuntimeState()
runner.configureAttachmentHandling()

// Track whether or not any issues were recorded across the entire run.
let issueRecorded = Locked(rawValue: false)
Expand Down
Loading