Skip to content

Commit bdfd43d

Browse files
authored
Avoid fork-bombing ourselves when hosted by swift-test. (#528)
This PR amends exit tests' child process spawning logic to ensure that they don't trigger a fork bomb or fail outright when the test process is hosted by Swift Package Manager (as in [this branch](https://github.com/swiftlang/swift-package-manager/tree/jgrynspan/one-test-product-two-libraries)). Exit tests are an experimental feature, but one that we use internally in our own test suite, so we need to make sure our tests continue to function if/when that SwiftPM branch is merged. ### 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 e678d1b commit bdfd43d

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

Sources/Testing/ExitTests/ExitTest.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,16 @@ extension ExitTest {
232232
// can precompute them.
233233
let childProcessExecutablePath = Result { try CommandLine.executablePath }
234234

235-
// We only need to pass arguments when hosted by XCTest.
235+
// Construct appropriate arguments for the child process. Generally these
236+
// arguments are going to be whatever's necessary to respawn the current
237+
// executable and get back into Swift Testing.
236238
let childArguments: [String] = {
237239
var result = [String]()
240+
241+
let parentArguments = CommandLine.arguments
238242
#if SWT_TARGET_OS_APPLE
239243
lazy var xctestTargetPath = Environment.variable(named: "XCTestBundlePath")
240-
?? CommandLine.arguments.dropFirst().last
244+
?? parentArguments.dropFirst().last
241245
// If the running executable appears to be the XCTest runner executable in
242246
// Xcode, figure out the path to the running XCTest bundle. If we can find
243247
// it, then we can re-run the host XCTestCase instance.
@@ -261,7 +265,23 @@ extension ExitTest {
261265
// to run.
262266
result += ["-XCTest", "/", xctestTargetPath]
263267
}
268+
269+
// When hosted by Swift Package Manager, forward all arguments to the
270+
// child process. (They aren't all meaningful in the context of an exit
271+
// test, but it keeps this code fairly simple!)
272+
lazy var isHostedBySwiftPM = parentArguments.contains("--test-bundle-path")
273+
if !isHostedByXCTest && isHostedBySwiftPM {
274+
result += parentArguments.dropFirst()
275+
}
276+
#else
277+
// When hosted by Swift Package Manager, we'll need to specify exactly
278+
// which testing library to call into from the shared test executable.
279+
let hasTestingLibraryArgument: Bool = parentArguments.contains { $0.starts(with: "--testing-library") }
280+
if hasTestingLibraryArgument {
281+
result += ["--testing-library", "swift-testing"]
282+
}
264283
#endif
284+
265285
return result
266286
}()
267287

0 commit comments

Comments
 (0)