Skip to content

Commit b67bc9c

Browse files
committed
Create and use test/utils.ensureExtensionIsActivated()
Cleans up some repetitive code and makes tests more stable.
1 parent 125c4c3 commit b67bc9c

File tree

5 files changed

+41
-29
lines changed

5 files changed

+41
-29
lines changed

test/core/paths.test.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,20 @@ import * as fs from "fs";
66
import * as path from "path";
77
import * as vscode from "vscode";
88
import { before } from "mocha";
9-
10-
// This lets us test the rest of our path assumptions against the baseline of
11-
// this test file existing at `<root>/out/test/core/paths.test.ts`.
12-
const rootPath = path.resolve(__dirname, "../../../")
13-
// tslint:disable-next-line: no-var-requires
14-
const packageJSON: any = require(path.resolve(rootPath, "package.json"));
15-
const extensionId = `${packageJSON.publisher}.${packageJSON.name}`;
9+
import utils = require("../utils");
1610

1711
suite("Path assumptions", () => {
18-
before(async () => {
19-
const extension = vscode.extensions.getExtension(extensionId);
20-
if (!extension.isActive) { await extension.activate(); }
21-
});
12+
before(utils.ensureExtensionIsActivated);
2213

2314
test("The examples folder can be opened (and exists)", async () => {
2415
assert(await vscode.commands.executeCommand("PowerShell.OpenExamplesFolder"));
2516
});
2617

2718
test("The session folder is created in the right place", async () => {
28-
assert(fs.existsSync(path.resolve(rootPath, "sessions")));
19+
assert(fs.existsSync(path.resolve(utils.rootPath, "sessions")));
2920
});
3021

3122
test("The logs folder is created in the right place", async () => {
32-
assert(fs.existsSync(path.resolve(rootPath, "logs")));
23+
assert(fs.existsSync(path.resolve(utils.rootPath, "logs")));
3324
});
3425
});

test/features/ExternalApi.test.ts

+9-16
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,18 @@
44
import * as assert from "assert";
55
import * as vscode from "vscode";
66
import { before, beforeEach, afterEach } from "mocha";
7+
import utils = require("../utils");
78
import { IExternalPowerShellDetails, IPowerShellExtensionClient } from "../../src/features/ExternalApi";
89

9-
// tslint:disable-next-line: no-var-requires
10-
const PackageJSON: any = require("../../../package.json");
11-
const testExtensionId = `${PackageJSON.publisher}.${PackageJSON.name}`;
12-
1310
suite("ExternalApi feature - Registration API", () => {
1411
let powerShellExtensionClient: IPowerShellExtensionClient;
1512
before(async () => {
16-
const powershellExtension = vscode.extensions.getExtension<IPowerShellExtensionClient>(testExtensionId);
17-
if (!powershellExtension.isActive) {
18-
powerShellExtensionClient = await powershellExtension.activate();
19-
return;
20-
}
13+
const powershellExtension = await utils.ensureExtensionIsActivated();
2114
powerShellExtensionClient = powershellExtension!.exports as IPowerShellExtensionClient;
2215
});
2316

2417
test("It can register and unregister an extension", () => {
25-
const sessionId: string = powerShellExtensionClient.registerExternalExtension(testExtensionId);
18+
const sessionId: string = powerShellExtensionClient.registerExternalExtension(utils.extensionId);
2619
assert.notStrictEqual(sessionId , "");
2720
assert.notStrictEqual(sessionId , null);
2821
assert.strictEqual(
@@ -31,7 +24,7 @@ suite("ExternalApi feature - Registration API", () => {
3124
});
3225

3326
test("It can register and unregister an extension with a version", () => {
34-
const sessionId: string = powerShellExtensionClient.registerExternalExtension(testExtensionId, "v2");
27+
const sessionId: string = powerShellExtensionClient.registerExternalExtension(utils.extensionId, "v2");
3528
assert.notStrictEqual(sessionId , "");
3629
assert.notStrictEqual(sessionId , null);
3730
assert.strictEqual(
@@ -48,12 +41,12 @@ suite("ExternalApi feature - Registration API", () => {
4841
});
4942

5043
test("It can't register the same extension twice", async () => {
51-
const sessionId: string = powerShellExtensionClient.registerExternalExtension(testExtensionId);
44+
const sessionId: string = powerShellExtensionClient.registerExternalExtension(utils.extensionId);
5245
try {
5346
assert.throws(
54-
() => powerShellExtensionClient.registerExternalExtension(testExtensionId),
47+
() => powerShellExtensionClient.registerExternalExtension(utils.extensionId),
5548
{
56-
message: `The extension '${testExtensionId}' is already registered.`
49+
message: `The extension '${utils.extensionId}' is already registered.`
5750
});
5851
} finally {
5952
powerShellExtensionClient.unregisterExternalExtension(sessionId);
@@ -74,7 +67,7 @@ suite("ExternalApi feature - Other APIs", () => {
7467
let powerShellExtensionClient: IPowerShellExtensionClient;
7568

7669
before(async () => {
77-
const powershellExtension = vscode.extensions.getExtension<IPowerShellExtensionClient>(testExtensionId);
70+
const powershellExtension = vscode.extensions.getExtension<IPowerShellExtensionClient>(utils.extensionId);
7871
if (!powershellExtension.isActive) {
7972
powerShellExtensionClient = await powershellExtension.activate();
8073
return;
@@ -83,7 +76,7 @@ suite("ExternalApi feature - Other APIs", () => {
8376
});
8477

8578
beforeEach(() => {
86-
sessionId = powerShellExtensionClient.registerExternalExtension(testExtensionId);
79+
sessionId = powerShellExtensionClient.registerExternalExtension(utils.extensionId);
8780
});
8881

8982
afterEach(() => {

test/features/ISECompatibility.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33

44
import * as assert from "assert";
55
import * as vscode from "vscode";
6+
import { before } from "mocha";
67
import { ISECompatibilityFeature } from "../../src/features/ISECompatibility";
8+
import utils = require("../utils");
79

810
suite("ISECompatibility feature", () => {
11+
before(utils.ensureExtensionIsActivated);
12+
913
test("It sets ISE Settings", async () => {
1014
await vscode.commands.executeCommand("PowerShell.EnableISEMode");
1115
for (const iseSetting of ISECompatibilityFeature.settings) {
1216
const currently = vscode.workspace.getConfiguration(iseSetting.path).get(iseSetting.name);
1317
assert.equal(currently, iseSetting.value);
1418
}
1519
});
20+
1621
test("It unsets ISE Settings", async () => {
1722
// Change state to something that DisableISEMode will change
1823
await vscode.workspace.getConfiguration("workbench").update("colorTheme", "PowerShell ISE", true);
@@ -24,6 +29,7 @@ suite("ISECompatibility feature", () => {
2429
assert.notEqual(currently, iseSetting.value);
2530
}
2631
}).timeout(10000);
32+
2733
test("It leaves Theme after being changed after enabling ISE Mode", async () => {
2834
await vscode.commands.executeCommand("PowerShell.EnableISEMode");
2935
assert.equal(vscode.workspace.getConfiguration("workbench").get("colorTheme"), "PowerShell ISE");

test/features/RunCode.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as fs from "fs";
66
import * as path from "path";
77
import rewire = require("rewire");
88
import vscode = require("vscode");
9+
import utils = require("../utils");
910

1011
// Setup function that is not exported.
1112
const customViews = rewire("../../src/features/RunCode");
@@ -41,6 +42,7 @@ suite("RunCode tests", () => {
4142
test("Can run Pester tests from file", async () => {
4243
const pesterTests = path.resolve(__dirname, "../../../examples/Tests/SampleModule.Tests.ps1");
4344
assert(fs.existsSync(pesterTests));
45+
await utils.ensureExtensionIsActivated();
4446
await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(pesterTests));
4547
assert(await vscode.commands.executeCommand("PowerShell.RunPesterTestsFromFile"));
4648
}).timeout(20000);

test/utils.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
"use strict";
5+
6+
import * as path from "path";
7+
import * as vscode from "vscode";
8+
9+
// This lets us test the rest of our path assumptions against the baseline of
10+
// this test file existing at `<root>/out/test/utils.js`.
11+
export const rootPath = path.resolve(__dirname, "../../")
12+
// tslint:disable-next-line: no-var-requires
13+
const packageJSON: any = require(path.resolve(rootPath, "package.json"));
14+
export const extensionId = `${packageJSON.publisher}.${packageJSON.name}`;
15+
16+
export async function ensureExtensionIsActivated(): Promise<vscode.Extension<any>> {
17+
const extension = vscode.extensions.getExtension(extensionId);
18+
if (!extension.isActive) { await extension.activate(); }
19+
return extension;
20+
}

0 commit comments

Comments
 (0)