Skip to content

Automatically initialize the SourceKit-LSP server when creating a TestSourceKitLSPClient #911

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 1 commit into from
Oct 20, 2023
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
9 changes: 8 additions & 1 deletion Sources/SKTestSupport/SKSwiftPMTestWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ public final class SKSwiftPMTestWorkspace {
toolchain: Toolchain,
testClient: TestSourceKitLSPClient? = nil
) async throws {
self.testClient = testClient ?? TestSourceKitLSPClient()
self.testClient =
if let testClient {
testClient
} else {
// Don't initialize the LSP server because we wire up all the properties that `InitializeRequest` would set
// manually below.
try await TestSourceKitLSPClient(initialize: false)
}

self.projectDir = URL(
fileURLWithPath: try resolveSymlinks(AbsolutePath(validating: projectDir.path)).pathString
Expand Down
18 changes: 16 additions & 2 deletions Sources/SKTestSupport/SKTibsTestWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ public final class SKTibsTestWorkspace {
clientCapabilities: ClientCapabilities,
testClient: TestSourceKitLSPClient? = nil
) async throws {
self.testClient = testClient ?? TestSourceKitLSPClient()
self.testClient =
if let testClient {
testClient
} else {
// Don't initialize the LSP server because we wire up all the properties that `InitializeRequest` would set
// manually below.
try await TestSourceKitLSPClient(initialize: false)
}
self.tibsWorkspace = try TibsTestWorkspace(
immutableProjectDir: immutableProjectDir,
persistentBuildDir: persistentBuildDir,
Expand All @@ -70,7 +77,14 @@ public final class SKTibsTestWorkspace {
clientCapabilities: ClientCapabilities,
testClient: TestSourceKitLSPClient? = nil
) async throws {
self.testClient = testClient ?? TestSourceKitLSPClient()
self.testClient =
if let testClient {
testClient
} else {
// Don't initialize the LSP server because we wire up all the properties that `InitializeRequest` would set
// manually below.
try await TestSourceKitLSPClient(initialize: false)
}

self.tibsWorkspace = try TibsTestWorkspace(
projectDir: projectDir,
Expand Down
29 changes: 28 additions & 1 deletion Sources/SKTestSupport/TestSourceKitLSPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,22 @@ public final class TestSourceKitLSPClient: MessageHandler {
private var requestHandlers: [Any] = []

/// - Parameters:
/// - serverOptions: The equivalent of the command line options with which sourcekit-lsp should be started
/// - useGlobalModuleCache: If `false`, the server will use its own module
/// cache in an empty temporary directory instead of the global module cache.
public init(serverOptions: SourceKitServer.Options = .testDefault, useGlobalModuleCache: Bool = true) {
/// - initialize: Whether an `InitializeRequest` should be automatically sent to the SourceKit-LSP server.
/// `true` by default
/// - initializationOptions: Initialization options to pass to the SourceKit-LSP server.
/// - capabilities: The test client's capabilities.
/// - workspaceFolders: Workspace folders to open.
public init(
serverOptions: SourceKitServer.Options = .testDefault,
useGlobalModuleCache: Bool = true,
initialize: Bool = true,
initializationOptions: LSPAny? = nil,
capabilities: ClientCapabilities = ClientCapabilities(),
workspaceFolders: [WorkspaceFolder]? = nil
) async throws {
if !useGlobalModuleCache {
moduleCache = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString)
} else {
Expand Down Expand Up @@ -92,6 +105,20 @@ public final class TestSourceKitLSPClient: MessageHandler {
)

self.serverToClientConnection.start(handler: WeakMessageHandler(self))

if initialize {
_ = try await self.send(
InitializeRequest(
processId: nil,
rootPath: nil,
rootURI: nil,
initializationOptions: initializationOptions,
capabilities: capabilities,
trace: .off,
workspaceFolders: workspaceFolders
)
)
}
}

deinit {
Expand Down
14 changes: 1 addition & 13 deletions Tests/SourceKitLSPTests/BuildSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ final class BuildSystemTests: XCTestCase {

override func setUp() async throws {
haveClangd = ToolchainRegistry.shared.toolchains.contains { $0.clangd != nil }
testClient = TestSourceKitLSPClient()
testClient = try await TestSourceKitLSPClient()
buildSystem = TestBuildSystem()

let server = testClient.server
Expand All @@ -112,18 +112,6 @@ final class BuildSystemTests: XCTestCase {

await server.setWorkspaces([workspace])
await workspace.buildSystemManager.setDelegate(server)

_ = try await testClient.send(
InitializeRequest(
processId: nil,
rootPath: nil,
rootURI: nil,
initializationOptions: nil,
capabilities: ClientCapabilities(workspace: nil, textDocument: nil),
trace: .off,
workspaceFolders: nil
)
)
}

override func tearDown() {
Expand Down
29 changes: 4 additions & 25 deletions Tests/SourceKitLSPTests/DocumentColorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,11 @@ import SourceKitLSP
import XCTest

final class DocumentColorTests: XCTestCase {
/// The mock client used to communicate with the SourceKit-LSP server.
///
/// - Note: Set before each test run in `setUp`.
private var testClient: TestSourceKitLSPClient! = nil

override func setUp() async throws {
testClient = TestSourceKitLSPClient()
let documentCapabilities = TextDocumentClientCapabilities()
_ = try await testClient.send(
InitializeRequest(
processId: nil,
rootPath: nil,
rootURI: nil,
initializationOptions: nil,
capabilities: ClientCapabilities(workspace: nil, textDocument: documentCapabilities),
trace: .off,
workspaceFolders: nil
)
)
}

override func tearDown() {
testClient = nil
}

// MARK: - Helpers

private func performDocumentColorRequest(text: String) async throws -> [ColorInformation] {
let testClient = try await TestSourceKitLSPClient()

let uri = DocumentURI.for(.swift)

testClient.openDocument(text, uri: uri)
Expand All @@ -58,6 +35,8 @@ final class DocumentColorTests: XCTestCase {
color: Color,
range: Range<Position>
) async throws -> [ColorPresentation] {
let testClient = try await TestSourceKitLSPClient()

let uri = DocumentURI.for(.swift)

testClient.openDocument(text, uri: uri)
Expand Down
29 changes: 1 addition & 28 deletions Tests/SourceKitLSPTests/DocumentSymbolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,10 @@ import SourceKitLSP
import XCTest

final class DocumentSymbolTests: XCTestCase {
typealias DocumentSymbolCapabilities = TextDocumentClientCapabilities.DocumentSymbol

/// The mock client used to communicate with the SourceKit-LSP server.
///
/// - Note: Set before each test run in `setUp`.
private var testClient: TestSourceKitLSPClient! = nil

override func setUp() async throws {
testClient = TestSourceKitLSPClient()
var documentCapabilities = TextDocumentClientCapabilities()
documentCapabilities.documentSymbol = DocumentSymbolCapabilities()
_ = try await testClient.send(
InitializeRequest(
processId: nil,
rootPath: nil,
rootURI: nil,
initializationOptions: nil,
capabilities: ClientCapabilities(workspace: nil, textDocument: documentCapabilities),
trace: .off,
workspaceFolders: nil
)
)
}

override func tearDown() {
testClient = nil
}

// MARK: - Helpers

private func performDocumentSymbolRequest(text: String) async throws -> DocumentSymbolResponse {
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI.for(.swift)

testClient.openDocument(text, uri: uri)
Expand Down
27 changes: 0 additions & 27 deletions Tests/SourceKitLSPTests/ExecuteCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,6 @@ import SourceKitLSP
import XCTest

final class ExecuteCommandTests: XCTestCase {

/// The mock client used to communicate with the SourceKit-LSP server.
///
/// - Note: Set before each test run in `setUp`.
private var testClient: TestSourceKitLSPClient! = nil

override func tearDown() {
testClient = nil
}

override func setUp() async throws {
testClient = TestSourceKitLSPClient()
_ = try await self.testClient.send(
InitializeRequest(
processId: nil,
rootPath: nil,
rootURI: nil,
initializationOptions: nil,
capabilities: ClientCapabilities(workspace: nil, textDocument: nil),
trace: .off,
workspaceFolders: nil
)
)
}

// MARK: - Tests

func testLocationSemanticRefactoring() async throws {
guard let ws = try await staticSourceKitTibsWorkspace(name: "SemanticRefactor") else { return }
let loc = ws.testLoc("sr:string")
Expand Down
25 changes: 1 addition & 24 deletions Tests/SourceKitLSPTests/InlayHintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,10 @@ import SourceKitLSP
import XCTest

final class InlayHintTests: XCTestCase {
/// The mock client used to communicate with the SourceKit-LSP server.
///
/// - Note: Set before each test run in `setUp`.
private var testClient: TestSourceKitLSPClient! = nil

override func setUp() async throws {
testClient = TestSourceKitLSPClient()
_ = try await testClient.send(
InitializeRequest(
processId: nil,
rootPath: nil,
rootURI: nil,
initializationOptions: nil,
capabilities: ClientCapabilities(workspace: nil, textDocument: nil),
trace: .off,
workspaceFolders: nil
)
)
}

override func tearDown() {
testClient = nil
}

// MARK: - Helpers

func performInlayHintRequest(text: String, range: Range<Position>? = nil) async throws -> [InlayHint] {
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI.for(.swift)

testClient.openDocument(text, uri: uri)
Expand Down
41 changes: 13 additions & 28 deletions Tests/SourceKitLSPTests/LocalClangTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,18 @@ final class LocalClangTests: XCTestCase {
/// - Note: Set before each test run in `setUp`.
private var haveClangd: Bool = false

/// The mock client used to communicate with the SourceKit-LSP server.
///
/// - Note: Set before each test run in `setUp`.
private var testClient: TestSourceKitLSPClient! = nil

override func setUp() async throws {
haveClangd = ToolchainRegistry.shared.toolchains.contains { $0.clangd != nil }
if LocalClangTests.requireClangd && !haveClangd {
XCTFail("cannot find clangd in toolchain")
}

testClient = TestSourceKitLSPClient()
let documentSymbol = TextDocumentClientCapabilities.DocumentSymbol(
dynamicRegistration: nil,
symbolKind: nil,
hierarchicalDocumentSymbolSupport: true
)
let textDocument = TextDocumentClientCapabilities(documentSymbol: documentSymbol)
_ = try await self.testClient.send(
InitializeRequest(
processId: nil,
rootPath: nil,
rootURI: nil,
initializationOptions: nil,
capabilities: ClientCapabilities(workspace: nil, textDocument: textDocument),
trace: .off,
workspaceFolders: nil
)
)
}

override func tearDown() {
testClient = nil
}

// MARK: - Tests

func testSymbolInfo() async throws {
guard haveClangd else { return }
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI.for(.cpp)

let locations = testClient.openDocument(
Expand Down Expand Up @@ -142,6 +115,7 @@ final class LocalClangTests: XCTestCase {

func testFoldingRange() async throws {
guard haveClangd else { return }
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI.for(.cpp)

testClient.openDocument(
Expand Down Expand Up @@ -169,6 +143,17 @@ final class LocalClangTests: XCTestCase {

func testDocumentSymbols() async throws {
guard haveClangd else { return }
let testClient = try await TestSourceKitLSPClient(
capabilities: ClientCapabilities(
textDocument: TextDocumentClientCapabilities(
documentSymbol: TextDocumentClientCapabilities.DocumentSymbol(
dynamicRegistration: nil,
symbolKind: nil,
hierarchicalDocumentSymbolSupport: true
)
)
)
)
let uri = DocumentURI.for(.cpp)

testClient.openDocument(
Expand Down
Loading