Skip to content

Commit 37a6503

Browse files
authored
Improve macros test template (#6646)
The testable module for a macro will only build for the host platform which currently causes somewhat confusing issues if e.g. one is running tests for iOS in Xcode. This change will allow us to skip the tests if they are being run in an unsupported configuration. rdar://110541100
1 parent cefff2b commit 37a6503

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Sources/Workspace/InitPackage.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,14 +641,19 @@ public final class InitPackage {
641641
import SwiftSyntaxMacros
642642
import SwiftSyntaxMacrosTestSupport
643643
import XCTest
644+
645+
// 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.
646+
#if canImport(\##(moduleName)Macros)
644647
import \##(moduleName)Macros
645648
646649
let testMacros: [String: Macro.Type] = [
647650
"stringify": StringifyMacro.self,
648651
]
652+
#endif
649653
650654
final class \##(moduleName)Tests: XCTestCase {
651-
func testMacro() {
655+
func testMacro() throws {
656+
#if canImport(\##(moduleName)Macros)
652657
assertMacroExpansion(
653658
"""
654659
#stringify(a + b)
@@ -658,9 +663,13 @@ public final class InitPackage {
658663
""",
659664
macros: testMacros
660665
)
666+
#else
667+
throw XCTSkip("macros are only supported when running tests for the host platform")
668+
#endif
661669
}
662670
663-
func testMacroWithStringLiteral() {
671+
func testMacroWithStringLiteral() throws {
672+
#if canImport(\##(moduleName)Macros)
664673
assertMacroExpansion(
665674
#"""
666675
#stringify("Hello, \(name)")
@@ -670,6 +679,9 @@ public final class InitPackage {
670679
"""#,
671680
macros: testMacros
672681
)
682+
#else
683+
throw XCTSkip("macros are only supported when running tests for the host platform")
684+
#endif
673685
}
674686
}
675687

0 commit comments

Comments
 (0)