From 4f999011419b5eb16f4e1e5a19c0ab006c7453ac Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Wed, 16 Jan 2019 20:47:45 -0700 Subject: [PATCH 1/2] Interpret null Describe TestName to mean value can't be eval'd --- src/features/PesterTests.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/features/PesterTests.ts b/src/features/PesterTests.ts index 45111e295b..a77cb905f6 100644 --- a/src/features/PesterTests.ts +++ b/src/features/PesterTests.ts @@ -18,12 +18,12 @@ export class PesterTestsFeature implements IFeature { this.command = vscode.commands.registerCommand( "PowerShell.RunPesterTestsFromFile", () => { - this.launchTests(vscode.window.activeTextEditor.document.uri, false); + this.launchTests(vscode.window.activeTextEditor.document.uri, false, undefined); }); this.command = vscode.commands.registerCommand( "PowerShell.DebugPesterTestsFromFile", () => { - this.launchTests(vscode.window.activeTextEditor.document.uri, true); + this.launchTests(vscode.window.activeTextEditor.document.uri, true, undefined); }); // This command is provided for usage by PowerShellEditorServices (PSES) only this.command = vscode.commands.registerCommand( @@ -41,7 +41,21 @@ export class PesterTestsFeature implements IFeature { this.languageClient = languageClient; } - private launchTests(uriString, runInDebugger, describeBlockName?) { + private async launchTests(uriString, runInDebugger, describeBlockName?) { + // PSES passes null for the describeBlockName to signal that it can't evaluate the TestName. + if (describeBlockName === null) { + const answer = await vscode.window.showErrorMessage( + "This Describe block's TestName parameter cannot be evaluated. " + + `Would you like to ${runInDebugger ? "debug" : "run"} all the tests in this file?`, + "Yes", "No"); + + if (answer === "Yes") { + describeBlockName = undefined; + } else { + return; + } + } + const uri = vscode.Uri.parse(uriString); const currentDocument = vscode.window.activeTextEditor.document; const settings = Settings.load(); From 05bcbfb5479a3495c23ce586a6843c1e14ec23ea Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Thu, 17 Jan 2019 21:30:00 -0700 Subject: [PATCH 2/2] Address PR feedback - more changes but not relying on null/undefined --- src/features/PesterTests.ts | 46 +++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/src/features/PesterTests.ts b/src/features/PesterTests.ts index a77cb905f6..75bb17698b 100644 --- a/src/features/PesterTests.ts +++ b/src/features/PesterTests.ts @@ -9,21 +9,28 @@ import { SessionManager } from "../session"; import Settings = require("../settings"); import utils = require("../utils"); +enum LaunchType { + Debug, + Run, +} + export class PesterTestsFeature implements IFeature { private command: vscode.Disposable; private languageClient: LanguageClient; constructor(private sessionManager: SessionManager) { + // File context-menu command - Run Pester Tests this.command = vscode.commands.registerCommand( "PowerShell.RunPesterTestsFromFile", () => { - this.launchTests(vscode.window.activeTextEditor.document.uri, false, undefined); + this.launchAllTestsInActiveEditor(LaunchType.Run); }); + // File context-menu command - Debug Pester Tests this.command = vscode.commands.registerCommand( "PowerShell.DebugPesterTestsFromFile", () => { - this.launchTests(vscode.window.activeTextEditor.document.uri, true, undefined); + this.launchAllTestsInActiveEditor(LaunchType.Debug); }); // This command is provided for usage by PowerShellEditorServices (PSES) only this.command = vscode.commands.registerCommand( @@ -41,21 +48,37 @@ export class PesterTestsFeature implements IFeature { this.languageClient = languageClient; } - private async launchTests(uriString, runInDebugger, describeBlockName?) { + private launchAllTestsInActiveEditor(launchType: LaunchType) { + const uriString = vscode.window.activeTextEditor.document.uri.toString(); + const launchConfig = this.createLaunchConfig(uriString, launchType); + this.launch(launchConfig); + } + + private async launchTests(uriString: string, runInDebugger: boolean, describeBlockName?: string) { // PSES passes null for the describeBlockName to signal that it can't evaluate the TestName. - if (describeBlockName === null) { + if (!describeBlockName) { const answer = await vscode.window.showErrorMessage( "This Describe block's TestName parameter cannot be evaluated. " + `Would you like to ${runInDebugger ? "debug" : "run"} all the tests in this file?`, "Yes", "No"); - if (answer === "Yes") { - describeBlockName = undefined; - } else { + if (answer === "No") { return; } } + const launchType = runInDebugger ? LaunchType.Debug : LaunchType.Run; + const launchConfig = this.createLaunchConfig(uriString, launchType); + + if (describeBlockName) { + launchConfig.args.push("-TestName"); + launchConfig.args.push(`'${describeBlockName}'`); + } + + this.launch(launchConfig); + } + + private createLaunchConfig(uriString: string, launchType: LaunchType) { const uri = vscode.Uri.parse(uriString); const currentDocument = vscode.window.activeTextEditor.document; const settings = Settings.load(); @@ -76,7 +99,7 @@ export class PesterTestsFeature implements IFeature { "@{IncludeVSCodeMarker=$true}", ], internalConsoleOptions: "neverOpen", - noDebug: !runInDebugger, + noDebug: (launchType === LaunchType.Run), createTemporaryIntegratedConsole: settings.debugging.createTemporaryIntegratedConsole, cwd: currentDocument.isUntitled @@ -84,11 +107,10 @@ export class PesterTestsFeature implements IFeature { : path.dirname(currentDocument.fileName), }; - if (describeBlockName) { - launchConfig.args.push("-TestName"); - launchConfig.args.push(`'${describeBlockName}'`); - } + return launchConfig; + } + private launch(launchConfig) { // Create or show the interactive console // TODO #367: Check if "newSession" mode is configured vscode.commands.executeCommand("PowerShell.ShowSessionConsole", true);