Skip to content

Commit 2d31852

Browse files
committed
Testing library list simplification
1 parent 41b404b commit 2d31852

File tree

3 files changed

+17
-30
lines changed

3 files changed

+17
-30
lines changed

Sources/Commands/PackageCommands/Init.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ extension SwiftPackageCommand {
5858
let initPackage = try InitPackage(
5959
name: packageName,
6060
packageType: initMode,
61-
supportedTestingLibraries: testLibraryOptions.enabledTestingLibraries,
61+
supportedTestingLibraries: Set(testLibraryOptions.enabledTestingLibraries),
6262
destinationPath: cwd,
6363
installedSwiftPMConfiguration: swiftCommandState.getHostToolchain().installedSwiftPMConfiguration,
6464
fileSystem: swiftCommandState.fileSystem

Sources/Commands/SwiftTestCommand.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
243243
var ranSuccessfully = true
244244

245245
// Run XCTest.
246-
if options.testLibraryOptions.enableXCTestSupport {
246+
if options.testLibraryOptions.isEnabled(.xctest) {
247247
if !self.options.shouldRunInParallel {
248248
let xctestArgs = try xctestArgs(for: testProducts, swiftCommandState: swiftCommandState)
249249
ranSuccessfully = try await runTestProducts(
@@ -287,7 +287,7 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
287287
}
288288

289289
// Run Swift Testing (parallel or not, it has a single entry point.)
290-
if options.testLibraryOptions.enableSwiftTestingLibrarySupport {
290+
if options.testLibraryOptions.isEnabled(.swiftTesting) {
291291
ranSuccessfully = try await runTestProducts(
292292
testProducts,
293293
additionalArguments: [],
@@ -575,7 +575,7 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
575575
throw StringError("'--num-workers' must be greater than zero")
576576
}
577577

578-
if !options.testLibraryOptions.enableXCTestSupport {
578+
guard options.testLibraryOptions.isEnabled(.xctest) else {
579579
throw StringError("'--num-workers' is only supported when testing with XCTest")
580580
}
581581
}
@@ -660,7 +660,7 @@ extension SwiftTestCommand {
660660
sanitizers: globalOptions.build.sanitizers
661661
)
662662

663-
if testLibraryOptions.enableXCTestSupport {
663+
if testLibraryOptions.isEnabled(.xctest) {
664664
let testSuites = try TestingSupport.getTestSuites(
665665
in: testProducts,
666666
swiftCommandState: swiftCommandState,
@@ -676,7 +676,7 @@ extension SwiftTestCommand {
676676
}
677677
}
678678

679-
if testLibraryOptions.enableSwiftTestingLibrarySupport {
679+
if testLibraryOptions.isEnabled(.swiftTesting) {
680680
let additionalArguments = ["--", "--list-tests"] + CommandLine.arguments.dropFirst()
681681
let runner = TestRunner(
682682
bundlePaths: testProducts.map(\.bundlePath),

Sources/CoreCommands/Options.swift

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -580,12 +580,6 @@ public struct TestLibraryOptions: ParsableArguments {
580580
help: "Enable support for XCTest")
581581
public var explicitlyEnableXCTestSupport: Bool?
582582

583-
/// Whether to enable support for XCTest.
584-
public var enableXCTestSupport: Bool {
585-
// Default to enabled.
586-
explicitlyEnableXCTestSupport ?? true
587-
}
588-
589583
/// Whether to enable support for Swift Testing (as explicitly specified by the user.)
590584
///
591585
/// Callers will generally want to use ``enableSwiftTestingLibrarySupport`` since it will
@@ -603,26 +597,19 @@ public struct TestLibraryOptions: ParsableArguments {
603597
help: .hidden)
604598
public var explicitlyEnableExperimentalSwiftTestingLibrarySupport: Bool?
605599

606-
/// Whether to enable support for swift-testing.
607-
public var enableSwiftTestingLibrarySupport: Bool {
608-
// Default to enabled.
609-
explicitlyEnableSwiftTestingLibrarySupport ??
610-
explicitlyEnableExperimentalSwiftTestingLibrarySupport ??
611-
true
612-
}
613-
614-
/// Get the set of enabled testing libraries.
615-
public var enabledTestingLibraries: Set<BuildParameters.Testing.Library> {
616-
var result = Set<BuildParameters.Testing.Library>()
617-
618-
if enableXCTestSupport {
619-
result.insert(.xctest)
620-
}
621-
if enableSwiftTestingLibrarySupport {
622-
result.insert(.swiftTesting)
600+
/// Test whether or not a given library is enabled.
601+
public func isEnabled(_ library: BuildParameters.Testing.Library) -> Bool {
602+
switch library {
603+
case .xctest:
604+
explicitlyEnableXCTestSupport ?? true
605+
case .swiftTesting:
606+
explicitlyEnableSwiftTestingLibrarySupport ?? explicitlyEnableExperimentalSwiftTestingLibrarySupport ?? true
623607
}
608+
}
624609

625-
return result
610+
/// The list of enabled testing libraries.
611+
public var enabledTestingLibraries: [BuildParameters.Testing.Library] {
612+
[.xctest, .swiftTesting].lazy.filter(isEnabled)
626613
}
627614
}
628615

0 commit comments

Comments
 (0)