Skip to content

Commit 5abe5b6

Browse files
committed
Fix build completion monitoring for testing
1 parent 212245e commit 5abe5b6

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

language-server/src/services/schemas-neovim.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe("Feature - workspace (neovim)", () => {
2424

2525
documentUriA = await client.writeDocument("./subjectA.schema.json", `{ "$schema": "https://json-schema.org/draft/2020-12/schema" }`);
2626
documentUriB = await client.writeDocument("./subjectB.schema.json", `{ "$schema": "https://json-schema.org/draft/2020-12/schema" }`);
27+
await client.ready();
2728
});
2829

2930
afterAll(async () => {
@@ -42,11 +43,13 @@ describe("Feature - workspace (neovim)", () => {
4243
test("a change to a watched file should validate the workspace", async () => {
4344
const schemaUris: string[] = [];
4445

46+
await client.ready();
4547
client.onNotification(PublishDiagnosticsNotification.type, (params) => {
4648
schemaUris.push(params.uri);
4749
});
4850

4951
await client.writeDocument("./subjectB.schema.json", `{ "$schema": "https://json-schema.org/draft/2020-12/schema" }`);
52+
await client.ready();
5053

5154
expect(schemaUris).to.eql([documentUriA, documentUriB]);
5255
});

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

Lines changed: 33 additions & 22 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
}
@@ -308,17 +316,8 @@ export class TestClient<Configuration> {
308316
});
309317
}
310318

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-
});
319+
async ready() {
320+
await this.buildCompleted.promise;
322321
}
323322
}
324323

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

0 commit comments

Comments
 (0)