Files
sourcekit-lsp/Tests/SourceKitLSPTests/CopiedHeaderTests.swift
2025-12-19 19:52:17 +05:30

197 lines
6.1 KiB
Swift
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import BuildServerIntegration
@_spi(SourceKitLSP) import BuildServerProtocol
@_spi(SourceKitLSP) import LanguageServerProtocol
@_spi(Testing) @_spi(SourceKitLSP) import SKLogging
import SKTestSupport
import SwiftExtensions
import XCTest
class CopiedHeaderTests: SourceKitLSPTestCase {
actor BuildServer: CustomBuildServer {
let inProgressRequestsTracker = CustomBuildServerInProgressRequestTracker()
private let projectRoot: URL
private var headerCopyDestination: URL {
projectRoot.appending(components: "header-copy", "CopiedTest.h")
}
init(projectRoot: URL, connectionToSourceKitLSP: any Connection) {
self.projectRoot = projectRoot
}
func initializeBuildRequest(_ request: InitializeBuildRequest) async throws -> InitializeBuildResponse {
return try initializationResponseSupportingBackgroundIndexing(
projectRoot: projectRoot,
outputPathsProvider: false
)
}
func buildTargetSourcesRequest(_ request: BuildTargetSourcesRequest) -> BuildTargetSourcesResponse {
return BuildTargetSourcesResponse(items: [
SourcesItem(
target: .dummy,
sources: [
SourceItem(
uri: DocumentURI(projectRoot.appending(component: "Test.c")),
kind: .file,
generated: false,
dataKind: .sourceKit,
data: SourceKitSourceItemData(
language: .c,
kind: .source,
outputPath: nil,
copyDestinations: nil
).encodeToLSPAny()
),
SourceItem(
uri: DocumentURI(projectRoot.appending(component: "Test.h")),
kind: .file,
generated: false,
dataKind: .sourceKit,
data: SourceKitSourceItemData(
language: .c,
kind: .header,
outputPath: nil,
copyDestinations: [DocumentURI(headerCopyDestination)]
).encodeToLSPAny()
),
]
)
])
}
func textDocumentSourceKitOptionsRequest(
_ request: TextDocumentSourceKitOptionsRequest
) throws -> TextDocumentSourceKitOptionsResponse? {
return TextDocumentSourceKitOptionsResponse(compilerArguments: [
request.textDocument.uri.pseudoPath, "-I", try headerCopyDestination.deletingLastPathComponent().filePath,
])
}
func prepareTarget(_ request: BuildTargetPrepareRequest) async throws -> VoidResponse {
try FileManager.default.createDirectory(
at: headerCopyDestination.deletingLastPathComponent(),
withIntermediateDirectories: true
)
try FileManager.default.copyItem(
at: projectRoot.appending(component: "Test.h"),
to: headerCopyDestination
)
return VoidResponse()
}
}
func testFindReferencesInCopiedHeader() async throws {
let project = try await CustomBuildServerTestProject(
files: [
"Test.h": """
void 1⃣hello();
""",
"Test.c": """
#include <CopiedTest.h>
void test() {
2⃣hello();
}
""",
],
buildServer: BuildServer.self,
enableBackgroundIndexing: true
)
try await project.testClient.send(SynchronizeRequest(copyFileMap: true))
let (uri, positions) = try project.openDocument("Test.c")
let response = try await project.testClient.send(
ReferencesRequest(
textDocument: TextDocumentIdentifier(uri),
position: positions["2"],
context: ReferencesContext(includeDeclaration: true)
)
)
let expected = [
try project.location(from: "2", to: "2", in: "Test.c"),
try project.location(from: "1", to: "1", in: "Test.h"),
]
XCTAssertEqual(response, expected)
}
func testFindDeclarationInCopiedHeader() async throws {
let project = try await CustomBuildServerTestProject(
files: [
"Test.h": """
void 1⃣hello2⃣();
""",
"Test.c": """
#include <CopiedTest.h>
void hello() {}
void test() {
3⃣hello();
}
""",
],
buildServer: BuildServer.self,
enableBackgroundIndexing: true
)
try await project.testClient.send(SynchronizeRequest(copyFileMap: true))
let (uri, positions) = try project.openDocument("Test.c")
let response = try await project.testClient.send(
DeclarationRequest(
textDocument: TextDocumentIdentifier(uri),
position: positions["3"]
)
)
XCTAssertEqual(
response?.locations,
[
try project.location(from: "1", to: "2", in: "Test.h")
]
)
}
func testWorkspaceSymbolsInCopiedHeader() async throws {
let project = try await CustomBuildServerTestProject(
files: [
"Test.h": """
void 1⃣hello();
""",
"Test.c": """
#include <CopiedTest.h>
void test() {
hello();
}
""",
],
buildServer: BuildServer.self,
enableBackgroundIndexing: true
)
try await project.testClient.send(SynchronizeRequest(copyFileMap: true))
_ = try project.openDocument("Test.c")
let response = try await project.testClient.send(
WorkspaceSymbolsRequest(query: "hello")
)
let item = try XCTUnwrap(response?.only)
guard case .symbolInformation(let info) = item else {
XCTFail("Expected a symbol information")
return
}
XCTAssertEqual(info.location, try project.location(from: "1", to: "1", in: "Test.h"))
}
}