Skip to content

Commit a22bf90

Browse files
authored
Merge pull request #1781 from ahoppen/no-timer-target-change
Don’t rely on timers for BSP target / settings change tests
2 parents 820b454 + 64024f6 commit a22bf90

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

Sources/SKTestSupport/INPUTS/AbstractBuildServer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ def handle_message(self, message: Dict[str, object]) -> Optional[Dict[str, objec
7676
return self.register_for_changes(params)
7777
elif method == "textDocument/sourceKitOptions":
7878
return self.textdocument_sourcekitoptions(params)
79+
elif method == "workspace/didChangeWatchedFiles":
80+
return self.workspace_did_change_watched_files(params)
7981
elif method == "workspace/buildTargets":
8082
return self.workspace_build_targets(params)
8183

@@ -147,6 +149,9 @@ def buildtarget_sources(self, request: Dict[str, object]) -> Dict[str, object]:
147149
code=-32601, message=f"'buildTarget/sources' not implemented"
148150
)
149151

152+
def workspace_did_change_watched_files(self, notification: Dict[str, object]) -> None:
153+
pass
154+
150155
def workspace_build_targets(self, request: Dict[str, object]) -> Dict[str, object]:
151156
raise RequestError(
152157
code=-32601, message=f"'workspace/buildTargets' not implemented"

Tests/BuildSystemIntegrationTests/BuildServerBuildSystemTests.swift

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,11 @@ final class BuildServerBuildSystemTests: XCTestCase {
8989
"""
9090
],
9191
buildServer: """
92-
import threading
93-
9492
class BuildServer(AbstractBuildServer):
95-
timer_has_fired: bool = False
96-
97-
def timer_fired(self):
98-
self.timer_has_fired = True
99-
self.send_notification("buildTarget/didChange", {})
93+
has_changed_targets: bool = False
10094
10195
def workspace_build_targets(self, request: Dict[str, object]) -> Dict[str, object]:
102-
if self.timer_has_fired:
96+
if self.has_changed_targets:
10397
return {
10498
"targets": [
10599
{
@@ -112,11 +106,10 @@ final class BuildServerBuildSystemTests: XCTestCase {
112106
]
113107
}
114108
else:
115-
threading.Timer(1, self.timer_fired).start()
116109
return {"targets": []}
117110
118111
def buildtarget_sources(self, request: Dict[str, object]) -> Dict[str, object]:
119-
assert self.timer_has_fired
112+
assert self.has_changed_targets
120113
return {
121114
"items": [
122115
{
@@ -129,10 +122,14 @@ final class BuildServerBuildSystemTests: XCTestCase {
129122
}
130123
131124
def textdocument_sourcekitoptions(self, request: Dict[str, object]) -> Dict[str, object]:
132-
assert self.timer_has_fired
125+
assert self.has_changed_targets
133126
return {
134127
"compilerArguments": [r"$TEST_DIR/Test.swift", "-DDEBUG", $SDK_ARGS]
135128
}
129+
130+
def workspace_did_change_watched_files(self, notification: Dict[str, object]) -> None:
131+
self.has_changed_targets = True
132+
self.send_notification("buildTarget/didChange", {})
136133
"""
137134
)
138135

@@ -144,6 +141,13 @@ final class BuildServerBuildSystemTests: XCTestCase {
144141
)
145142
XCTAssertEqual(initialDiagnostics.fullReport?.items, [])
146143

144+
// We use an arbitrary file change to signal to the BSP server that it should send the targets changed notification
145+
project.testClient.send(
146+
DidChangeWatchedFilesNotification(changes: [
147+
FileEvent(uri: try DocumentURI(string: "file:///dummy"), type: .created)
148+
])
149+
)
150+
147151
// But then the 1s timer in the build server should fire, we get a `buildTarget/didChange` notification and we have
148152
// build settings for Test.swift
149153
try await repeatUntilExpectedResult {
@@ -168,14 +172,8 @@ final class BuildServerBuildSystemTests: XCTestCase {
168172
"""
169173
],
170174
buildServer: """
171-
import threading
172-
173175
class BuildServer(AbstractBuildServer):
174-
timer_has_fired: bool = False
175-
176-
def timer_fired(self):
177-
self.timer_has_fired = True
178-
self.send_notification("buildTarget/didChange", {})
176+
has_changed_settings: bool = False
179177
180178
def workspace_build_targets(self, request: Dict[str, object]) -> Dict[str, object]:
181179
return {
@@ -203,15 +201,18 @@ final class BuildServerBuildSystemTests: XCTestCase {
203201
}
204202
205203
def textdocument_sourcekitoptions(self, request: Dict[str, object]) -> Dict[str, object]:
206-
if self.timer_has_fired:
204+
if self.has_changed_settings:
207205
return {
208206
"compilerArguments": [r"$TEST_DIR/Test.swift", "-DDEBUG", $SDK_ARGS]
209207
}
210208
else:
211-
threading.Timer(1, self.timer_fired).start()
212209
return {
213210
"compilerArguments": [r"$TEST_DIR/Test.swift", $SDK_ARGS]
214211
}
212+
213+
def workspace_did_change_watched_files(self, notification: Dict[str, object]) -> None:
214+
self.has_changed_settings = True
215+
self.send_notification("buildTarget/didChange", {})
215216
"""
216217
)
217218

@@ -223,6 +224,13 @@ final class BuildServerBuildSystemTests: XCTestCase {
223224
)
224225
XCTAssertEqual(initialDiagnostics.fullReport?.items.map(\.message), ["DEBUG NOT SET"])
225226

227+
// We use an arbitrary file change to signal to the BSP server that it should send the targets changed notification
228+
project.testClient.send(
229+
DidChangeWatchedFilesNotification(changes: [
230+
FileEvent(uri: try DocumentURI(string: "file:///dummy"), type: .created)
231+
])
232+
)
233+
226234
// But then the 1s timer in the build server should fire, we get a `buildTarget/didChange` notification and we get
227235
// build settings for Test.swift that include -DDEBUG
228236
try await repeatUntilExpectedResult {

0 commit comments

Comments
 (0)