Skip to content

Commit 77f4535

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

File tree

5 files changed

+43
-34
lines changed

5 files changed

+43
-34
lines changed

test/core/paths.test.ts

Lines changed: 4 additions & 13 deletions
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(async () => { await 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

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,19 @@
22
// Licensed under the MIT License.
33

44
import * as assert from "assert";
5-
import * as vscode from "vscode";
65
import { before, beforeEach, afterEach } from "mocha";
6+
import utils = require("../utils");
77
import { IExternalPowerShellDetails, IPowerShellExtensionClient } from "../../src/features/ExternalApi";
88

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

2416
test("It can register and unregister an extension", () => {
25-
const sessionId: string = powerShellExtensionClient.registerExternalExtension(testExtensionId);
17+
const sessionId: string = powerShellExtensionClient.registerExternalExtension(utils.extensionId);
2618
assert.notStrictEqual(sessionId , "");
2719
assert.notStrictEqual(sessionId , null);
2820
assert.strictEqual(
@@ -31,7 +23,7 @@ suite("ExternalApi feature - Registration API", () => {
3123
});
3224

3325
test("It can register and unregister an extension with a version", () => {
34-
const sessionId: string = powerShellExtensionClient.registerExternalExtension(testExtensionId, "v2");
26+
const sessionId: string = powerShellExtensionClient.registerExternalExtension(utils.extensionId, "v2");
3527
assert.notStrictEqual(sessionId , "");
3628
assert.notStrictEqual(sessionId , null);
3729
assert.strictEqual(
@@ -48,12 +40,12 @@ suite("ExternalApi feature - Registration API", () => {
4840
});
4941

5042
test("It can't register the same extension twice", async () => {
51-
const sessionId: string = powerShellExtensionClient.registerExternalExtension(testExtensionId);
43+
const sessionId: string = powerShellExtensionClient.registerExternalExtension(utils.extensionId);
5244
try {
5345
assert.throws(
54-
() => powerShellExtensionClient.registerExternalExtension(testExtensionId),
46+
() => powerShellExtensionClient.registerExternalExtension(utils.extensionId),
5547
{
56-
message: `The extension '${testExtensionId}' is already registered.`
48+
message: `The extension '${utils.extensionId}' is already registered.`
5749
});
5850
} finally {
5951
powerShellExtensionClient.unregisterExternalExtension(sessionId);
@@ -74,16 +66,12 @@ suite("ExternalApi feature - Other APIs", () => {
7466
let powerShellExtensionClient: IPowerShellExtensionClient;
7567

7668
before(async () => {
77-
const powershellExtension = vscode.extensions.getExtension<IPowerShellExtensionClient>(testExtensionId);
78-
if (!powershellExtension.isActive) {
79-
powerShellExtensionClient = await powershellExtension.activate();
80-
return;
81-
}
69+
const powershellExtension = await utils.ensureExtensionIsActivated();
8270
powerShellExtensionClient = powershellExtension!.exports as IPowerShellExtensionClient;
8371
});
8472

8573
beforeEach(() => {
86-
sessionId = powerShellExtensionClient.registerExternalExtension(testExtensionId);
74+
sessionId = powerShellExtensionClient.registerExternalExtension(utils.extensionId);
8775
});
8876

8977
afterEach(() => {

test/features/ISECompatibility.test.ts

Lines changed: 6 additions & 0 deletions
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(async () => { await 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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import * as assert from "assert";
55
import * as fs from "fs";
66
import * as path from "path";
7+
import { before } from "mocha";
78
import rewire = require("rewire");
89
import vscode = require("vscode");
10+
import utils = require("../utils");
911

1012
// Setup function that is not exported.
1113
const customViews = rewire("../../src/features/RunCode");
@@ -17,6 +19,8 @@ enum LaunchType {
1719
}
1820

1921
suite("RunCode tests", () => {
22+
before(async () => { await utils.ensureExtensionIsActivated(); } );
23+
2024
test("Can create the launch config", () => {
2125
const commandToRun: string = "Invoke-Build";
2226
const args: string[] = ["Clean"];

test/utils.ts

Lines changed: 20 additions & 0 deletions
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)