Skip to content

Commit

Permalink
Add separate "add test" manifest actions for XCTest and Swift Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
DougGregor committed May 8, 2024
1 parent 624d4b6 commit 81699f0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
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))",
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

0 comments on commit 81699f0

Please sign in to comment.