Skip to content

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

Merged
merged 8 commits into from
Apr 27, 2024

Conversation

DougGregor
Copy link
Member

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.

@DougGregor
Copy link
Member Author

This depends on the SwiftPM changes in swiftlang/swift-package-manager#7474

@DougGregor
Copy link
Member Author

Copy link
Member

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?

Copy link
Member Author

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") {
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 4c1e119

@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@DougGregor DougGregor changed the title Add refactoring action for adding a test target to a package manifest Add refactoring actions for adding a test targets and products to a package manifest Apr 23, 2024
@DougGregor
Copy link
Member Author

@DougGregor
Copy link
Member Author

swiftlang/swift-package-manager#7477

@swift-ci please test Windows

@DougGregor
Copy link
Member Author

swiftlang/swift-package-manager#7477

@swift-ci please test Windows

@DougGregor DougGregor force-pushed the package-editing-refactor branch from e5d9ff0 to 950176b Compare April 23, 2024 14:40
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test Windows

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please test Windows

Copy link
Member

@ahoppen ahoppen left a 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.

Comment on lines +65 to +60
let edits = try AddTarget.addTarget(target, to: scope.file)
return [
CodeAction(
title: "Add test target",
kind: .refactor,
edit: edits.asWorkspaceEdit(snapshot: scope.snapshot)
)
]
Copy link
Member

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.

Copy link
Member Author

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?

Copy link
Member

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)
Copy link
Member

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.

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.
@DougGregor DougGregor force-pushed the package-editing-refactor branch from 950176b to de43434 Compare April 25, 2024 04:26
@DougGregor DougGregor force-pushed the package-editing-refactor branch from de43434 to 3674e7f Compare April 25, 2024 04:57
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test Windows

@DougGregor
Copy link
Member Author

swiftlang/swift-package-manager#7498

@swift-ci please test Windows

@DougGregor
Copy link
Member Author

swiftlang/swift-package-manager#7498

@swift-ci please test Windows

@DougGregor
Copy link
Member Author

@DougGregor
Copy link
Member Author

1 similar comment
@DougGregor
Copy link
Member Author

@DougGregor
Copy link
Member Author

swiftlang/swift#73267

@swift-ci please test Windows

@DougGregor
Copy link
Member Author

}

/// A list of target kinds that allow the creation of tests.
static let targetsThatAllowTests: Set<String> = [
Copy link

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> = [
Copy link

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.

Copy link
Member Author

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

@DougGregor
Copy link
Member Author

swiftlang/swift-package-manager#7509

@swift-ci clean test Windows

@DougGregor
Copy link
Member Author

@DougGregor DougGregor merged commit e0f7221 into swiftlang:main Apr 27, 2024
@DougGregor DougGregor deleted the package-editing-refactor branch April 27, 2024 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants