Skip to content

Improve test stability with ensureEditorServicesIsConnected #3643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions src/features/ExternalApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface IPowerShellExtensionClient {
registerExternalExtension(id: string, apiVersion?: string): string;
unregisterExternalExtension(uuid: string): boolean;
getPowerShellVersionDetails(uuid: string): Promise<IExternalPowerShellDetails>;
waitUntilStarted(uuid: string): Promise<void>;
}

/*
Expand Down Expand Up @@ -99,6 +100,16 @@ export class ExternalApiFeature extends LanguageClientConsumer implements IPower
return true;
}

private getRegisteredExtension(uuid: string = ""): IExternalExtension {
if (!ExternalApiFeature.registeredExternalExtension.has(uuid)) {
throw new Error(
"UUID provided was invalid, make sure you ran the 'powershellExtensionClient.registerExternalExtension(extensionId)' method and pass in the UUID that it returns to subsequent methods.");
}

// TODO: When we have more than one API version, make sure to include a check here.
return ExternalApiFeature.registeredExternalExtension.get(uuid);
}

/*
DESCRIPTION:
This will fetch the version details of the PowerShell used to start
Expand All @@ -118,13 +129,7 @@ export class ExternalApiFeature extends LanguageClientConsumer implements IPower
}
*/
public async getPowerShellVersionDetails(uuid: string = ""): Promise<IExternalPowerShellDetails> {
if (!ExternalApiFeature.registeredExternalExtension.has(uuid)) {
throw new Error(
"UUID provided was invalid, make sure you ran the 'powershellExtensionClient.registerExternalExtension(extensionId)' method and pass in the UUID that it returns to subsequent methods.");
}

// TODO: When we have more than one API version, make sure to include a check here.
const extension = ExternalApiFeature.registeredExternalExtension.get(uuid);
const extension = this.getRegisteredExtension(uuid);
this.log.writeDiagnostic(`Extension '${extension.id}' called 'getPowerShellVersionDetails'`);

await this.sessionManager.waitUntilStarted();
Expand All @@ -137,6 +142,25 @@ export class ExternalApiFeature extends LanguageClientConsumer implements IPower
architecture: versionDetails.architecture
};
}
/*
DESCRIPTION:
This will wait until the extension's PowerShell session is started.

USAGE:
powerShellExtensionClient.waitUntilStarted(
"uuid"); // the uuid from above for tracking purposes

RETURNS:
A void promise that resolves only once the extension is started.

If the extension is not started by some mechanism
then this will wait indefinitely.
*/
public async waitUntilStarted(uuid: string = ""): Promise<void> {
const extension = this.getRegisteredExtension(uuid);
this.log.writeDiagnostic(`Extension '${extension.id}' called 'waitUntilStarted'`);
return this.sessionManager.waitUntilStarted();
}

public dispose() {
// Nothing to dispose.
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export function activate(context: vscode.ExtensionContext): IPowerShellExtension
registerExternalExtension: (id: string, apiVersion: string = 'v1') => externalApi.registerExternalExtension(id, apiVersion),
unregisterExternalExtension: uuid => externalApi.unregisterExternalExtension(uuid),
getPowerShellVersionDetails: uuid => externalApi.getPowerShellVersionDetails(uuid),
waitUntilStarted: uuid => externalApi.waitUntilStarted(uuid),
};
}

Expand Down
2 changes: 1 addition & 1 deletion test/core/paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as vscode from "vscode";
import utils = require("../utils");

describe("Path assumptions", function () {
before(utils.ensureExtensionIsActivated);
before(utils.ensureEditorServicesIsConnected);

// TODO: This is skipped because it intereferes with other tests. Either
// need to find a way to close the opened folder via a Code API, or find
Expand Down
2 changes: 1 addition & 1 deletion test/features/ISECompatibility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("ISE compatibility feature", function () {
before(async function () {
// Save user's current theme.
currentTheme = await vscode.workspace.getConfiguration("workbench").get("colorTheme");
await utils.ensureExtensionIsActivated();
await utils.ensureEditorServicesIsConnected();
});

after(async function () {
Expand Down
4 changes: 1 addition & 3 deletions test/features/RunCode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum LaunchType {
}

describe("RunCode feature", function () {
before(utils.ensureExtensionIsActivated);
before(utils.ensureEditorServicesIsConnected);

it("Creates the launch config", function () {
const commandToRun: string = "Invoke-Build";
Expand Down Expand Up @@ -49,8 +49,6 @@ describe("RunCode feature", function () {
// Open the PowerShell file with Pester tests and then wait a while for
// the extension to finish connecting to the server.
await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(pesterTests));
// TODO: Find a non-sleep way to wait for the connection to establish.
await sleep(15000);

// Now run the Pester tests, check the debugger started, wait a bit for
// it to run, and then kill it for safety's sake.
Expand Down
9 changes: 9 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import * as path from "path";
import * as vscode from "vscode";
import { IPowerShellExtensionClient } from "../src/features/ExternalApi";

// This lets us test the rest of our path assumptions against the baseline of
// this test file existing at `<root>/out/test/utils.js`.
Expand All @@ -18,3 +19,11 @@ export async function ensureExtensionIsActivated(): Promise<vscode.Extension<any
if (!extension.isActive) { await extension.activate(); }
return extension;
}

export async function ensureEditorServicesIsConnected(): Promise<void> {
const powershellExtension = await ensureExtensionIsActivated();
const client = powershellExtension!.exports as IPowerShellExtensionClient;
const sessionId = client.registerExternalExtension(extensionId);
await client.waitUntilStarted(sessionId);
client.unregisterExternalExtension(sessionId);
}