Skip to content

Commit e2ec041

Browse files
authored
Change default behavior of exit tests to always succeed. (#858)
Exit tests simulate calling `main()` (or some similar thing), but if you don't explicitly exit from them, they force a failure. After much mulling and some discussion with colleagues a while back, we should change the behavior so that, if a test doesn't otherwise terminate, it acts as if `main()` returned naturally—that is, by exiting with `EXIT_SUCCESS` rather than by forcing `EXIT_FAILURE`. This behavior is more consistent with the feature's theory of operation. For example: ```swift await #expect(exitsWith: .success) { assert(2 > 1) // this is true and doesn't assert, so this test should pass, right? } ``` Exit tests remain an experimental feature. ### 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 0b77119 commit e2ec041

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

Sources/Testing/ExitTests/ExitTest.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,9 @@ extension ExitTest {
158158
_errorInMain(error)
159159
}
160160

161-
// Run some glue code that terminates the process with an exit condition
162-
// that does not match the expected one. If the exit test's body doesn't
163-
// terminate, we'll manually call exit() and cause the test to fail.
164-
let expectingFailure = expectedExitCondition == .failure
165-
exit(expectingFailure ? EXIT_SUCCESS : EXIT_FAILURE)
161+
// If we get to this point without terminating, then we simulate main()'s
162+
// behavior which is to exit with EXIT_SUCCESS.
163+
exit(EXIT_SUCCESS)
166164
}
167165
}
168166

Tests/TestingTests/ExitTestTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ private import _TestingInternals
2222
exit(EXIT_FAILURE + 1)
2323
}
2424
}
25+
await #expect(exitsWith: .success) {}
2526
await #expect(exitsWith: .success) {
2627
exit(EXIT_SUCCESS)
2728
}
@@ -63,7 +64,7 @@ private import _TestingInternals
6364
}
6465

6566
@Test("Exit tests (failing)") func failing() async {
66-
await confirmation("Exit tests failed", expectedCount: 10) { failed in
67+
await confirmation("Exit tests failed", expectedCount: 9) { failed in
6768
var configuration = Configuration()
6869
configuration.eventHandler = { event, _ in
6970
if case .issueRecorded = event.kind {
@@ -410,7 +411,6 @@ private import _TestingInternals
410411

411412
@Suite(.hidden) struct FailingExitTests {
412413
@Test(.hidden) func failingExitTests() async {
413-
await #expect(exitsWith: .success) {}
414414
await #expect(exitsWith: .failure) {}
415415
await #expect(exitsWith: .exitCode(123)) {}
416416
await #expect(exitsWith: .failure) {

0 commit comments

Comments
 (0)