Skip to content

Add infrastructure to define indexed single-file workspaces inside the tests #914

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 9 commits into from
Oct 24, 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
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ let package = Package(
"LSPLogging",
"SKCore",
.product(name: "SwiftPM-auto", package: "swift-package-manager"),
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core")
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
],
exclude: ["CMakeLists.txt"]
),
Expand All @@ -245,6 +245,7 @@ let package = Package(
dependencies: [
"CSKTestSupport",
"LSPTestSupport",
"SKCore",
"SourceKitLSP",
.product(name: "ISDBTestSupport", package: "indexstore-db"),
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
public struct PollIndexRequest: RequestType {
public static var method: String = "workspace/_pollIndex"
public typealias Response = VoidResponse

public init() {}
}
5 changes: 0 additions & 5 deletions Sources/SKTestSupport/INPUTS/BasicCXX/Object.h

This file was deleted.

6 changes: 0 additions & 6 deletions Sources/SKTestSupport/INPUTS/BasicCXX/main.c

This file was deleted.

1 change: 0 additions & 1 deletion Sources/SKTestSupport/INPUTS/BasicCXX/project.json

This file was deleted.

22 changes: 0 additions & 22 deletions Sources/SKTestSupport/INPUTS/CallHierarchy/a.swift

This file was deleted.

1 change: 0 additions & 1 deletion Sources/SKTestSupport/INPUTS/CallHierarchy/project.json

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion Sources/SKTestSupport/INPUTS/ClangCrashRecovery/main.cpp

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

5 changes: 0 additions & 5 deletions Sources/SKTestSupport/INPUTS/CodeActionCxx/main.cpp

This file was deleted.

1 change: 0 additions & 1 deletion Sources/SKTestSupport/INPUTS/CodeActionCxx/project.json

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

7 changes: 0 additions & 7 deletions Sources/SKTestSupport/INPUTS/Fixit/Fixit.swift

This file was deleted.

1 change: 0 additions & 1 deletion Sources/SKTestSupport/INPUTS/Fixit/project.json

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

8 changes: 0 additions & 8 deletions Sources/SKTestSupport/INPUTS/FoldingRange/project.json

This file was deleted.

97 changes: 97 additions & 0 deletions Sources/SKTestSupport/IndexedSingleSwiftFileWorkspace.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 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 Foundation
import ISDBTibs
import LanguageServerProtocol
import SKCore
import SourceKitLSP
import TSCBasic

public struct IndexedSingleSwiftFileWorkspace {
enum Error: Swift.Error {
case swiftcNotFound
}

public let testClient: TestSourceKitLSPClient
public let fileURI: DocumentURI
public let positions: DocumentPositions

public init(
_ markedText: String,
testName: String = #function
) async throws {
let testWorkspaceDirectory = try testScratchDir(testName: testName)

let testFileURL = testWorkspaceDirectory.appendingPathComponent("test.swift")
let indexURL = testWorkspaceDirectory.appendingPathComponent("index")
let indexDBURL = testWorkspaceDirectory.appendingPathComponent("index-db")
guard let swiftc = ToolchainRegistry.shared.default?.swiftc?.asURL else {
throw Error.swiftcNotFound
}

// Create workspace with source file and compile_commands.json

try extractMarkers(markedText).textWithoutMarkers.write(to: testFileURL, atomically: false, encoding: .utf8)

var compilerArguments: [String] = [
testFileURL.path,
"-index-store-path", indexURL.path,
"-index-ignore-system-modules",
"-o", testWorkspaceDirectory.appendingPathComponent("test.o").path,
]
if let sdk = TibsBuilder.defaultSDKPath {
compilerArguments += ["-sdk", sdk]
}

let compilationDatabase = JSONCompilationDatabase(
[
JSONCompilationDatabase.Command(
directory: testWorkspaceDirectory.path,
filename: testFileURL.path,
commandLine: [swiftc.path] + compilerArguments
)
]
)
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
try encoder.encode(compilationDatabase).write(
to: testWorkspaceDirectory.appendingPathComponent("compile_commands.json")
)

// Run swiftc to build the index store
try await Process.checkNonZeroExit(arguments: [swiftc.path] + compilerArguments)

// Create the test client
var options = SourceKitServer.Options.testDefault
options.indexOptions = IndexOptions(
indexStorePath: try AbsolutePath(validating: indexURL.path),
indexDatabasePath: try AbsolutePath(validating: indexDBURL.path)
)
self.testClient = try await TestSourceKitLSPClient(
serverOptions: options,
workspaceFolders: [
WorkspaceFolder(uri: DocumentURI(testWorkspaceDirectory))
],
cleanUp: {
try? FileManager.default.removeItem(at: testWorkspaceDirectory)
}
)

// Wait for the indexstore-db to finish indexing
_ = try await testClient.send(PollIndexRequest())

// Open the document
self.fileURI = DocumentURI(testFileURL)
self.positions = testClient.openDocument(markedText, uri: fileURI)
}
}
Loading