Skip to content

Commit cf6f4ae

Browse files
authored
Automatically enable swift-testing support in swift test when it's a dependency. (#7220)
This PR causes `swift test` to check the dependencies of the package under test; if they include an explicit reference to the public swift-testing repository on GitHub, then `--enable-experimental-swift-testing` is assumed. A developer can still opt out by explicitly writing `--disable-experimental-swift-testing`. Resolves rdar://99155992.
1 parent 3035c56 commit cf6f4ae

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

Sources/Commands/SwiftTestTool.swift

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,39 @@ struct SharedOptions: ParsableArguments {
7878
help: "Enable support for XCTest")
7979
var enableXCTestSupport: Bool = true
8080

81-
/// Whether to enable support for swift-testing.
81+
/// Storage for whether to enable support for swift-testing.
8282
@Flag(name: .customLong("experimental-swift-testing"),
8383
inversion: .prefixedEnableDisable,
8484
help: "Enable experimental support for swift-testing")
85-
var enableSwiftTestingLibrarySupport: Bool = false
85+
var _enableSwiftTestingLibrarySupport: Bool?
86+
87+
/// Whether to enable support for swift-testing.
88+
func enableSwiftTestingLibrarySupport(swiftTool: SwiftTool) throws -> Bool {
89+
// Honor the user's explicit command-line selection, if any.
90+
if let callerSuppliedValue = _enableSwiftTestingLibrarySupport {
91+
return callerSuppliedValue
92+
}
93+
94+
// If the active package has a dependency on swift-testing, automatically enable support for it so that extra steps are not needed.
95+
let workspace = try swiftTool.getActiveWorkspace()
96+
let root = try swiftTool.getWorkspaceRoot()
97+
let rootManifests = try temp_await {
98+
workspace.loadRootManifests(
99+
packages: root.packages,
100+
observabilityScope: swiftTool.observabilityScope,
101+
completion: $0
102+
)
103+
}
104+
let isEnabledByDependency = rootManifests.values.lazy
105+
.flatMap(\.dependencies)
106+
.map(\.identity)
107+
.map(String.init(describing:))
108+
.contains("swift-testing")
109+
if isEnabledByDependency {
110+
swiftTool.observabilityScope.emit(debug: "Enabling swift-testing support due to its presence as a package dependency.")
111+
}
112+
return isEnabledByDependency
113+
}
86114
}
87115

88116
struct TestToolOptions: ParsableArguments {
@@ -359,7 +387,7 @@ public struct SwiftTestTool: SwiftCommand {
359387
let command = try List.parse()
360388
try command.run(swiftTool)
361389
} else {
362-
if options.sharedOptions.enableSwiftTestingLibrarySupport {
390+
if try options.sharedOptions.enableSwiftTestingLibrarySupport(swiftTool: swiftTool) {
363391
try swiftTestingRun(swiftTool)
364392
}
365393
if options.sharedOptions.enableXCTestSupport {
@@ -651,7 +679,7 @@ extension SwiftTestTool {
651679
// MARK: - Common implementation
652680

653681
func run(_ swiftTool: SwiftTool) throws {
654-
if sharedOptions.enableSwiftTestingLibrarySupport {
682+
if try sharedOptions.enableSwiftTestingLibrarySupport(swiftTool: swiftTool) {
655683
try swiftTestingRun(swiftTool)
656684
}
657685
if sharedOptions.enableXCTestSupport {
@@ -1212,7 +1240,7 @@ extension SwiftTool {
12121240
experimentalTestOutput: options.enableExperimentalTestOutput,
12131241
library: library
12141242
)
1215-
if options.sharedOptions.enableSwiftTestingLibrarySupport {
1243+
if try options.sharedOptions.enableSwiftTestingLibrarySupport(swiftTool: self) {
12161244
result.flags.swiftCompilerFlags += ["-DSWIFT_PM_SUPPORTS_SWIFT_TESTING"]
12171245
}
12181246
return result

0 commit comments

Comments
 (0)