Skip to content

Commit 843863f

Browse files
authored
Some minor exit test refinements. (#349)
I moved the forced call to `exit()` into `ExitTest.callAsFunction()` so that we can't accidentally miss it. Also removed a redundant `do {}` scope. ### 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 62d1c28 commit 843863f

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

Sources/Testing/ExitTests/ExitTest.swift

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,20 @@ public struct ExitTest: Sendable {
3333
public var sourceLocation: SourceLocation
3434

3535
/// Call the exit test in the current process.
36-
public func callAsFunction() async -> Void {
36+
///
37+
/// This function invokes the closure originally passed to
38+
/// `#expect(exitsWith:)` _in the current process_. That closure is expected
39+
/// to terminate the process; if it does not, the testing library will
40+
/// terminate the process in a way that causes the corresponding expectation
41+
/// to fail.
42+
public func callAsFunction() async -> Never {
3743
await body()
44+
45+
// Run some glue code that terminates the process with an exit condition
46+
// that does not match the expected one. If the exit test's body doesn't
47+
// terminate, we'll manually call exit() and cause the test to fail.
48+
let expectingFailure = expectedExitCondition.matches(.failure)
49+
exit(expectingFailure ? EXIT_SUCCESS : EXIT_FAILURE)
3850
}
3951
}
4052

@@ -96,19 +108,7 @@ extension ExitTest {
96108
}
97109
}
98110

99-
if var result = context.result {
100-
// Add some glue code that terminates the process with an exit condition
101-
// that does not match the expected one. If the exit test's body doesn't
102-
// terminate, we'll manually call exit() and cause the test to fail.
103-
let expectingFailure = result.expectedExitCondition.matches(.failure)
104-
result.body = { [body = result.body] in
105-
await body()
106-
exit(expectingFailure ? EXIT_SUCCESS : EXIT_FAILURE)
107-
}
108-
return result
109-
}
110-
111-
return nil
111+
return context.result
112112
}
113113
}
114114

@@ -288,34 +288,30 @@ extension ExitTest {
288288
// to run.
289289
childEnvironment["SWT_EXPERIMENTAL_EXIT_TEST_SOURCE_LOCATION"] = try String(data: JSONEncoder().encode(exitTest.sourceLocation), encoding: .utf8)!
290290

291-
let actualExitCode: Int32
292-
let wasSignalled: Bool
293-
do {
294-
(actualExitCode, wasSignalled) = try await withCheckedThrowingContinuation { continuation in
295-
let process = Process()
296-
process.executableURL = childProcessURL
297-
process.arguments = childArguments
298-
process.environment = childEnvironment
299-
process.terminationHandler = { process in
300-
continuation.resume(returning: (process.terminationStatus, process.terminationReason == .uncaughtSignal))
301-
}
302-
do {
303-
try process.run()
304-
} catch {
305-
continuation.resume(throwing: error)
306-
}
291+
let (actualExitCode, wasSignalled) = try await withCheckedThrowingContinuation { continuation in
292+
let process = Process()
293+
process.executableURL = childProcessURL
294+
process.arguments = childArguments
295+
process.environment = childEnvironment
296+
process.terminationHandler = { process in
297+
continuation.resume(returning: (process.terminationStatus, process.terminationReason == .uncaughtSignal))
298+
}
299+
do {
300+
try process.run()
301+
} catch {
302+
continuation.resume(throwing: error)
307303
}
304+
}
308305

309-
if wasSignalled {
306+
if wasSignalled {
310307
#if os(Windows)
311-
// Actually an uncaught SEH/VEH exception (which we don't model yet.)
312-
return .failure
308+
// Actually an uncaught SEH/VEH exception (which we don't model yet.)
309+
return .failure
313310
#else
314-
return .signal(actualExitCode)
311+
return .signal(actualExitCode)
315312
#endif
316-
}
317-
return .exitCode(actualExitCode)
318313
}
314+
return .exitCode(actualExitCode)
319315
}
320316
}
321317
}

0 commit comments

Comments
 (0)