Skip to content

Commit 41b404b

Browse files
committed
Make sure --parallel works
1 parent 432dd51 commit 41b404b

File tree

1 file changed

+52
-53
lines changed

1 file changed

+52
-53
lines changed

Sources/Commands/SwiftTestCommand.swift

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -240,62 +240,69 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
240240
_ = try? localFileSystem.removeFileTree(buildParameters.testOutputPath)
241241
}
242242

243-
if !self.options.shouldRunInParallel {
244-
if options.testLibraryOptions.enableXCTestSupport {
243+
var ranSuccessfully = true
244+
245+
// Run XCTest.
246+
if options.testLibraryOptions.enableXCTestSupport {
247+
if !self.options.shouldRunInParallel {
245248
let xctestArgs = try xctestArgs(for: testProducts, swiftCommandState: swiftCommandState)
246-
try await runTestProducts(
249+
ranSuccessfully = try await runTestProducts(
247250
testProducts,
248251
additionalArguments: xctestArgs,
249252
productsBuildParameters: buildParameters,
250253
swiftCommandState: swiftCommandState,
251254
library: .xctest
252-
)
253-
}
254-
if options.testLibraryOptions.enableSwiftTestingLibrarySupport {
255-
try await runTestProducts(
256-
testProducts,
257-
additionalArguments: [],
258-
productsBuildParameters: buildParameters,
255+
) && ranSuccessfully
256+
} else {
257+
let testSuites = try TestingSupport.getTestSuites(
258+
in: testProducts,
259259
swiftCommandState: swiftCommandState,
260-
library: .swiftTesting
260+
enableCodeCoverage: options.enableCodeCoverage,
261+
shouldSkipBuilding: options.sharedOptions.shouldSkipBuilding,
262+
experimentalTestOutput: options.enableExperimentalTestOutput,
263+
sanitizers: globalOptions.build.sanitizers
261264
)
262-
}
263-
} else {
264-
let testSuites = try TestingSupport.getTestSuites(
265-
in: testProducts,
266-
swiftCommandState: swiftCommandState,
267-
enableCodeCoverage: options.enableCodeCoverage,
268-
shouldSkipBuilding: options.sharedOptions.shouldSkipBuilding,
269-
experimentalTestOutput: options.enableExperimentalTestOutput,
270-
sanitizers: globalOptions.build.sanitizers
271-
)
272-
let tests = try testSuites
273-
.filteredTests(specifier: options.testCaseSpecifier)
274-
.skippedTests(specifier: options.skippedTests(fileSystem: swiftCommandState.fileSystem))
265+
let tests = try testSuites
266+
.filteredTests(specifier: options.testCaseSpecifier)
267+
.skippedTests(specifier: options.skippedTests(fileSystem: swiftCommandState.fileSystem))
275268

276-
// Run the tests using the parallel runner.
277-
let runner = ParallelTestRunner(
278-
bundlePaths: testProducts.map { $0.bundlePath },
279-
cancellator: swiftCommandState.cancellator,
280-
toolchain: toolchain,
281-
numJobs: options.numberOfWorkers ?? ProcessInfo.processInfo.activeProcessorCount,
282-
buildOptions: globalOptions.build,
283-
productsBuildParameters: buildParameters,
284-
shouldOutputSuccess: swiftCommandState.logLevel <= .info,
285-
observabilityScope: swiftCommandState.observabilityScope
286-
)
269+
// Run the tests using the parallel runner.
270+
let runner = ParallelTestRunner(
271+
bundlePaths: testProducts.map { $0.bundlePath },
272+
cancellator: swiftCommandState.cancellator,
273+
toolchain: toolchain,
274+
numJobs: options.numberOfWorkers ?? ProcessInfo.processInfo.activeProcessorCount,
275+
buildOptions: globalOptions.build,
276+
productsBuildParameters: buildParameters,
277+
shouldOutputSuccess: swiftCommandState.logLevel <= .info,
278+
observabilityScope: swiftCommandState.observabilityScope
279+
)
287280

288-
let testResults = try runner.run(tests)
281+
let testResults = try runner.run(tests)
289282

290-
try generateXUnitOutputIfRequested(for: testResults, swiftCommandState: swiftCommandState)
283+
try generateXUnitOutputIfRequested(for: testResults, swiftCommandState: swiftCommandState)
291284

292-
if !runner.ranSuccessfully {
293-
swiftCommandState.executionStatus = .failure
285+
ranSuccessfully = ranSuccessfully && runner.ranSuccessfully
294286
}
287+
}
295288

296-
if self.options.enableExperimentalTestOutput, !runner.ranSuccessfully {
297-
try Self.handleTestOutput(productsBuildParameters: buildParameters, packagePath: testProducts[0].packagePath)
298-
}
289+
// Run Swift Testing (parallel or not, it has a single entry point.)
290+
if options.testLibraryOptions.enableSwiftTestingLibrarySupport {
291+
ranSuccessfully = try await runTestProducts(
292+
testProducts,
293+
additionalArguments: [],
294+
productsBuildParameters: buildParameters,
295+
swiftCommandState: swiftCommandState,
296+
library: .swiftTesting
297+
) && ranSuccessfully
298+
}
299+
300+
if !ranSuccessfully {
301+
swiftCommandState.executionStatus = .failure
302+
}
303+
304+
if self.options.enableExperimentalTestOutput, !ranSuccessfully {
305+
try Self.handleTestOutput(productsBuildParameters: buildParameters, packagePath: testProducts[0].packagePath)
299306
}
300307
}
301308

@@ -390,12 +397,12 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
390397
productsBuildParameters: BuildParameters,
391398
swiftCommandState: SwiftCommandState,
392399
library: BuildParameters.Testing.Library
393-
) async throws {
400+
) async throws -> Bool {
394401
#if os(macOS)
395402
if library == .swiftTesting {
396403
// On macOS, the xctest executable provided with Xcode acts as a harness
397404
// for Swift Testing, so we don't need to run Swift Testing tests separately.
398-
return
405+
return true
399406
}
400407
#endif
401408

@@ -424,19 +431,11 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
424431
)
425432

426433
// Finally, run the tests.
427-
let ranSuccessfully = runner.test(outputHandler: {
434+
return runner.test(outputHandler: {
428435
// command's result output goes on stdout
429436
// ie "swift test" should output to stdout
430437
print($0, terminator: "")
431438
})
432-
433-
if !ranSuccessfully {
434-
swiftCommandState.executionStatus = .failure
435-
}
436-
437-
if self.options.enableExperimentalTestOutput, !ranSuccessfully {
438-
try Self.handleTestOutput(productsBuildParameters: productsBuildParameters, packagePath: testProducts[0].packagePath)
439-
}
440439
}
441440

442441
private static func handleTestOutput(productsBuildParameters: BuildParameters, packagePath: AbsolutePath) throws {

0 commit comments

Comments
 (0)