Skip to content

Commit 5739a2a

Browse files
committed
Fix build completion monitoring for testing
1 parent 212245e commit 5739a2a

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

language-server/src/test/test-client.ts

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export class TestClient<Configuration> {
4444
private openDocuments: Set<string>;
4545
private workspaceFolder: Promise<string>;
4646
private watchEnabled: boolean;
47+
private buildCompleted;
4748

4849
onRequest: Connection["onRequest"];
4950
sendRequest: Connection["sendRequest"];
@@ -97,8 +98,21 @@ export class TestClient<Configuration> {
9798
}
9899
});
99100

100-
this.client.onRequest(WorkDoneProgressCreateRequest.type, () => {
101-
// Nothing to do
101+
this.buildCompleted = createPromise();
102+
let builds = 0;
103+
104+
this.client.onRequest(WorkDoneProgressCreateRequest.type, ({ token }) => {
105+
this.client.onProgress(WorkDoneProgress.type, token, ({ kind }) => {
106+
if (kind === "begin") {
107+
builds++;
108+
} else if (kind === "end") {
109+
builds--;
110+
if (builds === 0) {
111+
this.buildCompleted.resolve();
112+
this.buildCompleted = createPromise();
113+
}
114+
}
115+
});
102116
});
103117

104118
this.client.onRequest(ConfigurationRequest.type, (params) => {
@@ -221,8 +235,6 @@ export class TestClient<Configuration> {
221235
async changeConfiguration(settings: Partial<Configuration>) {
222236
this._settings = settings;
223237

224-
const buildCompleted = this.buildCompleted();
225-
226238
if (this.configurationChangeNotificationOptions === null) {
227239
await this.client.sendNotification(DidChangeConfigurationNotification.type, {
228240
settings: null
@@ -235,7 +247,7 @@ export class TestClient<Configuration> {
235247
});
236248
}
237249

238-
await buildCompleted;
250+
await this.buildCompleted.promise;
239251
}
240252

241253
async writeDocument(uri: string, text: string) {
@@ -244,8 +256,6 @@ export class TestClient<Configuration> {
244256

245257
await writeFile(fileURLToPath(fullUri), text, "utf-8");
246258

247-
const buildCompleted = this.buildCompleted();
248-
249259
if (this.watchEnabled) {
250260
await this.client.sendNotification(DidChangeWatchedFilesNotification.type, {
251261
changes: [{
@@ -255,7 +265,7 @@ export class TestClient<Configuration> {
255265
});
256266
}
257267

258-
await buildCompleted;
268+
await this.buildCompleted.promise;
259269

260270
return fullUri;
261271
}
@@ -265,8 +275,6 @@ export class TestClient<Configuration> {
265275

266276
await rm(fileURLToPath(fullUri));
267277

268-
const buildCompleted = this.buildCompleted();
269-
270278
if (this.watchEnabled) {
271279
await this.client.sendNotification(DidChangeWatchedFilesNotification.type, {
272280
changes: [{
@@ -276,7 +284,7 @@ export class TestClient<Configuration> {
276284
});
277285
}
278286

279-
await buildCompleted;
287+
await this.buildCompleted.promise;
280288

281289
return fullUri;
282290
}
@@ -307,19 +315,6 @@ export class TestClient<Configuration> {
307315
}
308316
});
309317
}
310-
311-
// TODO: Duplicated code
312-
private buildCompleted() {
313-
return new Promise<void>((resolve) => {
314-
this.client.onRequest(WorkDoneProgressCreateRequest.type, ({ token }) => {
315-
this.client.onProgress(WorkDoneProgress.type, token, ({ kind }) => {
316-
if (kind === "end") {
317-
resolve();
318-
}
319-
});
320-
});
321-
});
322-
}
323318
}
324319

325320
export class TestStream extends Duplex {
@@ -331,3 +326,15 @@ export class TestStream extends Duplex {
331326
_read() {
332327
}
333328
}
329+
330+
const createPromise = () => {
331+
let resolve: () => void;
332+
let reject: (reason?: any) => void; // eslint-disable-line @typescript-eslint/no-explicit-any
333+
const promise = new Promise<void>((resolveFn, rejectFn) => {
334+
resolve = resolveFn;
335+
reject = rejectFn;
336+
});
337+
338+
// @ts-expect-error TypeScript doesn't think resolve and reject have been assigned
339+
return { promise, resolve, reject };
340+
};

0 commit comments

Comments
 (0)