Skip to content

Avoid fork-bombing ourselves when hosted by swift-test. #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions Sources/Testing/ExitTests/ExitTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,16 @@ extension ExitTest {
// can precompute them.
let childProcessExecutablePath = Result { try CommandLine.executablePath }

// We only need to pass arguments when hosted by XCTest.
// Construct appropriate arguments for the child process. Generally these
// arguments are going to be whatever's necessary to respawn the current
// executable and get back into Swift Testing.
let childArguments: [String] = {
var result = [String]()

let parentArguments = CommandLine.arguments
#if SWT_TARGET_OS_APPLE
lazy var xctestTargetPath = Environment.variable(named: "XCTestBundlePath")
?? CommandLine.arguments.dropFirst().last
?? parentArguments.dropFirst().last
// If the running executable appears to be the XCTest runner executable in
// Xcode, figure out the path to the running XCTest bundle. If we can find
// it, then we can re-run the host XCTestCase instance.
Expand All @@ -261,7 +265,23 @@ extension ExitTest {
// to run.
result += ["-XCTest", "/", xctestTargetPath]
}

// When hosted by Swift Package Manager, forward all arguments to the
// child process. (They aren't all meaningful in the context of an exit
// test, but it keeps this code fairly simple!)
lazy var isHostedBySwiftPM = parentArguments.contains("--test-bundle-path")
if !isHostedByXCTest && isHostedBySwiftPM {
result += parentArguments.dropFirst()
}
#else
// When hosted by Swift Package Manager, we'll need to specify exactly
// which testing library to call into from the shared test executable.
let hasTestingLibraryArgument: Bool = parentArguments.contains { $0.starts(with: "--testing-library") }
if hasTestingLibraryArgument {
result += ["--testing-library", "swift-testing"]
}
#endif

return result
}()

Expand Down