Skip to content

Commit 48b617c

Browse files
committed
Define the sources of ClangCrashRecoveryBuildSettings inline with the tests
1 parent 9780672 commit 48b617c

File tree

6 files changed

+88
-78
lines changed

6 files changed

+88
-78
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class MultiFileTestWorkspace {
6262

6363
public init(
6464
files: [RelativeFileLocation: String],
65+
workspaces: (URL) -> [WorkspaceFolder] = { [WorkspaceFolder(uri: DocumentURI($0))] },
6566
testName: String = #function
6667
) async throws {
6768
scratchDirectory = try testScratchDir(testName: testName)
@@ -92,9 +93,7 @@ public class MultiFileTestWorkspace {
9293
self.fileData = fileData
9394

9495
self.testClient = try await TestSourceKitLSPClient(
95-
workspaceFolders: [
96-
WorkspaceFolder(uri: DocumentURI(scratchDirectory))
97-
],
96+
workspaceFolders: workspaces(scratchDirectory),
9897
cleanUp: { [scratchDirectory] in
9998
try? FileManager.default.removeItem(at: scratchDirectory)
10099
}

Tests/SourceKitDTests/CrashRecoveryTests.swift

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,21 +218,35 @@ final class CrashRecoveryTests: XCTestCase {
218218
func testClangdCrashRecoveryReopensWithCorrectBuildSettings() async throws {
219219
try XCTSkipIf(longTestsDisabled)
220220

221-
let ws = try await staticSourceKitTibsWorkspace(name: "ClangCrashRecoveryBuildSettings")!
222-
let loc = ws.testLoc("loc")
221+
let ws = try await MultiFileTestWorkspace(files: [
222+
"main.cpp": """
223+
#if FOO
224+
void 1️⃣foo2️⃣() {}
225+
#else
226+
void foo() {}
227+
#endif
228+
229+
int main() {
230+
3️⃣foo4️⃣();
231+
}
232+
""",
233+
"compile_flags.txt": """
234+
-DFOO
235+
""",
236+
])
223237

224-
try ws.openDocument(loc.url, language: .cpp)
238+
let (mainUri, positions) = try ws.openDocument("main.cpp")
225239

226240
// Do a sanity check and verify that we get the expected result from a hover response before crashing clangd.
227241

228242
let expectedHighlightResponse = [
229-
DocumentHighlight(range: Position(line: 3, utf16index: 5)..<Position(line: 3, utf16index: 8), kind: .text),
230-
DocumentHighlight(range: Position(line: 9, utf16index: 2)..<Position(line: 9, utf16index: 5), kind: .text),
243+
DocumentHighlight(range: positions["1️⃣"]..<positions["2️⃣"], kind: .text),
244+
DocumentHighlight(range: positions["3️⃣"]..<positions["4️⃣"], kind: .text),
231245
]
232246

233247
let highlightRequest = DocumentHighlightRequest(
234-
textDocument: loc.docIdentifier,
235-
position: Position(line: 9, utf16index: 3)
248+
textDocument: TextDocumentIdentifier(mainUri),
249+
position: positions["3️⃣"]
236250
)
237251
let preCrashHighlightResponse = try await ws.testClient.send(highlightRequest)
238252
precondition(
@@ -242,7 +256,7 @@ final class CrashRecoveryTests: XCTestCase {
242256

243257
// Crash clangd
244258

245-
try await crashClangd(for: ws.testClient, document: loc.docUri)
259+
try await crashClangd(for: ws.testClient, document: mainUri)
246260

247261
// Check that we have re-opened the document with the correct build settings
248262
// If we did not recover the correct build settings, document highlight would

Tests/SourceKitLSPTests/CompilationDatabaseTests.swift

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,64 +13,62 @@
1313
import Foundation
1414
import LanguageServerProtocol
1515
import SKCore
16+
import SKTestSupport
1617
import TSCBasic
1718
import XCTest
1819

1920
final class CompilationDatabaseTests: XCTestCase {
2021
func testModifyCompilationDatabase() async throws {
21-
let ws = try await mutableSourceKitTibsTestWorkspace(name: "ClangCrashRecoveryBuildSettings")!
22-
let loc = ws.testLoc("loc")
22+
let ws = try await MultiFileTestWorkspace(files: [
23+
"main.cpp": """
24+
#if FOO
25+
void 1️⃣foo2️⃣() {}
26+
#else
27+
void 3️⃣foo4️⃣() {}
28+
#endif
2329
24-
try ws.openDocument(loc.url, language: .cpp)
30+
int main() {
31+
5️⃣foo6️⃣();
32+
}
33+
""",
34+
"compile_flags.txt": """
35+
-DFOO
36+
""",
37+
])
38+
39+
let (mainUri, positions) = try ws.openDocument("main.cpp")
2540

2641
// Do a sanity check and verify that we get the expected result from a hover response before modifing the compile commands.
2742

2843
let highlightRequest = DocumentHighlightRequest(
29-
textDocument: loc.docIdentifier,
30-
position: Position(line: 9, utf16index: 3)
44+
textDocument: TextDocumentIdentifier(mainUri),
45+
position: positions["5️⃣"]
3146
)
3247
let preChangeHighlightResponse = try await ws.testClient.send(highlightRequest)
3348
XCTAssertEqual(
3449
preChangeHighlightResponse,
3550
[
36-
DocumentHighlight(range: Position(line: 3, utf16index: 5)..<Position(line: 3, utf16index: 8), kind: .text),
37-
DocumentHighlight(range: Position(line: 9, utf16index: 2)..<Position(line: 9, utf16index: 5), kind: .text),
51+
DocumentHighlight(range: positions["1️⃣"]..<positions["2️⃣"], kind: .text),
52+
DocumentHighlight(range: positions["5️⃣"]..<positions["6️⃣"], kind: .text),
3853
]
3954
)
4055

4156
// Remove -DFOO from the compile commands.
4257

43-
let compilationDatabaseUrl = ws.builder.buildRoot.appendingPathComponent("compile_commands.json")
44-
45-
_ = try ws.sources.edit({ builder in
46-
let compilationDatabase = try XCTUnwrap(
47-
JSONCompilationDatabase(
48-
file: AbsolutePath(validating: compilationDatabaseUrl.path)
49-
)
50-
)
51-
let newCommands = compilationDatabase.allCommands.map {
52-
(command: CompilationDatabaseCompileCommand) -> CompilationDatabaseCompileCommand in
53-
var command = command
54-
command.commandLine.removeAll(where: { $0 == "-DFOO" })
55-
return command
56-
}
57-
let newCompilationDatabase = JSONCompilationDatabase(newCommands)
58-
let newCompilationDatabaseData = try JSONEncoder().encode(newCompilationDatabase)
59-
let newCompilationDatabaseStr = String(data: newCompilationDatabaseData, encoding: .utf8)!
60-
builder.write(newCompilationDatabaseStr, to: compilationDatabaseUrl)
61-
})
58+
let compileFlagsUri = try ws.uri(for: "compile_flags.txt")
59+
try "".write(to: compileFlagsUri.fileURL!, atomically: false, encoding: .utf8)
6260

6361
ws.testClient.send(
6462
DidChangeWatchedFilesNotification(changes: [
65-
FileEvent(uri: DocumentURI(compilationDatabaseUrl), type: .changed)
63+
FileEvent(uri: compileFlagsUri, type: .changed)
6664
])
6765
)
6866

6967
// DocumentHighlight should now point to the definition in the `#else` block.
7068

7169
let expectedPostEditHighlight = [
72-
DocumentHighlight(range: Position(line: 5, utf16index: 5)..<Position(line: 5, utf16index: 8), kind: .text),
73-
DocumentHighlight(range: Position(line: 9, utf16index: 2)..<Position(line: 9, utf16index: 5), kind: .text),
70+
DocumentHighlight(range: positions["3️⃣"]..<positions["4️⃣"], kind: .text),
71+
DocumentHighlight(range: positions["5️⃣"]..<positions["6️⃣"], kind: .text),
7472
]
7573

7674
var didReceiveCorrectHighlight = false

Tests/SourceKitLSPTests/WorkspaceTests.swift

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -118,37 +118,51 @@ final class WorkspaceTests: XCTestCase {
118118
}
119119

120120
func testMultipleClangdWorkspaces() async throws {
121-
guard let ws = try await staticSourceKitTibsWorkspace(name: "ClangModules") else { return }
122-
123-
let loc = ws.testLoc("main_file")
121+
let ws = try await MultiFileTestWorkspace(
122+
files: [
123+
"WorkspaceA/main.cpp": """
124+
#if FOO
125+
void 1️⃣foo2️⃣() {}
126+
#else
127+
void foo() {}
128+
#endif
129+
130+
int main() {
131+
3️⃣foo4️⃣();
132+
}
133+
""",
134+
"WorkspaceA/compile_flags.txt": """
135+
-DFOO
136+
""",
137+
"WorkspaceB/test.m": "",
138+
],
139+
workspaces: { scratchDir in
140+
return [
141+
WorkspaceFolder(uri: DocumentURI(scratchDir.appendingPathComponent("WorkspaceA"))),
142+
WorkspaceFolder(uri: DocumentURI(scratchDir.appendingPathComponent("WorkspaceB"))),
143+
]
144+
}
145+
)
124146

125-
try ws.openDocument(loc.url, language: .objective_c)
147+
_ = try ws.openDocument("test.m")
126148

127149
let diags = try await ws.testClient.nextDiagnosticsNotification()
128150
XCTAssertEqual(diags.diagnostics.count, 0)
129151

130-
let otherWs = try await staticSourceKitTibsWorkspace(
131-
name: "ClangCrashRecoveryBuildSettings",
132-
testClient: ws.testClient
133-
)!
134-
assert(ws.testClient === otherWs.testClient, "Sanity check: The two workspaces should be opened in the same server")
135-
let otherLoc = otherWs.testLoc("loc")
136-
137-
try otherWs.openDocument(otherLoc.url, language: .cpp)
138-
139-
// Do a sanity check and verify that we get the expected result from a hover response before crashing clangd.
140-
141-
let expectedHighlightResponse = [
142-
DocumentHighlight(range: Position(line: 3, utf16index: 5)..<Position(line: 3, utf16index: 8), kind: .text),
143-
DocumentHighlight(range: Position(line: 9, utf16index: 2)..<Position(line: 9, utf16index: 5), kind: .text),
144-
]
152+
let (mainUri, positions) = try ws.openDocument("main.cpp")
145153

146154
let highlightRequest = DocumentHighlightRequest(
147-
textDocument: otherLoc.docIdentifier,
148-
position: Position(line: 9, utf16index: 3)
155+
textDocument: TextDocumentIdentifier(mainUri),
156+
position: positions["3️⃣"]
157+
)
158+
let highlightResponse = try await ws.testClient.send(highlightRequest)
159+
XCTAssertEqual(
160+
highlightResponse,
161+
[
162+
DocumentHighlight(range: positions["1️⃣"]..<positions["2️⃣"], kind: .text),
163+
DocumentHighlight(range: positions["3️⃣"]..<positions["4️⃣"], kind: .text),
164+
]
149165
)
150-
let highlightResponse = try await otherWs.testClient.send(highlightRequest)
151-
XCTAssertEqual(highlightResponse, expectedHighlightResponse)
152166
}
153167

154168
func testRecomputeFileWorkspaceMembershipOnPackageSwiftChange() async throws {

0 commit comments

Comments
 (0)