Skip to content

Commit 19a986d

Browse files
committed
Refactor how isExplicitlyEnabled() works
1 parent bb43131 commit 19a986d

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-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: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,8 @@ 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+
lazy var testEntryPointPath = testProducts.lazy.compactMap(\.testEntryPointPath).first
329+
if options.testLibraryOptions.isExplicitlyEnabled(.swiftTesting) || testEntryPointPath == nil {
336330
results.append(
337331
try await runTestProducts(
338332
testProducts,
@@ -342,6 +336,12 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
342336
library: .swiftTesting
343337
)
344338
)
339+
} else if let testEntryPointPath {
340+
// Cannot run Swift Testing because an entry point file was used and the developer
341+
// didn't explicitly enable Swift Testing.
342+
swiftCommandState.observabilityScope.emit(
343+
debug: "Skipping automatic Swift Testing invocation because a test entry point path is present: \(testEntryPointPath)"
344+
)
345345
}
346346
}
347347

@@ -734,14 +734,8 @@ extension SwiftTestCommand {
734734
}
735735

736736
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 {
737+
lazy var testEntryPointPath = testProducts.lazy.compactMap(\.testEntryPointPath).first
738+
if testLibraryOptions.isExplicitlyEnabled(.swiftTesting) || testEntryPointPath == nil {
745739
let additionalArguments = ["--list-tests"] + CommandLine.arguments.dropFirst()
746740
let runner = TestRunner(
747741
bundlePaths: testProducts.map(\.binaryPath),
@@ -762,6 +756,12 @@ extension SwiftTestCommand {
762756
if result == .failure {
763757
swiftCommandState.executionStatus = .failure
764758
}
759+
} else if let testEntryPointPath {
760+
// Cannot run Swift Testing because an entry point file was used and the developer
761+
// didn't explicitly enable Swift Testing.
762+
swiftCommandState.observabilityScope.emit(
763+
debug: "Skipping automatic Swift Testing invocation (list) because a test entry point path is present: \(testEntryPointPath)"
764+
)
765765
}
766766
}
767767
}

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)