You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow specifying testing libraries for swift package init (#7186)
This PR adds `-enable-experimental-swift-testing` (and `-disable-xctest`
and their inverses) to `swift package init`. These options behave,
broadly, the same as they do for `swift test`. They determine which
testing library a new package will use and adjust the generated template
to match.
It is important to note that any combination of the two libraries is
supported: a developer may wish to use only one or the other, or both,
or may wish to opt out of a test target entirely. All four combinations
are supported, however for simplicity's sake if both libraries are
enabled, we only generate example code for swift-testing.
Note that right now, correct macro target support is impeded by
swiftlang/swift-syntax#2400. I don't think that
issue blocks a change here (since it's in an experimental feature
anyway!) but it does mean that `swift package init --type macro
--enable-experimental-swift-testing` produces some dead tests. Once that
issue is resolved, we can revise the template to produce meaningful
tests instead.
Resolves rdar://99279056.
// Macro implementations build for the host, so the corresponding module is not available when cross-compiling. Cross-compiled tests may still make use of the macro itself in end-to-end tests.
654
-
#if canImport(\##(moduleName)Macros)
655
-
import \##(moduleName)Macros
735
+
content +=##"""
736
+
import SwiftSyntax
737
+
import SwiftSyntaxBuilder
738
+
import SwiftSyntaxMacros
739
+
import SwiftSyntaxMacrosTestSupport
740
+
"""##
656
741
657
-
let testMacros: [String: Macro.Type] = [
658
-
"stringify": StringifyMacro.self,
659
-
]
660
-
#endif
742
+
if options.supportedTestingLibraries.contains(.swiftTesting){
743
+
content +="import Testing\n"
744
+
}
745
+
if options.supportedTestingLibraries.contains(.xctest){
746
+
content +="import XCTest\n"
747
+
}
748
+
749
+
content +=##"""
750
+
751
+
// Macro implementations build for the host, so the corresponding module is not available when cross-compiling. Cross-compiled tests may still make use of the macro itself in end-to-end tests.
752
+
#if canImport(\##(moduleName)Macros)
753
+
import \##(moduleName)Macros
754
+
755
+
let testMacros: [String: Macro.Type] = [
756
+
"stringify": StringifyMacro.self,
757
+
]
758
+
#endif
661
759
760
+
761
+
"""##
762
+
763
+
// Prefer swift-testing if specified, otherwise XCTest. If both are
764
+
// specified, the developer is free to write tests using both
765
+
// libraries, but we still only want to present a single library's
766
+
// example tests.
767
+
if options.supportedTestingLibraries.contains(.swiftTesting){
0 commit comments