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 Apr 23, 2024
1 parent e5d9ff0 commit cf6a78c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 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 @@ -55,21 +55,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
8 changes: 7 additions & 1 deletion Tests/SourceKitLSPTests/CodeActionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,13 @@ final class CodeActionTests: XCTestCase {
// Make sure we get the expected package manifest editing actions.
XCTAssertTrue(
codeActions.contains { action in
return action.title == "Add test target"
return action.title == "Add test target (Swift Testing)"
}
)

XCTAssertTrue(
codeActions.contains { action in
return action.title == "Add test target (XCTest)"
}
)

Expand Down

0 comments on commit cf6a78c

Please sign in to comment.