Skip to content

Commit 6e94d73

Browse files
committed
Introduce new Configuration SPI controlling whether warning issues should be delivered, consolidating some similar functionality with an existing SPI. Address the ABI compatibility problem by defaulting all existing entry point versions to disable this feature, but default it to enabled so that the eventual v1 will have it enabled by default
1 parent 751fa82 commit 6e94d73

File tree

4 files changed

+71
-14
lines changed

4 files changed

+71
-14
lines changed

Sources/Testing/ABI/EntryPoints/ABIEntryPoint.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extension ABIv0 {
4747
/// callback.
4848
public static var entryPoint: EntryPoint {
4949
return { configurationJSON, recordHandler in
50-
try await Testing.entryPoint(
50+
try await _entryPoint(
5151
configurationJSON: configurationJSON,
5252
recordHandler: recordHandler
5353
) == EXIT_SUCCESS
@@ -87,7 +87,7 @@ typealias ABIEntryPoint_v0 = @Sendable (
8787
@usableFromInline func copyABIEntryPoint_v0() -> UnsafeMutableRawPointer {
8888
let result = UnsafeMutablePointer<ABIEntryPoint_v0>.allocate(capacity: 1)
8989
result.initialize { configurationJSON, recordHandler in
90-
try await entryPoint(
90+
try await _entryPoint(
9191
configurationJSON: configurationJSON,
9292
eventStreamVersionIfNil: -1,
9393
recordHandler: recordHandler
@@ -104,7 +104,7 @@ typealias ABIEntryPoint_v0 = @Sendable (
104104
///
105105
/// This function will be removed (with its logic incorporated into
106106
/// ``ABIv0/entryPoint-swift.type.property``) in a future update.
107-
private func entryPoint(
107+
private func _entryPoint(
108108
configurationJSON: UnsafeRawBufferPointer?,
109109
eventStreamVersionIfNil: Int? = nil,
110110
recordHandler: @escaping @Sendable (_ recordJSON: UnsafeRawBufferPointer) -> Void

Sources/Testing/ABI/EntryPoints/EntryPoint.swift

+9
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,15 @@ public func configurationForEntryPoint(from args: __CommandLineArguments_v0) thr
547547
configuration.exitTestHandler = ExitTest.handlerForEntryPoint()
548548
#endif
549549

550+
switch args.eventStreamVersion {
551+
case .some(...0):
552+
// If the event stream version was specified explicitly to a value < 1,
553+
// disable delivery of warning issue events to maintain legacy behavior.
554+
configuration.eventHandlingOptions.isWarningIssueEventDeliveryEnabled = false
555+
default:
556+
break
557+
}
558+
550559
return configuration
551560
}
552561

Sources/Testing/Events/Event.swift

+21-4
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,7 @@ extension Event {
290290
if let configuration = configuration ?? Configuration.current {
291291
// The caller specified a configuration, or the current task has an
292292
// associated configuration. Post to either configuration's event handler.
293-
switch kind {
294-
case .expectationChecked where !configuration.deliverExpectationCheckedEvents:
295-
break
296-
default:
293+
if configuration.eventHandlingOptions.shouldHandleEvent(self) {
297294
configuration.handleEvent(self, in: context)
298295
}
299296
} else {
@@ -306,6 +303,26 @@ extension Event {
306303
}
307304
}
308305

306+
extension Configuration.EventHandlingOptions {
307+
/// Determine whether the specified event should be handled according to the
308+
/// options in this instance.
309+
///
310+
/// - Parameters:
311+
/// - event: The event to consider handling.
312+
///
313+
/// - Returns: Whether or not the event should be handled or suppressed.
314+
fileprivate func shouldHandleEvent(_ event: borrowing Event) -> Bool {
315+
switch event.kind {
316+
case let .issueRecorded(issue):
317+
issue.severity > .warning || isWarningIssueEventDeliveryEnabled
318+
case .expectationChecked:
319+
isExpectationCheckedEventDeliveryEnabled
320+
default:
321+
true
322+
}
323+
}
324+
}
325+
309326
#if !SWT_NO_SNAPSHOT_TYPES
310327
// MARK: - Snapshotting
311328

Sources/Testing/Running/Configuration.swift

+38-7
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,35 @@ public struct Configuration: Sendable {
178178

179179
// MARK: - Event handling
180180

181-
/// Whether or not events of the kind
182-
/// ``Event/Kind-swift.enum/expectationChecked(_:)`` should be delivered to
183-
/// this configuration's ``eventHandler`` closure.
181+
/// A type describing options to use when delivering events to this
182+
/// configuration's event handler
183+
public struct EventHandlingOptions: Sendable {
184+
/// Whether or not events of the kind ``Event/Kind-swift.enum/issueRecorded(_:)``
185+
/// containing issues with warning (or lower) severity should be delivered
186+
/// to the event handler of the configuration these options are applied to.
187+
///
188+
/// By default, events matching this criteria are delivered to event
189+
/// handlers unless the entry point specifies an event stream version which
190+
/// predates the introduction of warning issues, in which case this is
191+
/// disabled to maintain legacy behavior.
192+
public var isWarningIssueEventDeliveryEnabled: Bool = true
193+
194+
/// Whether or not events of the kind
195+
/// ``Event/Kind-swift.enum/expectationChecked(_:)`` should be delivered to
196+
/// the event handler of the configuration these options are applied to.
197+
///
198+
/// By default, events of this kind are not delivered to event handlers
199+
/// because they occur frequently in a typical test run and can generate
200+
/// significant back-pressure on the event handler.
201+
public var isExpectationCheckedEventDeliveryEnabled: Bool = false
202+
}
203+
204+
/// The options to use when delivering events to this configuration's event
205+
/// handler.
184206
///
185-
/// By default, events of this kind are not delivered to event handlers
186-
/// because they occur frequently in a typical test run and can generate
187-
/// significant backpressure on the event handler.
188-
public var deliverExpectationCheckedEvents = false
207+
/// The default value of this property is an instance of ``EventHandlingOptions-swift.struct``
208+
/// with its properties initialized to their default values.
209+
public var eventHandlingOptions: EventHandlingOptions = .init()
189210

190211
/// The event handler to which events should be passed when they occur.
191212
public var eventHandler: Event.Handler = { _, _ in }
@@ -325,4 +346,14 @@ extension Configuration {
325346
}
326347
}
327348
#endif
349+
350+
@available(*, deprecated, message: "Set eventHandlingOptions.isExpectationCheckedEventDeliveryEnabled instead.")
351+
public var deliverExpectationCheckedEvents: Bool {
352+
get {
353+
eventHandlingOptions.isExpectationCheckedEventDeliveryEnabled
354+
}
355+
set {
356+
eventHandlingOptions.isExpectationCheckedEventDeliveryEnabled = newValue
357+
}
358+
}
328359
}

0 commit comments

Comments
 (0)