Skip to content

Commit 3f388d0

Browse files
authored
Relax the restrictions on exit tests running in the background. (#371)
Although it's not likely going to happen much in the real world, this PR makes it possible to run an exit test on a detached background task. ### 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.
1 parent 3ac742f commit 3f388d0

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

Sources/Testing/ExitTests/ExitTest.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ func callExitTest(
137137
isRequired: Bool,
138138
sourceLocation: SourceLocation
139139
) async -> Result<Void, any Error> {
140-
// FIXME: use lexicalContext to capture these misuses at compile time.
141-
guard let configuration = Configuration.current, Test.current != nil else {
140+
guard let configuration = Configuration.current ?? Configuration.all.first else {
142141
preconditionFailure("A test must be running on the current task to use #expect(exitsWith:).")
143142
}
144143

Sources/Testing/Expectations/Expectation+Macro.swift

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,7 @@ public macro require(
466466
/// }
467467
/// ```
468468
///
469-
/// Additionally, an exit test:
470-
///
471-
/// - _must_ run within a test function (or a function called by one);
472-
/// - cannot run within another exit test; and
473-
/// - cannot be run on a detached child task, a dispatch queue, or a background
474-
/// thread.
469+
/// An exit test cannot run within another exit test.
475470
@_spi(Experimental)
476471
#if SWT_NO_EXIT_TESTS
477472
@available(*, unavailable, message: "Exit tests are not available on this platform.")
@@ -546,12 +541,7 @@ public macro require(
546541
/// }
547542
/// ```
548543
///
549-
/// Additionally, an exit test:
550-
///
551-
/// - _must_ run within a test function (or a function called by one);
552-
/// - cannot run within another exit test; and
553-
/// - cannot be run on a detached child task, a dispatch queue, or a background
554-
/// thread.
544+
/// An exit test cannot run within another exit test.
555545
@_spi(Experimental)
556546
#if SWT_NO_EXIT_TESTS
557547
@available(*, unavailable, message: "Exit tests are not available on this platform.")

Tests/TestingTests/ExitTestTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ private import TestingInternals
4141
await #expect(exitsWith: .signal(SIGABRT)) {
4242
abort()
4343
}
44+
#endif
45+
#if !SWT_NO_UNSTRUCTURED_TASKS
46+
#if false
47+
// Test the detached (no task-local configuration) path. Disabled because,
48+
// like other tests using Task.detached, it can interfere with other tests
49+
// running concurrently.
50+
#expect(Test.current != nil)
51+
await Task.detached {
52+
#expect(Test.current == nil)
53+
await #expect(exitsWith: .failure) {
54+
fatalError()
55+
}
56+
}.value
57+
#endif
4458
#endif
4559
}
4660

0 commit comments

Comments
 (0)