@@ -49,151 +49,94 @@ fileprivate actor TestBuildServer: CustomBuildServer {
4949}
5050
5151final 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