@@ -49,151 +49,94 @@ fileprivate actor TestBuildServer: CustomBuildServer {
49
49
}
50
50
51
51
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
-
119
52
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
134
59
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
+ )
136
69
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
+ )
138
75
139
- testClient . openDocument ( text , uri : doc )
76
+ let ( uri , _ ) = try project . openDocument ( " test.c " )
140
77
141
- let diags = try await testClient. nextDiagnosticsNotification ( )
78
+ let diags = try await project . testClient. nextDiagnosticsNotification ( )
142
79
XCTAssertEqual ( diags. diagnostics. count, 1 )
143
- XCTAssertEqual ( text, try documentManager. latestSnapshot ( doc) . text)
144
80
145
81
// Modify the build settings and inform the delegate.
146
82
// This should trigger a new publish diagnostics and we should no longer have errors.
147
83
let newSettings = TextDocumentSourceKitOptionsResponse ( compilerArguments: args + [ " -DFOO " ] )
148
- await buildServer. setBuildSettings ( for: doc , to: newSettings)
84
+ try await project . buildServer ( ) . setBuildSettings ( for: uri , to: newSettings)
149
85
150
86
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 {
152
88
return false
153
89
}
154
- return try text == documentManager . latestSnapshot ( doc ) . text && refreshedDiags. diagnostics. count == 0
90
+ return refreshedDiags. diagnostics. count == 0
155
91
}
156
92
}
157
93
158
94
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
172
101
173
- foo()
174
- """
102
+ foo()
103
+ """
104
+ ] ,
105
+ buildServer: TestBuildServer . self,
106
+ usePullDiagnostics: false
107
+ )
175
108
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
+ )
177
121
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 ( )
180
124
XCTAssertEqual ( diags1. diagnostics. count, 1 )
181
- XCTAssertEqual ( text, try documentManager. latestSnapshot ( doc) . text)
182
125
183
126
// Modify the build settings and inform the delegate.
184
127
// This should trigger a new publish diagnostics and we should no longer have errors.
185
128
let newSettings = TextDocumentSourceKitOptionsResponse ( compilerArguments: args + [ " -DFOO " ] )
186
- await buildServer. setBuildSettings ( for: doc , to: newSettings)
129
+ try await project . buildServer ( ) . setBuildSettings ( for: uri , to: newSettings)
187
130
188
131
// No expected errors here because we fixed the settings.
189
- let diags2 = try await testClient. nextDiagnosticsNotification ( )
132
+ let diags2 = try await project . testClient. nextDiagnosticsNotification ( )
190
133
XCTAssertEqual ( diags2. diagnostics. count, 0 )
191
134
}
192
135
193
136
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 " : """
197
140
#ifdef FOO
198
141
static void foo() {}
199
142
#endif
@@ -202,61 +145,66 @@ final class BuildServerTests: XCTestCase {
202
145
foo();
203
146
return 0;
204
147
}
205
- """
148
+ """
149
+ ] ,
150
+ buildServer: TestBuildServer . self,
151
+ usePullDiagnostics: false
152
+ )
206
153
207
- let documentManager = self . testClient . server . documentManager
154
+ let args = [ try project . uri ( for : " test.c " ) . pseudoPath , " -DDEBUG " ]
208
155
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 ( )
211
158
// Expect diagnostics to be withheld.
212
159
XCTAssertEqual ( openDiags. diagnostics. count, 0 )
213
- XCTAssertEqual ( text, try documentManager. latestSnapshot ( doc) . text)
214
160
215
161
// Modify the build settings and inform the delegate.
216
162
// This should trigger a new publish diagnostics and we should see a diagnostic.
217
163
let newSettings = TextDocumentSourceKitOptionsResponse ( compilerArguments: args)
218
- await buildServer. setBuildSettings ( for: doc , to: newSettings)
164
+ try await project . buildServer ( ) . setBuildSettings ( for: uri , to: newSettings)
219
165
220
- let refreshedDiags = try await testClient. nextDiagnosticsNotification ( )
166
+ let refreshedDiags = try await project . testClient. nextDiagnosticsNotification ( )
221
167
XCTAssertEqual ( refreshedDiags. diagnostics. count, 1 )
222
- XCTAssertEqual ( text, try documentManager. latestSnapshot ( doc) . text)
223
168
}
224
169
225
170
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 " : """
240
174
#if FOO
241
175
func foo() {}
242
176
#endif
243
177
244
178
foo()
245
179
func
246
- """
180
+ """
181
+ ] ,
182
+ buildServer: TestBuildServer . self,
183
+ usePullDiagnostics: false
184
+ )
247
185
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
+ )
249
198
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 ( )
252
201
XCTAssertEqual ( openDiags. diagnostics. count, 1 )
253
- XCTAssertEqual ( text, try documentManager. latestSnapshot ( doc) . text)
254
202
255
203
// 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)
257
205
258
206
// Two errors since `-DFOO` was not passed.
259
- let refreshedDiags = try await testClient. nextDiagnosticsNotification ( )
207
+ let refreshedDiags = try await project . testClient. nextDiagnosticsNotification ( )
260
208
XCTAssertEqual ( refreshedDiags. diagnostics. count, 2 )
261
209
}
262
210
}
0 commit comments