Skip to content

Commit 6952004

Browse files
authored
[6.2] Only clear exit test ID env var upon successful lookup (#1227)
1 parent 9ae56eb commit 6952004

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

Sources/Testing/ExitTests/ExitTest.swift

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ extension ExitTest {
233233
#endif
234234
}
235235

236+
/// The name of the environment variable used to identify the exit test to
237+
/// call in a spawned exit test process.
238+
private static let _idEnvironmentVariableName = "SWT_EXIT_TEST_ID"
239+
236240
/// Call the exit test in the current process.
237241
///
238242
/// This function invokes the closure originally passed to
@@ -698,6 +702,17 @@ extension ExitTest {
698702
#endif
699703
}
700704

705+
/// The ID of the exit test to run, if any, specified in the environment.
706+
static var environmentIDForEntryPoint: ID? {
707+
guard var idString = Environment.variable(named: Self._idEnvironmentVariableName) else {
708+
return nil
709+
}
710+
711+
return try? idString.withUTF8 { idBuffer in
712+
try JSON.decode(ExitTest.ID.self, from: UnsafeRawBufferPointer(idBuffer))
713+
}
714+
}
715+
701716
/// Find the exit test function specified in the environment of the current
702717
/// process, if any.
703718
///
@@ -708,21 +723,15 @@ extension ExitTest {
708723
/// `__swiftPMEntryPoint()` function. The effect of using it under other
709724
/// configurations is undefined.
710725
static func findInEnvironmentForEntryPoint() -> Self? {
711-
// Find the ID of the exit test to run, if any, in the environment block.
712-
var id: ExitTest.ID?
713-
if var idString = Environment.variable(named: "SWT_EXIT_TEST_ID") {
714-
// Clear the environment variable. It's an implementation detail and exit
715-
// test code shouldn't be dependent on it. Use ExitTest.current if needed!
716-
Environment.setVariable(nil, named: "SWT_EXIT_TEST_ID")
717-
718-
id = try? idString.withUTF8 { idBuffer in
719-
try JSON.decode(ExitTest.ID.self, from: UnsafeRawBufferPointer(idBuffer))
720-
}
721-
}
722-
guard let id, var result = find(identifiedBy: id) else {
726+
guard let id = environmentIDForEntryPoint, var result = find(identifiedBy: id) else {
723727
return nil
724728
}
725729

730+
// Since an exit test was found, clear the environment variable. It's an
731+
// implementation detail and exit test code shouldn't be dependent on it.
732+
// Use ExitTest.current if needed!
733+
Environment.setVariable(nil, named: Self._idEnvironmentVariableName)
734+
726735
// If an exit test was found, inject back channel handling into its body.
727736
// External tools authors should set up their own back channel mechanisms
728737
// and ensure they're installed before calling ExitTest.callAsFunction().
@@ -852,7 +861,7 @@ extension ExitTest {
852861
// Insert a specific variable that tells the child process which exit test
853862
// to run.
854863
try JSON.withEncoding(of: exitTest.id) { json in
855-
childEnvironment["SWT_EXIT_TEST_ID"] = String(decoding: json, as: UTF8.self)
864+
childEnvironment[Self._idEnvironmentVariableName] = String(decoding: json, as: UTF8.self)
856865
}
857866

858867
typealias ResultUpdater = @Sendable (inout ExitTest.Result) -> Void

0 commit comments

Comments
 (0)