-
Notifications
You must be signed in to change notification settings - Fork 470
Add support for Swift Testing in SwiftSyntaxMacrosTestsSupport #3192
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?
Changes from 3 commits
2ddc19b
ba15ee0
dff2179
d7fd8ff
8f55686
be537f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,9 @@ public import SwiftSyntaxMacroExpansion | |
| public import SwiftSyntaxMacros | ||
| @_spi(XCTestFailureLocation) public import SwiftSyntaxMacrosGenericTestSupport | ||
| private import XCTest | ||
| #if canImport(Testing) | ||
| import Testing | ||
| #endif | ||
| #else | ||
| import SwiftIfConfig | ||
| import SwiftSyntax | ||
|
|
@@ -61,8 +64,10 @@ public func assertMacroExpansion( | |
| testFileName: String = "test.swift", | ||
| indentationWidth: Trivia = .spaces(4), | ||
| buildConfiguration: (any BuildConfiguration)? = nil, | ||
| fileID: StaticString = #fileID, | ||
| file: StaticString = #filePath, | ||
| line: UInt = #line | ||
| line: UInt = #line, | ||
| column: UInt = #column | ||
| ) { | ||
| let specs = macros.mapValues { MacroSpec(type: $0) } | ||
| assertMacroExpansion( | ||
|
|
@@ -76,8 +81,10 @@ public func assertMacroExpansion( | |
| testFileName: testFileName, | ||
| indentationWidth: indentationWidth, | ||
| buildConfiguration: buildConfiguration, | ||
| fileID: fileID, | ||
| file: file, | ||
| line: line | ||
| line: line, | ||
| column: column | ||
| ) | ||
| } | ||
|
|
||
|
|
@@ -110,8 +117,10 @@ public func assertMacroExpansion( | |
| testFileName: String = "test.swift", | ||
| indentationWidth: Trivia = .spaces(4), | ||
| buildConfiguration: (any BuildConfiguration)? = nil, | ||
| fileID: StaticString = #fileID, | ||
| file: StaticString = #filePath, | ||
| line: UInt = #line | ||
| line: UInt = #line, | ||
| column: UInt = #column | ||
| ) { | ||
| SwiftSyntaxMacrosGenericTestSupport.assertMacroExpansion( | ||
| originalSource, | ||
|
|
@@ -125,7 +134,15 @@ public func assertMacroExpansion( | |
| indentationWidth: indentationWidth, | ||
| buildConfiguration: buildConfiguration, | ||
| failureHandler: { | ||
| #if canImport(Testing) | ||
| if Test.current != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the correct test to determine which library is in use because code can run in a detached task. See swiftlang/swift-testing#475
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the correct way? Should we split it out into a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no correct way at this time, which is why that issue is still open. Jerry's work should allow us to just call
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The pitch looks like it will solve the issue, but still require work in the library to migrate over to Swift Testing APIs. What I propose is that we land this now, as it solves a problem that exists for users today (and potentially provide a release in the next monthly Linux release/Swift patch release) and then fix forward when the proposal lands. Given it's still in the pitch phase it likely won't be landed until 6.4 and waiting 10 months for a solution seems like a bad idea. Regarding the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @stmontgomery Your take? You okay with this presumably being nonfunctional with the package build?
Let's at least document it as unsupported in the symbol's Markup?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'd be morally okay with saying "the features in the swift-syntax repository are dependent on the built-in copy of Swift Testing even if you include a package dependency" however this will break builds on non-Apple platforms with flat linker namespaces due to duplicate symbols at link time.
Once @jerryjrchen's work on the interop feature lands, it will be possible to implement this in a way that depends on neither XCTest nor Swift Testing. It may be a better idea to just wait until that work is done and revisit the problem at that point.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Only if there is both
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
All testing environments are development environments. If swift-syntax explicitly links the copy of Swift Testing in the toolchain, that will break developers who want to test their macros and have a package dependency on Swift Testing. In addition, if they are using the package copy of Swift Testing and you are using the toolchain's copy, your calls to e.g. Jerry's work should give us an escape hatch for this problem, so we should wait until it lands and then make the necessary changes here.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My concern with waiting is that it could be a year until that's shipped, meanwhile users have no indications their tests are passing incorrectly, whereas we could fix it today for the majority of use cases
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I appreciate the concern, but there are technical blockers here. We must not cause build failures for teams using Swift Testing as a package. |
||
| Issue.record(Comment(rawValue: $0.message), sourceLocation: .init(fileID: fileID.description, filePath: file.description, line: Int(line), column: Int(column))) | ||
| } else { | ||
| XCTFail($0.message, file: $0.location.staticFilePath, line: $0.location.unsignedLine) | ||
| } | ||
| #else | ||
| XCTFail($0.message, file: $0.location.staticFilePath, line: $0.location.unsignedLine) | ||
| #endif | ||
| }, | ||
| fileID: "", // Not used in the failure handler | ||
| filePath: file, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #if canImport(Testing) | ||
| import Testing | ||
| import SwiftSyntaxMacrosTestSupport | ||
|
|
||
| @Suite("Swift Testing Macro Expansion Tests") | ||
| struct SwiftTestingMacroExpansionTests { | ||
| @Test("Test Happy Path") | ||
| func testHappyPathWorks() { | ||
| assertMacroExpansion( | ||
| """ | ||
| @constantOne | ||
| var x: Int /*1*/ // hello | ||
| """, | ||
| expandedSource: """ | ||
| var x: Int { /*1*/ // hello | ||
| get { | ||
| return 1 | ||
| } | ||
| } | ||
| """, | ||
| macros: ["constantOne": ConstantOneGetter.self], | ||
| indentationWidth: .spaces(2) | ||
| ) | ||
| } | ||
|
|
||
| @Test("Test Failure") | ||
| func failureReportedCorrectly() { | ||
| withKnownIssue { | ||
| assertMacroExpansion( | ||
| """ | ||
| @constantOne | ||
| var x: Int /*1*/ // hello | ||
| """, | ||
| expandedSource: """ | ||
| var x: Int { /*1*/ // hello | ||
| get { | ||
| return 1 | ||
| } | ||
| } | ||
| """, | ||
| macros: ["constantOne": ConstantOneGetter.self], | ||
| indentationWidth: .spaces(4) | ||
| ) | ||
| } matching: { issue in | ||
| issue.description.contains("Macro expansion did not produce the expected expanded source") | ||
| } | ||
| } | ||
| } | ||
| #endif |
Uh oh!
There was an error while loading. Please reload this page.