Skip to content

Commit 58abe35

Browse files
committed
Refactor how isExplicitlyEnabled() works
1 parent bb43131 commit 58abe35

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

Sources/Commands/PackageCommands/Init.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ extension SwiftPackageCommand {
6363
if testLibraryOptions.isEnabled(.xctest) {
6464
supportedTestingLibraries.insert(.xctest)
6565
}
66-
if let explicitlyEnabled = testLibraryOptions.isExplicitlyEnabled(.swiftTesting), explicitlyEnabled {
66+
if testLibraryOptions.isExplicitlyEnabled(.swiftTesting) {
6767
supportedTestingLibraries.insert(.swiftTesting)
6868
}
6969

Sources/Commands/SwiftTestCommand.swift

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,7 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
325325

326326
// Run Swift Testing (parallel or not, it has a single entry point.)
327327
if options.testLibraryOptions.isEnabled(.swiftTesting) {
328-
if let testEntryPointPath = testProducts.lazy.compactMap(\.testEntryPointPath).first,
329-
options.testLibraryOptions.isExplicitlyEnabled(.swiftTesting) == nil {
330-
// Cannot run Swift Testing because an entry point file was used, and the developer
331-
// didn't explicitly enable Swift Testing.
332-
swiftCommandState.observabilityScope.emit(
333-
debug: "Skipping automatic Swift Testing invocation because a test entry point path is present: \(testEntryPointPath)"
334-
)
335-
} else {
328+
if options.testLibraryOptions.isExplicitlyEnabled(.swiftTesting) || testEntryPointPath == nil {
336329
results.append(
337330
try await runTestProducts(
338331
testProducts,
@@ -342,6 +335,12 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
342335
library: .swiftTesting
343336
)
344337
)
338+
} else if let testEntryPointPath {
339+
// Cannot run Swift Testing because an entry point file was used and the developer
340+
// didn't explicitly enable Swift Testing.
341+
swiftCommandState.observabilityScope.emit(
342+
debug: "Skipping automatic Swift Testing invocation because a test entry point path is present: \(testEntryPointPath)"
343+
)
345344
}
346345
}
347346

@@ -734,14 +733,8 @@ extension SwiftTestCommand {
734733
}
735734

736735
if testLibraryOptions.isEnabled(.swiftTesting) {
737-
if let testEntryPointPath = testProducts.lazy.compactMap(\.testEntryPointPath).first,
738-
testLibraryOptions.isExplicitlyEnabled(.swiftTesting) == nil {
739-
// Cannot run Swift Testing because an entry point file was used, and the developer
740-
// didn't explicitly enable Swift Testing.
741-
swiftCommandState.observabilityScope.emit(
742-
debug: "Skipping automatic Swift Testing invocation (list) because a test entry point path is present: \(testEntryPointPath)"
743-
)
744-
} else {
736+
lazy var testEntryPointPath = testProducts.lazy.compactMap(\.testEntryPointPath).first
737+
if testLibraryOptions.isExplicitlyEnabled(.swiftTesting) || testEntryPointPath == nil {
745738
let additionalArguments = ["--list-tests"] + CommandLine.arguments.dropFirst()
746739
let runner = TestRunner(
747740
bundlePaths: testProducts.map(\.binaryPath),
@@ -762,6 +755,12 @@ extension SwiftTestCommand {
762755
if result == .failure {
763756
swiftCommandState.executionStatus = .failure
764757
}
758+
} else if let testEntryPointPath {
759+
// Cannot run Swift Testing because an entry point file was used and the developer
760+
// didn't explicitly enable Swift Testing.
761+
swiftCommandState.observabilityScope.emit(
762+
debug: "Skipping automatic Swift Testing invocation (list) because a test entry point path is present: \(testEntryPointPath)"
763+
)
765764
}
766765
}
767766
}

Sources/CoreCommands/Options.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -597,24 +597,23 @@ public struct TestLibraryOptions: ParsableArguments {
597597
help: .private)
598598
public var explicitlyEnableExperimentalSwiftTestingLibrarySupport: Bool?
599599

600-
/// Test whether or not a given library is enabled.
601-
public func isEnabled(_ library: BuildParameters.Testing.Library) -> Bool {
600+
private func isEnabled(_ library: BuildParameters.Testing.Library, `default`: Bool) -> Bool {
602601
switch library {
603602
case .xctest:
604-
explicitlyEnableXCTestSupport ?? true
603+
explicitlyEnableXCTestSupport ?? `default`
605604
case .swiftTesting:
606-
explicitlyEnableSwiftTestingLibrarySupport ?? explicitlyEnableExperimentalSwiftTestingLibrarySupport ?? true
605+
explicitlyEnableSwiftTestingLibrarySupport ?? explicitlyEnableExperimentalSwiftTestingLibrarySupport ?? `default`
607606
}
608607
}
609608

609+
/// Test whether or not a given library is enabled.
610+
public func isEnabled(_ library: BuildParameters.Testing.Library) -> Bool {
611+
isEnabled(library, default: true)
612+
}
613+
610614
/// Test whether or not a given library was explicitly enabled by the developer.
611-
public func isExplicitlyEnabled(_ library: BuildParameters.Testing.Library) -> Bool? {
612-
switch library {
613-
case .xctest:
614-
explicitlyEnableXCTestSupport
615-
case .swiftTesting:
616-
explicitlyEnableSwiftTestingLibrarySupport ?? explicitlyEnableExperimentalSwiftTestingLibrarySupport
617-
}
615+
public func isExplicitlyEnabled(_ library: BuildParameters.Testing.Library) -> Bool {
616+
isEnabled(library, default: false)
618617
}
619618

620619
/// The list of enabled testing libraries.

0 commit comments

Comments
 (0)