Skip to content

Commit c558cc1

Browse files
committed
Add CodeLens support for running and debugging Pester tests
This change adds a new PowerShell.RunPesterTests command which can be invoked by CodeLenses which appear on Describe blocks in Pester test files. This allows the user to run or debug all of the tests in a particular Describe block.
1 parent 0590230 commit c558cc1

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/features/PesterTests.ts

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*---------------------------------------------------------
2+
* Copyright (C) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------*/
4+
5+
import vscode = require('vscode');
6+
import utils = require('../utils');
7+
import Window = vscode.window;
8+
import ChildProcess = require('child_process');
9+
import { SessionManager } from '../session';
10+
import { IFeature, LanguageClient } from '../feature';
11+
12+
export class PesterTestsFeature implements IFeature {
13+
14+
private command: vscode.Disposable;
15+
private languageClient: LanguageClient;
16+
17+
constructor(private sessionManager: SessionManager) {
18+
this.command = vscode.commands.registerCommand(
19+
'PowerShell.RunPesterTests',
20+
(uriString, runInDebugger, describeBlockName?) => {
21+
this.launchTests(uriString, runInDebugger, describeBlockName);
22+
});
23+
}
24+
25+
public setLanguageClient(languageClient: LanguageClient) {
26+
this.languageClient = languageClient;
27+
}
28+
29+
public dispose() {
30+
this.command.dispose();
31+
}
32+
33+
private launchTests(uriString, runInDebugger, describeBlockName?) {
34+
var uri = vscode.Uri.parse(uriString);
35+
let currentDocument = vscode.window.activeTextEditor.document;
36+
37+
let launchConfig = {
38+
request: "launch",
39+
type: "PowerShell",
40+
script: "Invoke-Pester",
41+
args: [
42+
`-Script "${uri.fsPath}"`,
43+
describeBlockName
44+
? `-TestName "${describeBlockName}"`
45+
: ""
46+
],
47+
internalConsoleOptions: "neverOpen",
48+
noDebug: !runInDebugger,
49+
cwd:
50+
currentDocument.isUntitled
51+
? vscode.workspace.rootPath
52+
: currentDocument.fileName
53+
}
54+
55+
// Create or show the interactive console
56+
// TODO #367: Check if "newSession" mode is configured
57+
vscode.commands.executeCommand('PowerShell.ShowSessionConsole', true);
58+
59+
// Write out temporary debug session file
60+
utils.writeSessionFile(
61+
utils.getDebugSessionFilePath(),
62+
this.sessionManager.getSessionDetails());
63+
64+
vscode.commands.executeCommand('vscode.startDebug', launchConfig);
65+
}
66+
}

src/main.ts

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { ExpandAliasFeature } from './features/ExpandAlias';
1919
import { ShowHelpFeature } from './features/ShowOnlineHelp';
2020
import { CodeActionsFeature } from './features/CodeActions';
2121
import { RemoteFilesFeature } from './features/RemoteFiles';
22+
import { PesterTestsFeature } from './features/PesterTests';
2223
import { DebugSessionFeature } from './features/DebugSession';
2324
import { PickPSHostProcessFeature } from './features/DebugSession';
2425
import { SpecifyScriptArgsFeature } from './features/DebugSession';
@@ -110,6 +111,7 @@ export function activate(context: vscode.ExtensionContext): void {
110111
new ExpandAliasFeature(),
111112
new ShowHelpFeature(),
112113
new FindModuleFeature(),
114+
new PesterTestsFeature(sessionManager),
113115
new ExtensionCommandsFeature(),
114116
new SelectPSSARulesFeature(),
115117
new CodeActionsFeature(),

0 commit comments

Comments
 (0)