Skip to content

Commit 346c978

Browse files
authored
Merge pull request #2290 from ahoppen/custom-build-server-test-project
Use `CustomBuildServerTestProject` in `BuildServerTests`
2 parents 016a0b1 + daf16c4 commit 346c978

File tree

3 files changed

+92
-162
lines changed

3 files changed

+92
-162
lines changed

Sources/SKTestSupport/CustomBuildServerTestProject.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ package final class CustomBuildServerTestProject<BuildServer: CustomBuildServer>
278278
options: SourceKitLSPOptions? = nil,
279279
hooks: Hooks = Hooks(),
280280
enableBackgroundIndexing: Bool = false,
281+
usePullDiagnostics: Bool = true,
281282
pollIndex: Bool = true,
282283
preInitialization: ((TestSourceKitLSPClient) -> Void)? = nil,
283284
testScratchDir: URL? = nil,
@@ -296,6 +297,7 @@ package final class CustomBuildServerTestProject<BuildServer: CustomBuildServer>
296297
options: options,
297298
hooks: hooks,
298299
enableBackgroundIndexing: enableBackgroundIndexing,
300+
usePullDiagnostics: usePullDiagnostics,
299301
preInitialization: preInitialization,
300302
testScratchDir: testScratchDir,
301303
testName: testName

Sources/SourceKitLSP/Workspace.swift

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -363,26 +363,6 @@ package final class Workspace: Sendable, BuildServerManagerDelegate {
363363
await scheduleUpdateOfUnitOutputPathsInIndexIfNecessary()
364364
}
365365

366-
package static func forTesting(
367-
options: SourceKitLSPOptions,
368-
sourceKitLSPServer: SourceKitLSPServer,
369-
testHooks: Hooks,
370-
buildServerManager: BuildServerManager,
371-
indexTaskScheduler: TaskScheduler<AnyIndexTaskDescription>
372-
) async -> Workspace {
373-
return await Workspace(
374-
sourceKitLSPServer: sourceKitLSPServer,
375-
rootUri: nil,
376-
capabilityRegistry: CapabilityRegistry(clientCapabilities: ClientCapabilities()),
377-
options: options,
378-
hooks: testHooks,
379-
buildServerManager: buildServerManager,
380-
index: nil,
381-
indexDelegate: nil,
382-
indexTaskScheduler: indexTaskScheduler
383-
)
384-
}
385-
386366
/// Returns a `CheckedIndex` that verifies that all the returned entries are up-to-date with the given
387367
/// `IndexCheckLevel`.
388368
package func index(checkedFor checkLevel: IndexCheckLevel) -> CheckedIndex? {

Tests/SourceKitLSPTests/BuildServerTests.swift

Lines changed: 90 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -49,151 +49,94 @@ fileprivate actor TestBuildServer: CustomBuildServer {
4949
}
5050

5151
final class BuildServerTests: XCTestCase {
52-
/// The mock client used to communicate with the SourceKit-LSP server.p
53-
///
54-
/// - Note: Set before each test run in `setUp`.
55-
private var testClient: TestSourceKitLSPClient! = nil
56-
57-
/// The server's workspace data. Accessing this is unsafe if the server does so concurrently.
58-
///
59-
/// - Note: Set before each test run in `setUp`.
60-
private var workspace: Workspace! = nil
61-
62-
/// The build server that we use to verify SourceKitLSPServer behavior.
63-
///
64-
/// - Note: Set before each test run in `setUp`.
65-
private var buildServer: TestBuildServer! = nil
66-
67-
/// Whether clangd exists in the toolchain.
68-
///
69-
/// - Note: Set before each test run in `setUp`.
70-
private var haveClangd: Bool = false
71-
72-
override func setUp() async throws {
73-
testClient = try await TestSourceKitLSPClient(usePullDiagnostics: false)
74-
75-
let server = testClient.server
76-
77-
let testBuildServer = ThreadSafeBox<TestBuildServer?>(initialValue: nil)
78-
let buildServerManager = await BuildServerManager(
79-
buildServerSpec: BuildServerSpec(
80-
kind: .injected({ projectRoot, connectionToSourceKitLSP in
81-
assert(testBuildServer.value == nil, "Build server injector hook can only create a single TestBuildServer")
82-
let buildServer = TestBuildServer(
83-
projectRoot: projectRoot,
84-
connectionToSourceKitLSP: connectionToSourceKitLSP
85-
)
86-
testBuildServer.value = buildServer
87-
return LocalConnection(receiverName: "TestBuildServer", handler: buildServer)
88-
}),
89-
projectRoot: URL(fileURLWithPath: "/"),
90-
configPath: URL(fileURLWithPath: "/")
91-
),
92-
toolchainRegistry: .forTesting,
93-
options: try .testDefault(),
94-
connectionToClient: DummyBuildServerManagerConnectionToClient(),
95-
buildServerHooks: BuildServerHooks()
96-
)
97-
buildServer = try unwrap(testBuildServer.value)
98-
99-
self.workspace = await Workspace.forTesting(
100-
options: try .testDefault(),
101-
sourceKitLSPServer: server,
102-
testHooks: Hooks(),
103-
buildServerManager: buildServerManager,
104-
indexTaskScheduler: .forTesting
105-
)
106-
107-
await server.setWorkspaces([(workspace: workspace, isImplicit: false)])
108-
await workspace.buildServerManager.setDelegate(workspace)
109-
}
110-
111-
override func tearDown() {
112-
buildServer = nil
113-
workspace = nil
114-
testClient = nil
115-
}
116-
117-
// MARK: - Tests
118-
11952
func testClangdDocumentUpdatedBuildSettings() async throws {
120-
guard haveClangd else { return }
121-
122-
let doc = DocumentURI(for: .objective_c)
123-
let args = [doc.pseudoPath, "-DDEBUG"]
124-
let text = """
125-
#ifdef FOO
126-
static void foo() {}
127-
#endif
128-
129-
int main() {
130-
foo();
131-
return 0;
132-
}
133-
"""
53+
let project = try await CustomBuildServerTestProject(
54+
files: [
55+
"test.c": """
56+
#ifdef FOO
57+
static void foo() {}
58+
#endif
13459
135-
await buildServer.setBuildSettings(for: doc, to: TextDocumentSourceKitOptionsResponse(compilerArguments: args))
60+
int main() {
61+
foo();
62+
return 0;
63+
}
64+
"""
65+
],
66+
buildServer: TestBuildServer.self,
67+
usePullDiagnostics: false
68+
)
13669

137-
let documentManager = self.testClient.server.documentManager
70+
let args = [try project.uri(for: "test.c").pseudoPath, "-DDEBUG"]
71+
try await project.buildServer().setBuildSettings(
72+
for: project.uri(for: "test.c"),
73+
to: TextDocumentSourceKitOptionsResponse(compilerArguments: args)
74+
)
13875

139-
testClient.openDocument(text, uri: doc)
76+
let (uri, _) = try project.openDocument("test.c")
14077

141-
let diags = try await testClient.nextDiagnosticsNotification()
78+
let diags = try await project.testClient.nextDiagnosticsNotification()
14279
XCTAssertEqual(diags.diagnostics.count, 1)
143-
XCTAssertEqual(text, try documentManager.latestSnapshot(doc).text)
14480

14581
// Modify the build settings and inform the delegate.
14682
// This should trigger a new publish diagnostics and we should no longer have errors.
14783
let newSettings = TextDocumentSourceKitOptionsResponse(compilerArguments: args + ["-DFOO"])
148-
await buildServer.setBuildSettings(for: doc, to: newSettings)
84+
try await project.buildServer().setBuildSettings(for: uri, to: newSettings)
14985

15086
try await repeatUntilExpectedResult {
151-
guard let refreshedDiags = try? await testClient.nextDiagnosticsNotification(timeout: .seconds(1)) else {
87+
guard let refreshedDiags = try? await project.testClient.nextDiagnosticsNotification(timeout: .seconds(1)) else {
15288
return false
15389
}
154-
return try text == documentManager.latestSnapshot(doc).text && refreshedDiags.diagnostics.count == 0
90+
return refreshedDiags.diagnostics.count == 0
15591
}
15692
}
15793

15894
func testSwiftDocumentUpdatedBuildSettings() async throws {
159-
let doc = DocumentURI(for: .swift)
160-
let args = fallbackBuildSettings(
161-
for: doc,
162-
language: .swift,
163-
options: SourceKitLSPOptions.FallbackBuildSystemOptions()
164-
)!.compilerArguments
165-
166-
await buildServer.setBuildSettings(for: doc, to: TextDocumentSourceKitOptionsResponse(compilerArguments: args))
167-
168-
let text = """
169-
#if FOO
170-
func foo() {}
171-
#endif
95+
let project = try await CustomBuildServerTestProject(
96+
files: [
97+
"test.swift": """
98+
#if FOO
99+
func foo() {}
100+
#endif
172101
173-
foo()
174-
"""
102+
foo()
103+
"""
104+
],
105+
buildServer: TestBuildServer.self,
106+
usePullDiagnostics: false
107+
)
175108

176-
let documentManager = self.testClient.server.documentManager
109+
let args = try XCTUnwrap(
110+
fallbackBuildSettings(
111+
for: project.uri(for: "test.swift"),
112+
language: .swift,
113+
options: SourceKitLSPOptions.FallbackBuildSystemOptions()
114+
)
115+
).compilerArguments
116+
117+
try await project.buildServer().setBuildSettings(
118+
for: project.uri(for: "test.swift"),
119+
to: TextDocumentSourceKitOptionsResponse(compilerArguments: args)
120+
)
177121

178-
testClient.openDocument(text, uri: doc)
179-
let diags1 = try await testClient.nextDiagnosticsNotification()
122+
let (uri, _) = try project.openDocument("test.swift")
123+
let diags1 = try await project.testClient.nextDiagnosticsNotification()
180124
XCTAssertEqual(diags1.diagnostics.count, 1)
181-
XCTAssertEqual(text, try documentManager.latestSnapshot(doc).text)
182125

183126
// Modify the build settings and inform the delegate.
184127
// This should trigger a new publish diagnostics and we should no longer have errors.
185128
let newSettings = TextDocumentSourceKitOptionsResponse(compilerArguments: args + ["-DFOO"])
186-
await buildServer.setBuildSettings(for: doc, to: newSettings)
129+
try await project.buildServer().setBuildSettings(for: uri, to: newSettings)
187130

188131
// No expected errors here because we fixed the settings.
189-
let diags2 = try await testClient.nextDiagnosticsNotification()
132+
let diags2 = try await project.testClient.nextDiagnosticsNotification()
190133
XCTAssertEqual(diags2.diagnostics.count, 0)
191134
}
192135

193136
func testClangdDocumentFallbackWithholdsDiagnostics() async throws {
194-
let doc = DocumentURI(for: .objective_c)
195-
let args = [doc.pseudoPath, "-DDEBUG"]
196-
let text = """
137+
let project = try await CustomBuildServerTestProject(
138+
files: [
139+
"test.c": """
197140
#ifdef FOO
198141
static void foo() {}
199142
#endif
@@ -202,61 +145,66 @@ final class BuildServerTests: XCTestCase {
202145
foo();
203146
return 0;
204147
}
205-
"""
148+
"""
149+
],
150+
buildServer: TestBuildServer.self,
151+
usePullDiagnostics: false
152+
)
206153

207-
let documentManager = self.testClient.server.documentManager
154+
let args = [try project.uri(for: "test.c").pseudoPath, "-DDEBUG"]
208155

209-
testClient.openDocument(text, uri: doc)
210-
let openDiags = try await testClient.nextDiagnosticsNotification()
156+
let (uri, _) = try project.openDocument("test.c")
157+
let openDiags = try await project.testClient.nextDiagnosticsNotification()
211158
// Expect diagnostics to be withheld.
212159
XCTAssertEqual(openDiags.diagnostics.count, 0)
213-
XCTAssertEqual(text, try documentManager.latestSnapshot(doc).text)
214160

215161
// Modify the build settings and inform the delegate.
216162
// This should trigger a new publish diagnostics and we should see a diagnostic.
217163
let newSettings = TextDocumentSourceKitOptionsResponse(compilerArguments: args)
218-
await buildServer.setBuildSettings(for: doc, to: newSettings)
164+
try await project.buildServer().setBuildSettings(for: uri, to: newSettings)
219165

220-
let refreshedDiags = try await testClient.nextDiagnosticsNotification()
166+
let refreshedDiags = try await project.testClient.nextDiagnosticsNotification()
221167
XCTAssertEqual(refreshedDiags.diagnostics.count, 1)
222-
XCTAssertEqual(text, try documentManager.latestSnapshot(doc).text)
223168
}
224169

225170
func testSwiftDocumentFallbackWithholdsSemanticDiagnostics() async throws {
226-
let doc = DocumentURI(for: .swift)
227-
228-
// Primary settings must be different than the fallback settings.
229-
let fallbackSettings = fallbackBuildSettings(
230-
for: doc,
231-
language: .swift,
232-
options: SourceKitLSPOptions.FallbackBuildSystemOptions()
233-
)!
234-
let primarySettings = TextDocumentSourceKitOptionsResponse(
235-
compilerArguments: fallbackSettings.compilerArguments + ["-DPRIMARY"],
236-
workingDirectory: fallbackSettings.workingDirectory
237-
)
238-
239-
let text = """
171+
let project = try await CustomBuildServerTestProject(
172+
files: [
173+
"test.swift": """
240174
#if FOO
241175
func foo() {}
242176
#endif
243177
244178
foo()
245179
func
246-
"""
180+
"""
181+
],
182+
buildServer: TestBuildServer.self,
183+
usePullDiagnostics: false
184+
)
247185

248-
let documentManager = self.testClient.server.documentManager
186+
// Primary settings must be different than the fallback settings.
187+
let fallbackSettings = try XCTUnwrap(
188+
fallbackBuildSettings(
189+
for: project.uri(for: "test.swift"),
190+
language: .swift,
191+
options: SourceKitLSPOptions.FallbackBuildSystemOptions()
192+
)
193+
)
194+
let primarySettings = TextDocumentSourceKitOptionsResponse(
195+
compilerArguments: fallbackSettings.compilerArguments + ["-DPRIMARY"],
196+
workingDirectory: fallbackSettings.workingDirectory
197+
)
249198

250-
testClient.openDocument(text, uri: doc)
251-
let openDiags = try await testClient.nextDiagnosticsNotification()
199+
let (uri, _) = try project.openDocument("test.swift")
200+
let openDiags = try await project.testClient.nextDiagnosticsNotification()
252201
XCTAssertEqual(openDiags.diagnostics.count, 1)
253-
XCTAssertEqual(text, try documentManager.latestSnapshot(doc).text)
254202

255203
// Swap from fallback settings to primary build server settings.
256-
await buildServer.setBuildSettings(for: doc, to: primarySettings)
204+
try await project.buildServer().setBuildSettings(for: uri, to: primarySettings)
257205

258206
// Two errors since `-DFOO` was not passed.
259-
let refreshedDiags = try await testClient.nextDiagnosticsNotification()
207+
let refreshedDiags = try await project.testClient.nextDiagnosticsNotification()
260208
XCTAssertEqual(refreshedDiags.diagnostics.count, 2)
261209
}
262210
}

0 commit comments

Comments
 (0)