Add support for Swift Testing in SwiftSyntaxMacrosTestsSupport#3192
Add support for Swift Testing in SwiftSyntaxMacrosTestsSupport#31920xTim wants to merge 9 commits intoswiftlang:mainfrom
Conversation
| buildConfiguration: buildConfiguration, | ||
| failureHandler: { | ||
| #if canImport(Testing) | ||
| if Test.current != nil { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
What is the correct way? Should we split it out into a expect function instead?
There was a problem hiding this comment.
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 #expect() here and have it work under all configurations.
There was a problem hiding this comment.
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 Test.current issue - from my understanding this works in all instances apart from those running in a detached task. For this specific case, I can't see a scenario when a user would be using the assertMacroExpansion from a detached task so we can fix this for the majority of the users and those attempting to use it from a detached task will see no change in behaviour.
There was a problem hiding this comment.
@stmontgomery Your take? You okay with this presumably being nonfunctional with the package build?
For this specific case, I can't see a scenario when a user would be using the assertMacroExpansion from a detached task so we can fix this for the majority of the users and those attempting to use it from a detached task will see no change in behaviour.
Let's at least document it as unsupported in the symbol's Markup?
There was a problem hiding this comment.
If we can agree here that
SwiftSyntaxcan provideTestingsupport by depending on the toolchain'sTestingmodule can we unblock the PR on this matter?
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.
I think there is still the larger question on how this is currently implemented. The
if Test.current != nilcheck in this method is certainly less than ideal. Instead I would propose that we add an entirely new method calledexpectMacroExpansionthat is based onTestingunconditionally. So existing users ofassertMacroExpansioncan continue to use it withXCTestandTestingusers can start adopting the new one. How does that sound to everyone?
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.
There was a problem hiding this comment.
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.
Only if there is both Testing from the toolchain and from the package right? Which I thought we agreed is only really valid in development environments.
There was a problem hiding this comment.
Only if there is both Testing from the toolchain and from the package right? Which I thought we agreed is only really valid in development environments.
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. #expect() won't be routed to the infrastructure the developer's test target links against, so a failure will be invisible.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
I appreciate the concern, but there are technical blockers here. We must not cause build failures for teams using Swift Testing as a package.
…o use a built-in copy (#1452) This adds a new documentation article within the top-level `Documentation/` directory describing the various ways Swift Testing is distributed and offers the specific recommendation that most users prefer using a built-in copy instead of a package dependency. View the [rendered document](https://github.com/stmontgomery/swift-testing/blob/deployment-locations-doc/Documentation/Distributions.md) for more details. > Note: In this PR the new article is only being placed in the `Documentation/` directory. I chose to place it there, initially, and not within the project's official DocC documentation catalog, but that could potentially happen in the future. ### Motivation: We have been casually giving users the recommendation to prefer built-in copies of the `Testing` module for a while in settings like the Swift forums, bug reports, and informal chats. But thus far, we have not collected and formally documented the reasons why using a package copy can encounter issues. This discussion recently came up during a [discussion](swiftlang/swift-syntax#3192) about whether to adopt Swift Testing in a test-helper library in SwiftSyntax. My hope is that adding this document will help clarify expectations around the level of support we offer for the two distribution styles for Swift Testing. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated. --------- Co-authored-by: Joseph Heck <j_heck@apple.com>
|
Ok all, I've introduced a separate function to be used from Swift testing and added some documentation to the cc @FranzBusch |
|
I think this is still ultimately a moot point with @jerryjrchen's upcoming work? |
I thought we identified in the other thread that this is not the case. While the interoperability will allow the existing XCTest based method to work. It won't work with the |
|
To add some more context as well, I was talking to a couple of maintainers at FOSDEM who have both hit this issue with false positives
This work is still ongoing correct? Because it won't land until 6.4 at the earliest I'd assume |
|
Right but that doesn't mean we should ship new API that immediately becomes obsolete with the next Swift release. |
The end result of Jerry's work is, as I understand it, that the existing API in this library can adopt
I'm not sure I understand how that relates to my comment, but maybe you're referring to something earlier in the conversation I missed? |
We can resolve the |
|
Hi folks, I want to suggest a path forward here which I hope will unblock progress on this PR and allow it to proceed in the short term (if accepted by the repo's code owners, of course!) while laying out a plan for how it will align with our anticipated and fast-approaching interoperability work.
I've assembled a branch (atop this PR branch) with my suggested code changes for this PR in a commit you can see here: stmontgomery@c772dc6. Thoughts? |
|
There are two other points above I wanted to respond to:
Building on what @jerryjrchen said, the semantics of strict mode will be that in any situation where Swift Testing offers an API, that should be preferred over the analogous XCTest variant. It does not mandate that you must only call the APIs corresponding to the active test framework, in case that's something folks are concerned about here. I do think/hope strict mode can eventually be used in conjunction with this work, if end users choose to enable it.
I recognize that this plan does have the potential to break users who have a package dependency on Swift Testing. I suspect the practical impact of that is likely small due to few users relying on it, and I think the tradeoff here is very worthwhile since we'll be delivering a frequently requested feature. The document I wrote about distribution locations of Swift Testing was intended to help clarify our recommendations regarding use of the package copy, and frankly, to discourage its use more strongly than we have in the past. This change will roll out in a new major tag of SwiftSyntax, meaning (per SemVer) it will be an explicit action users must take so the timing of the upgrade will be in their control. And I believe we can mitigate and help users make the transition gracefully with a clearly-worded release note (discussed above). |
|
@stmontgomery looks good to me - happy to pull those changes into my branch as I have done (just some extra changes to add if you agree around the can import stuff and expanding the test info) or have you submit the PR |
|
Sorry I haven't been following this PR, so I may be missing some context. FWIW, the original intent behind the current structure was to take advantage of cross-import overlays once it's accepted and SwiftPM supports them.
import SwiftSyntaxMacrosTestSupport
import SwiftTestingand have That's said I'm a bit skeptical that cross-import overlays will be available in the near future. Also, as I understand it, we likely can't utilize Package traits because we treat XCTest and Testing as system libraries. Given that, plus the direction of the interoperability work, this PR (and the plan from @stmontgomery) seems to be the right direction. A couple of questions:
Just to confirm, this is safe because XCTAssert failure in a Swift Testing test or an #expect failure in an XCTest test is silently ignored (from the interoperability proposal)? What would happen after the interoperability is implemented? Would a failure be reported twice? Or the interoperability feature would consolidate the duplicated failure into one?
Could you elaborate on this? Any example that would break with this change? I don't believe a simple setup like this would work even with the current situation. |
| /// - Warning: If you call this function inside a Swift Testing test case, | ||
| /// the test will report no assertion failures even if the test fails. | ||
| /// Use ``expectMacroExpansion(_:expandedSource:diagnostics:macroSpecs:applyFixIts:fixedSource:testModuleName:testFileName:indentationWidth:buildConfiguration:fileID:file:line:column:)`` instead. | ||
| /// |
There was a problem hiding this comment.
This (and the one on the another function) isn't true now.
There was a problem hiding this comment.
Since which Swift version? Because it's definitely the case in 6.2.3
There was a problem hiding this comment.
I meant after your latest change. You're not even adding expectMacroExpansion anymore. :)
Ah, I see it now. |
As described in Adopt Swift-Testing in test utils such as
SwiftSyntaxMacrosTestSupport#2720SwiftSyntaxMacrosTestsSupportdoes not work with Swift Testing, which is an issue given Swift Testing is the way to test projects and XCTest is deprecated. If produces false positives (where tests pass even if they shouldn't) which is a major issue, especially as there are no warnings.This PR adds support for Swift Testing so that tests fail correctly. This does not introduce an issue with a circular dependency on Swift Testing. There is a circular dependency at the package level, but this is allowed due to swiftlang/swift-package-manager#7530. There is no circular dependency between targets.
SwiftSyntaxMacrosTestsSupportSwiftSyntaxMacrosTestSupport#2720