Skip to content

Commit 643d458

Browse files
authored
Merge pull request #914 from ahoppen/ahoppen/single-indexed-file-workspace
Add infrastructure to define indexed single-file workspaces inside the tests
2 parents 645b511 + 48b617c commit 643d458

39 files changed

+875
-520
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ let package = Package(
224224
"LSPLogging",
225225
"SKCore",
226226
.product(name: "SwiftPM-auto", package: "swift-package-manager"),
227-
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core")
227+
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
228228
],
229229
exclude: ["CMakeLists.txt"]
230230
),
@@ -245,6 +245,7 @@ let package = Package(
245245
dependencies: [
246246
"CSKTestSupport",
247247
"LSPTestSupport",
248+
"SKCore",
248249
"SourceKitLSP",
249250
.product(name: "ISDBTestSupport", package: "indexstore-db"),
250251
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),

Sources/LanguageServerProtocol/Requests/PollIndexRequest.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@
1818
public struct PollIndexRequest: RequestType {
1919
public static var method: String = "workspace/_pollIndex"
2020
public typealias Response = VoidResponse
21+
22+
public init() {}
2123
}

Sources/SKTestSupport/INPUTS/BasicCXX/Object.h

Lines changed: 0 additions & 5 deletions
This file was deleted.

Sources/SKTestSupport/INPUTS/BasicCXX/main.c

Lines changed: 0 additions & 6 deletions
This file was deleted.

Sources/SKTestSupport/INPUTS/BasicCXX/project.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

Sources/SKTestSupport/INPUTS/CallHierarchy/a.swift

Lines changed: 0 additions & 22 deletions
This file was deleted.

Sources/SKTestSupport/INPUTS/CallHierarchy/project.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

Sources/SKTestSupport/INPUTS/ChangeWorkspaceFolders/Package.swift

Lines changed: 0 additions & 19 deletions
This file was deleted.

Sources/SKTestSupport/INPUTS/ChangeWorkspaceFolders/Sources/otherPackage/otherPackage.swift

Lines changed: 0 additions & 5 deletions
This file was deleted.

Sources/SKTestSupport/INPUTS/ChangeWorkspaceFolders/Sources/package/package.swift

Lines changed: 0 additions & 7 deletions
This file was deleted.

Sources/SKTestSupport/INPUTS/ClangCrashRecovery/main.cpp

Lines changed: 0 additions & 1 deletion
This file was deleted.

Sources/SKTestSupport/INPUTS/ClangCrashRecovery/project.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

Sources/SKTestSupport/INPUTS/ClangCrashRecoveryBuildSettings/main.cpp

Lines changed: 0 additions & 11 deletions
This file was deleted.

Sources/SKTestSupport/INPUTS/ClangCrashRecoveryBuildSettings/project.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

Sources/SKTestSupport/INPUTS/CodeActionCxx/main.cpp

Lines changed: 0 additions & 5 deletions
This file was deleted.

Sources/SKTestSupport/INPUTS/CodeActionCxx/project.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

Sources/SKTestSupport/INPUTS/CodeCompleteSingleModule/CodeCompleteSingleModuleA.swift

Lines changed: 0 additions & 3 deletions
This file was deleted.

Sources/SKTestSupport/INPUTS/CodeCompleteSingleModule/CodeCompleteSingleModuleB.swift

Lines changed: 0 additions & 3 deletions
This file was deleted.

Sources/SKTestSupport/INPUTS/CodeCompleteSingleModule/project.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

Sources/SKTestSupport/INPUTS/Fixit/Fixit.swift

Lines changed: 0 additions & 7 deletions
This file was deleted.

Sources/SKTestSupport/INPUTS/Fixit/project.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

Sources/SKTestSupport/INPUTS/FoldingRange/FoldingRangeBase.swift

Lines changed: 0 additions & 40 deletions
This file was deleted.

Sources/SKTestSupport/INPUTS/FoldingRange/FoldingRangeDuplicateRanges.swift

Lines changed: 0 additions & 3 deletions
This file was deleted.

Sources/SKTestSupport/INPUTS/FoldingRange/FoldingRangeEmptyFile.swift

Lines changed: 0 additions & 1 deletion
This file was deleted.

Sources/SKTestSupport/INPUTS/FoldingRange/FoldingRangeMultilineDocLineComment.swift

Lines changed: 0 additions & 14 deletions
This file was deleted.

Sources/SKTestSupport/INPUTS/FoldingRange/project.json

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Foundation
14+
import ISDBTibs
15+
import LanguageServerProtocol
16+
import SKCore
17+
import SourceKitLSP
18+
import TSCBasic
19+
20+
public struct IndexedSingleSwiftFileWorkspace {
21+
enum Error: Swift.Error {
22+
case swiftcNotFound
23+
}
24+
25+
public let testClient: TestSourceKitLSPClient
26+
public let fileURI: DocumentURI
27+
public let positions: DocumentPositions
28+
29+
public init(
30+
_ markedText: String,
31+
testName: String = #function
32+
) async throws {
33+
let testWorkspaceDirectory = try testScratchDir(testName: testName)
34+
35+
let testFileURL = testWorkspaceDirectory.appendingPathComponent("test.swift")
36+
let indexURL = testWorkspaceDirectory.appendingPathComponent("index")
37+
let indexDBURL = testWorkspaceDirectory.appendingPathComponent("index-db")
38+
guard let swiftc = ToolchainRegistry.shared.default?.swiftc?.asURL else {
39+
throw Error.swiftcNotFound
40+
}
41+
42+
// Create workspace with source file and compile_commands.json
43+
44+
try extractMarkers(markedText).textWithoutMarkers.write(to: testFileURL, atomically: false, encoding: .utf8)
45+
46+
var compilerArguments: [String] = [
47+
testFileURL.path,
48+
"-index-store-path", indexURL.path,
49+
"-index-ignore-system-modules",
50+
"-o", testWorkspaceDirectory.appendingPathComponent("test.o").path,
51+
]
52+
if let sdk = TibsBuilder.defaultSDKPath {
53+
compilerArguments += ["-sdk", sdk]
54+
}
55+
56+
let compilationDatabase = JSONCompilationDatabase(
57+
[
58+
JSONCompilationDatabase.Command(
59+
directory: testWorkspaceDirectory.path,
60+
filename: testFileURL.path,
61+
commandLine: [swiftc.path] + compilerArguments
62+
)
63+
]
64+
)
65+
let encoder = JSONEncoder()
66+
encoder.outputFormatting = .prettyPrinted
67+
try encoder.encode(compilationDatabase).write(
68+
to: testWorkspaceDirectory.appendingPathComponent("compile_commands.json")
69+
)
70+
71+
// Run swiftc to build the index store
72+
try await Process.checkNonZeroExit(arguments: [swiftc.path] + compilerArguments)
73+
74+
// Create the test client
75+
var options = SourceKitServer.Options.testDefault
76+
options.indexOptions = IndexOptions(
77+
indexStorePath: try AbsolutePath(validating: indexURL.path),
78+
indexDatabasePath: try AbsolutePath(validating: indexDBURL.path)
79+
)
80+
self.testClient = try await TestSourceKitLSPClient(
81+
serverOptions: options,
82+
workspaceFolders: [
83+
WorkspaceFolder(uri: DocumentURI(testWorkspaceDirectory))
84+
],
85+
cleanUp: {
86+
try? FileManager.default.removeItem(at: testWorkspaceDirectory)
87+
}
88+
)
89+
90+
// Wait for the indexstore-db to finish indexing
91+
_ = try await testClient.send(PollIndexRequest())
92+
93+
// Open the document
94+
self.fileURI = DocumentURI(testFileURL)
95+
self.positions = testClient.openDocument(markedText, uri: fileURI)
96+
}
97+
}

0 commit comments

Comments
 (0)