Skip to content

Commit f118337

Browse files
committed
Add regression tests for paths and RunPesterTestsFromFile command
1 parent 18f9cec commit f118337

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

src/features/Examples.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
import * as fs from "fs";
45
import path = require("path");
56
import vscode = require("vscode");
67

@@ -15,6 +16,10 @@ export class ExamplesFeature implements vscode.Disposable {
1516
"vscode.openFolder",
1617
vscode.Uri.file(this.examplesPath),
1718
true);
19+
20+
// Return existence of the path for testing. The `vscode.openFolder`
21+
// command should do this, but doesn't (yet).
22+
return fs.existsSync(this.examplesPath)
1823
});
1924
}
2025

src/features/PesterTests.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
import * as path from "path";
5+
import * as fs from "fs";
56
import vscode = require("vscode");
67
import { SessionManager } from "../session";
78
import Settings = require("../settings");
@@ -24,42 +25,45 @@ export class PesterTestsFeature implements vscode.Disposable {
2425
this.command = vscode.commands.registerCommand(
2526
"PowerShell.RunPesterTestsFromFile",
2627
(fileUri) => {
27-
this.launchAllTestsInActiveEditor(LaunchType.Run, fileUri);
28+
return this.launchAllTestsInActiveEditor(LaunchType.Run, fileUri);
2829
});
2930
// File context-menu command - Debug Pester Tests
3031
this.command = vscode.commands.registerCommand(
3132
"PowerShell.DebugPesterTestsFromFile",
3233
(fileUri) => {
33-
this.launchAllTestsInActiveEditor(LaunchType.Debug, fileUri);
34+
return this.launchAllTestsInActiveEditor(LaunchType.Debug, fileUri);
3435
});
3536
// This command is provided for usage by PowerShellEditorServices (PSES) only
3637
this.command = vscode.commands.registerCommand(
3738
"PowerShell.RunPesterTests",
3839
(uriString, runInDebugger, describeBlockName?, describeBlockLineNumber?, outputPath?) => {
39-
this.launchTests(uriString, runInDebugger, describeBlockName, describeBlockLineNumber, outputPath);
40+
return this.launchTests(uriString, runInDebugger, describeBlockName, describeBlockLineNumber, outputPath);
4041
});
4142
}
4243

4344
public dispose() {
4445
this.command.dispose();
4546
}
4647

47-
private launchAllTestsInActiveEditor(launchType: LaunchType, fileUri: vscode.Uri) {
48+
private async launchAllTestsInActiveEditor(
49+
launchType: LaunchType,
50+
fileUri: vscode.Uri): Promise<boolean> {
51+
4852
const uriString = (fileUri || vscode.window.activeTextEditor.document.uri).toString();
4953
const launchConfig = this.createLaunchConfig(uriString, launchType);
50-
this.launch(launchConfig);
54+
return this.launch(launchConfig);
5155
}
5256

5357
private async launchTests(
5458
uriString: string,
5559
runInDebugger: boolean,
5660
describeBlockName?: string,
5761
describeBlockLineNumber?: number,
58-
outputPath?: string) {
62+
outputPath?: string): Promise<boolean> {
5963

6064
const launchType = runInDebugger ? LaunchType.Debug : LaunchType.Run;
6165
const launchConfig = this.createLaunchConfig(uriString, launchType, describeBlockName, describeBlockLineNumber, outputPath);
62-
this.launch(launchConfig);
66+
return this.launch(launchConfig);
6367
}
6468

6569
private createLaunchConfig(
@@ -126,9 +130,9 @@ export class PesterTestsFeature implements vscode.Disposable {
126130
return launchConfig;
127131
}
128132

129-
private launch(launchConfig) {
133+
private async launch(launchConfig): Promise<boolean> {
130134
// Create or show the interactive console
131-
// TODO #367: Check if "newSession" mode is configured
135+
// TODO: #367 Check if "newSession" mode is configured
132136
vscode.commands.executeCommand("PowerShell.ShowSessionConsole", true);
133137

134138
// Write out temporary debug session file
@@ -137,6 +141,10 @@ export class PesterTestsFeature implements vscode.Disposable {
137141
this.sessionManager.getSessionDetails());
138142

139143
// TODO: Update to handle multiple root workspaces.
140-
vscode.debug.startDebugging(vscode.workspace.workspaceFolders[0], launchConfig);
144+
//
145+
// Ensure the necessary script exists (for testing). The debugger will
146+
// start regardless, but we also pass its success along.
147+
return fs.existsSync(this.invokePesterStubScriptPath)
148+
&& vscode.debug.startDebugging(vscode.workspace.workspaceFolders[0], launchConfig);
141149
}
142150
}

test/core/paths.test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
import * as assert from "assert";
5+
import * as fs from "fs";
6+
import * as path from "path";
7+
import * as vscode from "vscode";
8+
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}`;
16+
17+
suite("Path assumptions", () => {
18+
before(async () => {
19+
const extension = vscode.extensions.getExtension(extensionId);
20+
if (!extension.isActive) { await extension.activate(); }
21+
});
22+
23+
test("The examples folder can be opened (and exists)", async () => {
24+
assert(await vscode.commands.executeCommand("PowerShell.OpenExamplesFolder"));
25+
});
26+
27+
test("The session folder is created in the right place", async () => {
28+
assert(fs.existsSync(path.resolve(rootPath, "sessions")));
29+
});
30+
31+
test("The logs folder is created in the right place", async () => {
32+
assert(fs.existsSync(path.resolve(rootPath, "logs")));
33+
});
34+
});

test/features/RunCode.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the MIT License.
33

44
import * as assert from "assert";
5+
import * as fs from "fs";
6+
import * as path from "path";
57
import rewire = require("rewire");
68
import vscode = require("vscode");
79

@@ -35,4 +37,11 @@ suite("RunCode tests", () => {
3537

3638
assert.deepEqual(actual, expected);
3739
});
40+
41+
test("Can run Pester tests from file", async () => {
42+
const pesterTests = path.resolve(__dirname, "../../../examples/Tests/SampleModule.Tests.ps1");
43+
assert(fs.existsSync(pesterTests));
44+
await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(pesterTests));
45+
assert(await vscode.commands.executeCommand("PowerShell.RunPesterTestsFromFile"));
46+
})
3847
});

0 commit comments

Comments
 (0)