@@ -78,11 +78,39 @@ struct SharedOptions: ParsableArguments {
78
78
help: " Enable support for XCTest " )
79
79
var enableXCTestSupport : Bool = true
80
80
81
- /// Whether to enable support for swift-testing.
81
+ /// Storage for whether to enable support for swift-testing.
82
82
@Flag ( name: . customLong( " experimental-swift-testing " ) ,
83
83
inversion: . prefixedEnableDisable,
84
84
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
+ }
86
114
}
87
115
88
116
struct TestToolOptions : ParsableArguments {
@@ -359,7 +387,7 @@ public struct SwiftTestTool: SwiftCommand {
359
387
let command = try List . parse ( )
360
388
try command. run ( swiftTool)
361
389
} else {
362
- if options. sharedOptions. enableSwiftTestingLibrarySupport {
390
+ if try options. sharedOptions. enableSwiftTestingLibrarySupport ( swiftTool : swiftTool ) {
363
391
try swiftTestingRun ( swiftTool)
364
392
}
365
393
if options. sharedOptions. enableXCTestSupport {
@@ -651,7 +679,7 @@ extension SwiftTestTool {
651
679
// MARK: - Common implementation
652
680
653
681
func run( _ swiftTool: SwiftTool ) throws {
654
- if sharedOptions. enableSwiftTestingLibrarySupport {
682
+ if try sharedOptions. enableSwiftTestingLibrarySupport ( swiftTool : swiftTool ) {
655
683
try swiftTestingRun ( swiftTool)
656
684
}
657
685
if sharedOptions. enableXCTestSupport {
@@ -1212,7 +1240,7 @@ extension SwiftTool {
1212
1240
experimentalTestOutput: options. enableExperimentalTestOutput,
1213
1241
library: library
1214
1242
)
1215
- if options. sharedOptions. enableSwiftTestingLibrarySupport {
1243
+ if try options. sharedOptions. enableSwiftTestingLibrarySupport ( swiftTool : self ) {
1216
1244
result. flags. swiftCompilerFlags += [ " -DSWIFT_PM_SUPPORTS_SWIFT_TESTING " ]
1217
1245
}
1218
1246
return result
0 commit comments