Skip to content

Commit 5e86c57

Browse files
Check the XCTest fallback handler *after* checking for other configurations. (#1542)
The new interop code orders the fallback handler _before_ the `Configuration.all` check, meaning that issues recorded in detached tasks _always_ fall back to XCTest. This can cause spurious test passage because these issues get recorded as warnings by default. (Our own tests are broken as a result). Resolves rdar://170161483. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated. --------- Co-authored-by: Jerry Chen <[email protected]>
1 parent a1b94d7 commit 5e86c57

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

Sources/Testing/Events/Event.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,18 @@ extension Event {
342342
if configuration.eventHandlingOptions.shouldHandleEvent(self) {
343343
configuration.handleEvent(self, in: context)
344344
}
345-
} else if postToFallbackHandler(in: context) {
346-
// The fallback event handler handled this event.
347345
} else {
348346
// The current task does NOT have an associated configuration. This event
349347
// will be lost! Post it to every registered event handler to avoid that.
350-
for configuration in Configuration.all {
351-
_post(in: context, configuration: configuration)
348+
let configurations = Configuration.all
349+
if configurations.isEmpty {
350+
// There are no registered event handlers. Use the fallback event
351+
// handler instead.
352+
_ = postToFallbackHandler(in: context)
353+
} else {
354+
for configuration in configurations {
355+
_post(in: context, configuration: configuration)
356+
}
352357
}
353358
}
354359
}

Sources/Testing/Running/Runner.RuntimeState.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,24 @@ extension Configuration {
161161
}
162162
}
163163

164+
#if DEBUG
165+
/// Removes all configurations set in the current process.
166+
///
167+
/// - Returns: The number of configuration instances that were removed from
168+
/// the current process' internal registry.
169+
///
170+
/// - Warning: This function should never be called in the main testing
171+
/// process. Only use it in an exit test when you need a (relatively) clean
172+
/// slate for e.g. XCTest interop.
173+
@discardableResult static func removeAll() -> Int {
174+
Self._all.withLock { all in
175+
let result = all.instances.count
176+
all.instances.removeAll(keepingCapacity: false)
177+
return result
178+
}
179+
}
180+
#endif
181+
164182
/// An atomic counter that tracks the number of "current" configurations that
165183
/// have set ``EventHandlingOptions/isExpectationCheckedEventEnabled`` to
166184
/// `true`.

Tests/TestingTests/EventHandlingInteropTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ struct EventHandlingInteropTests {
4747
/// its own fallback handler.
4848
@Test func `Post event without config -> fallback handler`() async throws {
4949
await #expect(processExitsWith: .success) {
50+
Configuration.removeAll()
51+
5052
let installer = try #require(Self.installer)
5153
try #require(
5254
installer(Self.capturingHandler), "Installation of fallback handler should succeed")

0 commit comments

Comments
 (0)