Skip to content
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 separate "add test" manifest actions for XCTest and Swift Testing #1197

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions Sources/SourceKitLSP/Swift/CodeActions/PackageManifestEdits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,36 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
}

do {
// Describe the target we are going to create.
let target = try TargetDescription(
name: "\(targetName)Tests",
dependencies: [.byName(name: targetName, condition: nil)],
type: .test
)
var actions: [CodeAction] = []

let edits = try AddTarget.addTarget(target, to: scope.file)
return [
CodeAction(
title: "Add test target",
kind: .refactor,
edit: edits.asWorkspaceEdit(snapshot: scope.snapshot)
)
let variants: [(AddTarget.TestHarness, String)] = [
(.swiftTesting, "Swift Testing"),
(.xctest, "XCTest"),
]
for (testingLibrary, libraryName) in variants {
// Describe the target we are going to create.
let target = try TargetDescription(
name: "\(targetName)Tests",
dependencies: [.byName(name: targetName, condition: nil)],
type: .test
)

let edits = try AddTarget.addTarget(
target,
to: scope.file,
configuration: .init(testHarness: testingLibrary)
)

actions.append(
CodeAction(
title: "Add test target (\(libraryName))",

Choose a reason for hiding this comment

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

A single test target can contain code for both XCTest and swift-testing. (See my comments over on swiftlang/swift-package-manager#7481.)

Copy link
Member Author

Choose a reason for hiding this comment

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

As noted over in swiftlang/swift-package-manager#7481, a code action that creates a brand-new test target that uses two different test libraries is not useful.

kind: .refactor,
edit: edits.asWorkspaceEdit(snapshot: scope.snapshot)
)
)
}

return actions
} catch {
return []
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/SourceKitLSPTests/CodeActionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -609,12 +609,12 @@ final class CodeActionTests: XCTestCase {

// Make sure we get the expected package manifest editing actions.
let addTestAction = codeActions.first { action in
return action.title == "Add test target"
return action.title == "Add test target (Swift Testing)"
}
XCTAssertNotNil(addTestAction)

guard let addTestChanges = addTestAction?.edit?.documentChanges else {
XCTFail("Didn't have changes in the 'Add test target' action")
XCTFail("Didn't have changes in the 'Add test target (Swift Testing)' action")
return
}

Expand Down