Skip to content

Commit

Permalink
Fix build completion monitoring for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jdesrosiers committed Oct 19, 2024
1 parent 212245e commit 5abe5b6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
3 changes: 3 additions & 0 deletions language-server/src/services/schemas-neovim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe("Feature - workspace (neovim)", () => {

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

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

await client.ready();
client.onNotification(PublishDiagnosticsNotification.type, (params) => {
schemaUris.push(params.uri);
});

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

expect(schemaUris).to.eql([documentUriA, documentUriB]);
});
Expand Down
55 changes: 33 additions & 22 deletions language-server/src/test/test-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class TestClient<Configuration> {
private openDocuments: Set<string>;
private workspaceFolder: Promise<string>;
private watchEnabled: boolean;
private buildCompleted;

onRequest: Connection["onRequest"];
sendRequest: Connection["sendRequest"];
Expand Down Expand Up @@ -97,8 +98,21 @@ export class TestClient<Configuration> {
}
});

this.client.onRequest(WorkDoneProgressCreateRequest.type, () => {
// Nothing to do
this.buildCompleted = createPromise();
let builds = 0;

this.client.onRequest(WorkDoneProgressCreateRequest.type, ({ token }) => {
this.client.onProgress(WorkDoneProgress.type, token, ({ kind }) => {
if (kind === "begin") {
builds++;
} else if (kind === "end") {
builds--;
if (builds === 0) {
this.buildCompleted.resolve();
this.buildCompleted = createPromise();
}
}
});
});

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

const buildCompleted = this.buildCompleted();

if (this.configurationChangeNotificationOptions === null) {
await this.client.sendNotification(DidChangeConfigurationNotification.type, {
settings: null
Expand All @@ -235,7 +247,7 @@ export class TestClient<Configuration> {
});
}

await buildCompleted;
await this.buildCompleted.promise;
}

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

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

const buildCompleted = this.buildCompleted();

if (this.watchEnabled) {
await this.client.sendNotification(DidChangeWatchedFilesNotification.type, {
changes: [{
Expand All @@ -255,7 +265,7 @@ export class TestClient<Configuration> {
});
}

await buildCompleted;
await this.buildCompleted.promise;

return fullUri;
}
Expand All @@ -265,8 +275,6 @@ export class TestClient<Configuration> {

await rm(fileURLToPath(fullUri));

const buildCompleted = this.buildCompleted();

if (this.watchEnabled) {
await this.client.sendNotification(DidChangeWatchedFilesNotification.type, {
changes: [{
Expand All @@ -276,7 +284,7 @@ export class TestClient<Configuration> {
});
}

await buildCompleted;
await this.buildCompleted.promise;

return fullUri;
}
Expand Down Expand Up @@ -308,17 +316,8 @@ export class TestClient<Configuration> {
});
}

// TODO: Duplicated code
private buildCompleted() {
return new Promise<void>((resolve) => {
this.client.onRequest(WorkDoneProgressCreateRequest.type, ({ token }) => {
this.client.onProgress(WorkDoneProgress.type, token, ({ kind }) => {
if (kind === "end") {
resolve();
}
});
});
});
async ready() {
await this.buildCompleted.promise;
}
}

Expand All @@ -331,3 +330,15 @@ export class TestStream extends Duplex {
_read() {
}
}

const createPromise = () => {
let resolve: () => void;
let reject: (reason?: any) => void; // eslint-disable-line @typescript-eslint/no-explicit-any
const promise = new Promise<void>((resolveFn, rejectFn) => {
resolve = resolveFn;
reject = rejectFn;
});

// @ts-expect-error TypeScript doesn't think resolve and reject have been assigned
return { promise, resolve, reject };
};

0 comments on commit 5abe5b6

Please sign in to comment.