-
Notifications
You must be signed in to change notification settings - Fork 307
Add refactoring actions for adding a test targets and products to a package manifest #1193
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
Add refactoring actions for adding a test targets and products to a package manifest #1193
Conversation
This depends on the SwiftPM changes in swiftlang/swift-package-manager#7474 |
swiftlang/swift-package-manager#7474 @swift-ci please test |
Sources/LanguageServerProtocol/SupportTypes/TextDocumentEdit.swift
Outdated
Show resolved
Hide resolved
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.
Could you add test cases for these refactorings?
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.
Done in 4c1e119
var actions = [CodeAction]() | ||
|
||
// If there's a target name, offer to create a test target derived from it. | ||
if let targetName = call.findStringArgument(label: "name") { |
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.
Should we also check that we are indeed at a target
? For example I think we shouldn’t offer this when invoking the refactoring on the call to the Package
initializer or on a testTarget
.
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.
Yeah, I want to validate that we're on a kind of target we know how to build a test for.
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.
Done in 4c1e119
@swift-ci please test |
swiftlang/swift-package-manager#7477 @swift-ci please test |
swiftlang/swift-package-manager#7477 @swift-ci please test |
swiftlang/swift-package-manager#7477 @swift-ci please test Windows |
swiftlang/swift-package-manager#7477 @swift-ci please test Windows |
e5d9ff0
to
950176b
Compare
@swift-ci please test |
@swift-ci please test Windows |
1 similar comment
@swift-ci please test Windows |
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.
Thanks, I have left a few more ideas inline.
Sources/SourceKitLSP/Swift/CodeActions/PackageManifestEdits.swift
Outdated
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/PackageManifestEdits.swift
Outdated
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/PackageManifestEdits.swift
Outdated
Show resolved
Hide resolved
let edits = try AddTarget.addTarget(target, to: scope.file) | ||
return [ | ||
CodeAction( | ||
title: "Add test target", | ||
kind: .refactor, | ||
edit: edits.asWorkspaceEdit(snapshot: scope.snapshot) | ||
) | ||
] |
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.
Would it make sense to do some rough check whether a test target with that name already exists? I think even if we just do a textual search for "\(targetName)Tests"
(ie the target name wrapped in quotes), that would cover most cases and would prevent us from showing a code action to add a test target when it obviously already exists.
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.
If we're going to do that check, it should happen down in the SwiftPM code. Personally, I'm not sure... someone might want to use the action and then go rename the text. Are we helping them by taking the action away?
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.
That’s a good point. Let’s leave it for now 👍🏽
targets: [targetName] | ||
) | ||
|
||
let edits = try AddProduct.addProduct(product, to: scope.file) |
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.
Similar here, do you think there’s a way to check if we already have a product for this target? I think textual search wouldn’t work here.
Sources/SourceKitLSP/Swift/CodeActions/PackageManifestEdits.swift
Outdated
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/PackageManifestEdits.swift
Outdated
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/PackageManifestEdits.swift
Outdated
Show resolved
Hide resolved
Leverage the newly-introduced package manifest editing tools in SwiftPM to create a package editing refactoring operation. This operation can be triggered from the a target in the manifest itself, e.g., .target(name: "MyLib") and will add a test target to the package manifest that depends on this target, i.e., .testTarget( name: "MyLibTests", dependencies: [ "MyLib" ] ) It will also create a new source file `Tests/MyLibTests/MyLibTests.swift` that that imports both MyLib and XCTest, and contains an XCTestCase subclass with one test to get you started.
950176b
to
de43434
Compare
de43434
to
3674e7f
Compare
@swift-ci please test |
@swift-ci please test Windows |
swiftlang/swift-package-manager#7498 @swift-ci please test Windows |
swiftlang/swift-package-manager#7498 @swift-ci please test Windows |
swiftlang/swift#73267 @swift-ci please test Windows |
swiftlang/swift#73267 @swift-ci please test Windows |
1 similar comment
swiftlang/swift#73267 @swift-ci please test Windows |
@swift-ci please test Windows |
swiftlang/swift-package-manager#7498 @swift-ci please test Windows |
} | ||
|
||
/// A list of target kinds that allow the creation of tests. | ||
static let targetsThatAllowTests: Set<String> = [ |
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.
I know this isn't particularly performance-sensitive, but this should probably be an Array. A Set is overkill.
} | ||
|
||
/// A list of target kinds that allow the creation of tests. | ||
static let targetsThatAllowProducts: Set<String> = [ |
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.
Same here. An Array would probably be better.
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.
Yes, you're right. I'll clean it up in a subsequent PR, because I'm drowning in unmerged PRs right now
swiftlang/swift-package-manager#7509 @swift-ci clean test Windows |
swiftlang/swift-syntax#2628 @swift-ci clean test Windows |
Leverage the newly-introduced package manifest editing tools in SwiftPM
to create a package editing refactoring operation. This operation can
be triggered from the a target in the manifest itself, e.g.,
and will add a test target to the package manifest that depends on
this target, i.e.,
It will also create a new source file
Tests/MyLibTests/MyLibTests.swift
that that imports both MyLib and XCTest, and contains an XCTestCase subclass
with one test to get you started.