Skip to content

Commit 31cafe7

Browse files
committed
Define sources of ChangeWorkspaceFolders inline
1 parent c277875 commit 31cafe7

File tree

6 files changed

+71
-69
lines changed

6 files changed

+71
-69
lines changed

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/MultiFileTestWorkspace.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public struct RelativeFileLocation: Hashable, ExpressibleByStringLiteral {
2828
}
2929

3030
public init(stringLiteral value: String) {
31-
self.init(value)
31+
let components = value.components(separatedBy: "/")
32+
self.init(directories: components.dropLast(), components.last!)
3233
}
3334
}
3435

Sources/SKTestSupport/SwiftPMTestWorkspace.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class SwiftPMTestWorkspace: MultiFileTestWorkspace {
3838
public init(
3939
files: [String: String],
4040
manifest: String = SwiftPMTestWorkspace.defaultPackageManifest,
41-
index: Bool = false,
41+
build: Bool = false,
4242
testName: String = #function
4343
) async throws {
4444
var filesByPath: [RelativeFileLocation: String] = [:]
@@ -55,8 +55,15 @@ public class SwiftPMTestWorkspace: MultiFileTestWorkspace {
5555
throw Error.swiftNotFound
5656
}
5757

58-
if index {
59-
try await Process.checkNonZeroExit(arguments: [swift.path, "build", "--package-path", scratchDirectory.path])
58+
if build {
59+
let arguments = [
60+
swift.path,
61+
"build",
62+
"--package-path", scratchDirectory.path,
63+
"-Xswiftc", "-index-ignore-system-modules",
64+
"-Xcc", "-index-ignore-system-symbols",
65+
]
66+
try await Process.checkNonZeroExit(arguments: arguments)
6067
}
6168
// Wait for the indexstore-db to finish indexing
6269
_ = try await testClient.send(PollIndexRequest())

Tests/SourceKitLSPTests/WorkspaceTests.swift

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -239,42 +239,67 @@ final class WorkspaceTests: XCTestCase {
239239
}
240240

241241
func testChangeWorkspaceFolders() async throws {
242-
guard let ws = try await staticSourceKitSwiftPMWorkspace(name: "ChangeWorkspaceFolders") else { return }
243-
// Build the package. We can't use ws.buildAndIndex() because that doesn't put the build products in .build where SourceKit-LSP expects them.
242+
let ws = try await MultiFileTestWorkspace(
243+
files: [
244+
"subdir/Sources/otherPackage/otherPackage.swift": """
245+
import package
246+
247+
func test() {
248+
Package().1️⃣helloWorld()
249+
}
250+
""",
251+
"subdir/Sources/package/package.swift": """
252+
public struct Package {
253+
public init() {}
254+
255+
public func helloWorld() {
256+
print("Hello world!")
257+
}
258+
}
259+
""",
260+
"subdir/Package.swift": """
261+
// swift-tools-version: 5.5
262+
263+
import PackageDescription
264+
265+
let package = Package(
266+
name: "package",
267+
products: [
268+
.library(name: "package", targets: ["package"]),
269+
.library(name: "otherPackage", targets: ["otherPackage"]),
270+
],
271+
targets: [
272+
.target(
273+
name: "package",
274+
dependencies: []
275+
),
276+
.target(
277+
name: "otherPackage",
278+
dependencies: ["package"]
279+
),
280+
]
281+
)
282+
""",
283+
]
284+
)
285+
286+
let packageDir = try ws.uri(for: "Package.swift").fileURL!.deletingLastPathComponent()
287+
244288
try await TSCBasic.Process.checkNonZeroExit(arguments: [
245289
ToolchainRegistry.shared.default!.swift!.pathString,
246290
"build",
247-
"--package-path", ws.sources.rootDirectory.path,
291+
"--package-path", packageDir.path,
248292
"-Xswiftc", "-index-ignore-system-modules",
249293
"-Xcc", "-index-ignore-system-symbols",
250294
])
251295

252-
let otherPackLoc = ws.testLoc("otherPackage:call")
253-
254-
let testClient = try await TestSourceKitLSPClient(
255-
capabilities: ClientCapabilities(
256-
workspace: .init(workspaceFolders: true)
257-
),
258-
workspaceFolders: [WorkspaceFolder(uri: DocumentURI(ws.sources.rootDirectory.deletingLastPathComponent()))]
259-
)
296+
let (otherPackageUri, positions) = try ws.openDocument("otherPackage.swift")
297+
let testPosition = positions["1️⃣"]
260298

261-
let docString = try String(data: Data(contentsOf: otherPackLoc.url), encoding: .utf8)!
262-
263-
testClient.send(
264-
DidOpenTextDocumentNotification(
265-
textDocument: TextDocumentItem(
266-
uri: otherPackLoc.docUri,
267-
language: .swift,
268-
version: 1,
269-
text: docString
270-
)
271-
)
272-
)
273-
274-
let preChangeWorkspaceResponse = try await testClient.send(
299+
let preChangeWorkspaceResponse = try await ws.testClient.send(
275300
CompletionRequest(
276-
textDocument: TextDocumentIdentifier(otherPackLoc.docUri),
277-
position: otherPackLoc.position
301+
textDocument: TextDocumentIdentifier(otherPackageUri),
302+
position: testPosition
278303
)
279304
)
280305

@@ -284,18 +309,18 @@ final class WorkspaceTests: XCTestCase {
284309
"Did not expect to receive cross-module code completion results if we opened the parent directory of the package"
285310
)
286311

287-
testClient.send(
312+
ws.testClient.send(
288313
DidChangeWorkspaceFoldersNotification(
289314
event: WorkspaceFoldersChangeEvent(added: [
290-
WorkspaceFolder(uri: DocumentURI(ws.sources.rootDirectory))
315+
WorkspaceFolder(uri: DocumentURI(packageDir))
291316
])
292317
)
293318
)
294319

295-
let postChangeWorkspaceResponse = try await testClient.send(
320+
let postChangeWorkspaceResponse = try await ws.testClient.send(
296321
CompletionRequest(
297-
textDocument: TextDocumentIdentifier(otherPackLoc.docUri),
298-
position: otherPackLoc.position
322+
textDocument: TextDocumentIdentifier(otherPackageUri),
323+
position: testPosition
299324
)
300325
)
301326

@@ -314,7 +339,7 @@ final class WorkspaceTests: XCTestCase {
314339
insertTextFormat: .plain,
315340
textEdit: .textEdit(
316341
TextEdit(
317-
range: otherPackLoc.position..<otherPackLoc.position,
342+
range: Range(testPosition),
318343
newText: "helloWorld()"
319344
)
320345
)
@@ -331,7 +356,7 @@ final class WorkspaceTests: XCTestCase {
331356
insertTextFormat: .plain,
332357
textEdit: .textEdit(
333358
TextEdit(
334-
range: otherPackLoc.position..<otherPackLoc.position,
359+
range: Range(testPosition),
335360
newText: "self"
336361
)
337362
)

0 commit comments

Comments
 (0)