Skip to content

Commit 803c360

Browse files
authored
Improve macros test template (#6654)
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 (cherry picked from commit 37a6503)
1 parent 49ec9c6 commit 803c360

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

Sources/Workspace/InitPackage.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,14 +628,19 @@ public final class InitPackage {
628628
import SwiftSyntaxMacros
629629
import SwiftSyntaxMacrosTestSupport
630630
import XCTest
631+
632+
// 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.
633+
#if canImport(\##(moduleName)Macros)
631634
import \##(moduleName)Macros
632635
633636
let testMacros: [String: Macro.Type] = [
634637
"stringify": StringifyMacro.self,
635638
]
639+
#endif
636640
637641
final class \##(moduleName)Tests: XCTestCase {
638-
func testMacro() {
642+
func testMacro() throws {
643+
#if canImport(\##(moduleName)Macros)
639644
assertMacroExpansion(
640645
"""
641646
#stringify(a + b)
@@ -645,9 +650,13 @@ public final class InitPackage {
645650
""",
646651
macros: testMacros
647652
)
653+
#else
654+
throw XCTSkip("macros are only supported when running tests for the host platform")
655+
#endif
648656
}
649657
650-
func testMacroWithStringLiteral() {
658+
func testMacroWithStringLiteral() throws {
659+
#if canImport(\##(moduleName)Macros)
651660
assertMacroExpansion(
652661
#"""
653662
#stringify("Hello, \(name)")
@@ -657,6 +666,9 @@ public final class InitPackage {
657666
"""#,
658667
macros: testMacros
659668
)
669+
#else
670+
throw XCTSkip("macros are only supported when running tests for the host platform")
671+
#endif
660672
}
661673
}
662674

0 commit comments

Comments
 (0)