-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Update macro template to support Swift Testing tests #8890 #8897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Update macro template to support Swift Testing tests #8890 #8897
Conversation
Add support for macro project using Swift Testing.
This resolves #8890 |
Ahh… just found this one! I hope we can ship a solution to this soon. FWIW it might be worth highlighting something to engineers reminding them this support would require swift-syntax 600: #if canImport(SwiftSyntax600)
...
#else
// error? or warning?
#endif It might be confusing to engineers if they create a macro target with this template and then try to back support swift-syntax 510. |
@@ -726,15 +720,22 @@ public final class InitPackage { | |||
import SwiftSyntax | |||
import SwiftSyntaxBuilder | |||
import SwiftSyntaxMacros | |||
import SwiftSyntaxMacrosTestSupport | |||
import SwiftSyntaxMacroExpansion |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should also import SwiftSyntaxMacrosGenericTestSupport
?
Ahh… nevermind… I see it below!
Issue.record( | ||
"\($0.message)", | ||
sourceLocation: | ||
SourceLocation( | ||
fileID: $0.location.fileID, | ||
filePath: $0.location.filePath, | ||
line: $0.location.line, | ||
column: $0.location.column | ||
) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nit: Could this be refactored to an extension
on Issue
? Then we can put this work in one place instead of duplicated it across N different tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extension Issue {
@discardableResult static func record(_ failure: TestFailureSpec) -> Self {
Self.record(
Comment(rawValue: failure.message),
sourceLocation: SourceLocation(
fileID: failure.location.fileID,
filePath: failure.location.filePath,
line: failure.location.line,
column: failure.location.column
)
)
}
}
cc @grynspan Was there anything blocking us from shipping that extension directly in swift-testing
? Or is there another canonical way to bridge between SwiftSyntaxMacrosGenericTestSupport.TestFailureSpec
and Testing.Issue
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also choose to factor this to a free function:
func failureHander(_ failure: TestFailureSpec) {
Issue.record(
Comment(rawValue: failure.message),
sourceLocation:
SourceLocation(
fileID: failure.location.fileID,
filePath: failure.location.filePath,
line: failure.location.line,
column: failure.location.column
)
)
}
And then pass that failureHander
to every test similar to how we pass testMacros
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Swift Testing cannot directly link to swift-syntax since it doesn't exist in the toolchain at runtime. That means we cannot export any symbols that reference types from swift-syntax (except in our macro target which is only loaded/run at compile time.)
we should work the the swift-syntax owners and the Testing Workgroup to ensure this aligns with the overall vision. |
Add support for macro project using Swift Testing in
InitPackage
.Motivation:
Since the introduction of
SwiftSyntaxMacrosGenericTestSupport
, Swift Testing should be supported to test macroModifications:
writeMacroTestsFile
to support Swift TestingMacroSpec
for both XCTest and Swift Testingswift-testing
dependency from the Package manifest when building macro template for Swift TestingunsupportedTestingLibraryForPackageType
error as it's unusedResult:
Generated test file when using Swift Testing:
Fixes: #8890