Skip to content

Commit 1b3f976

Browse files
committed
Check for presence of XCTest when swift test is called
1 parent fd9483d commit 1b3f976

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

Sources/Commands/PackageCommands/Init.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,16 @@ extension SwiftPackageCommand {
5959
// Which testing libraries should be used? XCTest is on by default,
6060
// but Swift Testing must remain off by default until it is present
6161
// in the Swift toolchain.
62+
<<<<<<< Updated upstream
6263
var supportedTestingLibraries = Set<BuildParameters.Testing.Library>()
6364
if testLibraryOptions.isEnabled(.xctest) {
65+
=======
66+
var supportedTestingLibraries = Set<TestingLibrary>()
67+
if testLibraryOptions.isEnabled(.xctest, swiftCommandState: swiftCommandState) {
68+
>>>>>>> Stashed changes
6469
supportedTestingLibraries.insert(.xctest)
6570
}
66-
if testLibraryOptions.isExplicitlyEnabled(.swiftTesting) {
71+
if testLibraryOptions.isExplicitlyEnabled(.swiftTesting, swiftCommandState: swiftCommandState) {
6772
supportedTestingLibraries.insert(.swiftTesting)
6873
}
6974

Sources/Commands/SwiftTestCommand.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
256256
var results = [TestRunner.Result]()
257257

258258
// Run XCTest.
259-
if options.testLibraryOptions.isEnabled(.xctest) {
259+
if options.testLibraryOptions.isEnabled(.xctest, swiftCommandState: swiftCommandState) {
260260
// validate XCTest available on darwin based systems
261261
let toolchain = try swiftCommandState.getTargetToolchain()
262262
if case let .unsupported(reason) = try swiftCommandState.getHostToolchain().swiftSDK.xctestSupport {
@@ -324,9 +324,9 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
324324
}
325325

326326
// Run Swift Testing (parallel or not, it has a single entry point.)
327-
if options.testLibraryOptions.isEnabled(.swiftTesting) {
327+
if options.testLibraryOptions.isEnabled(.swiftTesting, swiftCommandState: swiftCommandState) {
328328
lazy var testEntryPointPath = testProducts.lazy.compactMap(\.testEntryPointPath).first
329-
if options.testLibraryOptions.isExplicitlyEnabled(.swiftTesting) || testEntryPointPath == nil {
329+
if options.testLibraryOptions.isExplicitlyEnabled(.swiftTesting, swiftCommandState: swiftCommandState) || testEntryPointPath == nil {
330330
results.append(
331331
try await runTestProducts(
332332
testProducts,
@@ -412,7 +412,7 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
412412
public func run(_ swiftCommandState: SwiftCommandState) async throws {
413413
do {
414414
// Validate commands arguments
415-
try self.validateArguments(observabilityScope: swiftCommandState.observabilityScope)
415+
try self.validateArguments(swiftCommandState: swiftCommandState)
416416
} catch {
417417
swiftCommandState.observabilityScope.emit(error)
418418
throw ExitCode.failure
@@ -466,7 +466,7 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
466466
}
467467
additionalArguments += commandLineArguments
468468

469-
if var xunitPath = options.xUnitOutput, options.testLibraryOptions.isEnabled(.xctest) {
469+
if var xunitPath = options.xUnitOutput, options.testLibraryOptions.isEnabled(.xctest, swiftCommandState: swiftCommandState) {
470470
// We are running Swift Testing, XCTest is also running in this session, and an xUnit path
471471
// was specified. Make sure we don't stomp on XCTest's XML output by having Swift Testing
472472
// write to a different path.
@@ -634,7 +634,7 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
634634
/// Private function that validates the commands arguments
635635
///
636636
/// - Throws: if a command argument is invalid
637-
private func validateArguments(observabilityScope: ObservabilityScope) throws {
637+
private func validateArguments(swiftCommandState: SwiftCommandState) throws {
638638
// Validation for --num-workers.
639639
if let workers = options.numberOfWorkers {
640640

@@ -649,13 +649,13 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
649649
throw StringError("'--num-workers' must be greater than zero")
650650
}
651651

652-
guard options.testLibraryOptions.isEnabled(.xctest) else {
652+
guard options.testLibraryOptions.isEnabled(.xctest, swiftCommandState: swiftCommandState) else {
653653
throw StringError("'--num-workers' is only supported when testing with XCTest")
654654
}
655655
}
656656

657657
if options._deprecated_shouldListTests {
658-
observabilityScope.emit(warning: "'--list-tests' option is deprecated; use 'swift test list' instead")
658+
swiftCommandState.observabilityScope.emit(warning: "'--list-tests' option is deprecated; use 'swift test list' instead")
659659
}
660660
}
661661

@@ -739,7 +739,7 @@ extension SwiftTestCommand {
739739
library: .swiftTesting
740740
)
741741

742-
if testLibraryOptions.isEnabled(.xctest) {
742+
if testLibraryOptions.isEnabled(.xctest, swiftCommandState: swiftCommandState) {
743743
let testSuites = try TestingSupport.getTestSuites(
744744
in: testProducts,
745745
swiftCommandState: swiftCommandState,
@@ -755,9 +755,9 @@ extension SwiftTestCommand {
755755
}
756756
}
757757

758-
if testLibraryOptions.isEnabled(.swiftTesting) {
758+
if testLibraryOptions.isEnabled(.swiftTesting, swiftCommandState: swiftCommandState) {
759759
lazy var testEntryPointPath = testProducts.lazy.compactMap(\.testEntryPointPath).first
760-
if testLibraryOptions.isExplicitlyEnabled(.swiftTesting) || testEntryPointPath == nil {
760+
if testLibraryOptions.isExplicitlyEnabled(.swiftTesting, swiftCommandState: swiftCommandState) || testEntryPointPath == nil {
761761
let additionalArguments = ["--list-tests"] + CommandLine.arguments.dropFirst()
762762
let runner = TestRunner(
763763
bundlePaths: testProducts.map(\.binaryPath),

Sources/CoreCommands/Options.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import struct PackageModel.EnabledSanitizers
2525
import struct PackageModel.PackageIdentity
2626
import class PackageModel.Manifest
2727
import enum PackageModel.Sanitizer
28+
@_spi(SwiftPMInternal) import struct PackageModel.SwiftSDK
2829

2930
import struct PackageGraph.TraitConfiguration
3031

@@ -597,16 +598,28 @@ public struct TestLibraryOptions: ParsableArguments {
597598
help: .private)
598599
public var explicitlyEnableExperimentalSwiftTestingLibrarySupport: Bool?
599600

601+
<<<<<<< Updated upstream
600602
private func isEnabled(_ library: BuildParameters.Testing.Library, `default`: Bool) -> Bool {
603+
=======
604+
private func isEnabled(_ library: TestingLibrary, `default`: Bool, swiftCommandState: SwiftCommandState) -> Bool {
605+
>>>>>>> Stashed changes
601606
switch library {
602607
case .xctest:
603-
explicitlyEnableXCTestSupport ?? `default`
608+
if let explicitlyEnableXCTestSupport {
609+
return explicitlyEnableXCTestSupport
610+
}
611+
if let toolchain = try? swiftCommandState.getHostToolchain(),
612+
toolchain.swiftSDK.xctestSupport == .supported {
613+
return `default`
614+
}
615+
return false
604616
case .swiftTesting:
605-
explicitlyEnableSwiftTestingLibrarySupport ?? explicitlyEnableExperimentalSwiftTestingLibrarySupport ?? `default`
617+
return explicitlyEnableSwiftTestingLibrarySupport ?? explicitlyEnableExperimentalSwiftTestingLibrarySupport ?? `default`
606618
}
607619
}
608620

609621
/// Test whether or not a given library is enabled.
622+
<<<<<<< Updated upstream
610623
public func isEnabled(_ library: BuildParameters.Testing.Library) -> Bool {
611624
isEnabled(library, default: true)
612625
}
@@ -619,6 +632,15 @@ public struct TestLibraryOptions: ParsableArguments {
619632
/// The list of enabled testing libraries.
620633
public var enabledTestingLibraries: [BuildParameters.Testing.Library] {
621634
[.xctest, .swiftTesting].filter(isEnabled)
635+
=======
636+
public func isEnabled(_ library: TestingLibrary, swiftCommandState: SwiftCommandState) -> Bool {
637+
isEnabled(library, default: true, swiftCommandState: swiftCommandState)
638+
}
639+
640+
/// Test whether or not a given library was explicitly enabled by the developer.
641+
public func isExplicitlyEnabled(_ library: TestingLibrary, swiftCommandState: SwiftCommandState) -> Bool {
642+
isEnabled(library, default: false, swiftCommandState: swiftCommandState)
643+
>>>>>>> Stashed changes
622644
}
623645
}
624646

0 commit comments

Comments
 (0)