diff --git a/src/features/PesterTests.ts b/src/features/PesterTests.ts index 75bb17698b..8856d492c7 100644 --- a/src/features/PesterTests.ts +++ b/src/features/PesterTests.ts @@ -35,8 +35,8 @@ export class PesterTestsFeature implements IFeature { // This command is provided for usage by PowerShellEditorServices (PSES) only this.command = vscode.commands.registerCommand( "PowerShell.RunPesterTests", - (uriString, runInDebugger, describeBlockName?) => { - this.launchTests(uriString, runInDebugger, describeBlockName); + (uriString, runInDebugger, describeBlockName?, describeBlockLineNumber?) => { + this.launchTests(uriString, runInDebugger, describeBlockName, describeBlockLineNumber); }); } @@ -54,11 +54,12 @@ export class PesterTestsFeature implements IFeature { this.launch(launchConfig); } - private async launchTests(uriString: string, runInDebugger: boolean, describeBlockName?: string) { + private async launchTests(uriString: string, runInDebugger: boolean, describeBlockName?: string, + describeBlockLineNumber?: number) { // PSES passes null for the describeBlockName to signal that it can't evaluate the TestName. - if (!describeBlockName) { + if (!describeBlockName && !describeBlockLineNumber) { const answer = await vscode.window.showErrorMessage( - "This Describe block's TestName parameter cannot be evaluated. " + + "This Describe block's TestName parameter cannot be evaluated in versions of Pester before 4.6.0. " + `Would you like to ${runInDebugger ? "debug" : "run"} all the tests in this file?`, "Yes", "No"); @@ -68,9 +69,9 @@ export class PesterTestsFeature implements IFeature { } const launchType = runInDebugger ? LaunchType.Debug : LaunchType.Run; - const launchConfig = this.createLaunchConfig(uriString, launchType); + const launchConfig = this.createLaunchConfig(uriString, launchType, describeBlockLineNumber); - if (describeBlockName) { + if (describeBlockName && !describeBlockLineNumber) { launchConfig.args.push("-TestName"); launchConfig.args.push(`'${describeBlockName}'`); } @@ -78,7 +79,7 @@ export class PesterTestsFeature implements IFeature { this.launch(launchConfig); } - private createLaunchConfig(uriString: string, launchType: LaunchType) { + private createLaunchConfig(uriString: string, launchType: LaunchType, describeBlockLineNumber?: number) { const uri = vscode.Uri.parse(uriString); const currentDocument = vscode.window.activeTextEditor.document; const settings = Settings.load(); @@ -87,6 +88,14 @@ export class PesterTestsFeature implements IFeature { // special chars like & $ @ () [], we do have to double up the interior single quotes. const scriptPath = uri.fsPath.replace(/'/g, "''"); + let pesterOption: string; + if (describeBlockLineNumber) { + pesterOption = "(New-PesterOption -ScriptBlockFilter " + + `@( @{ IncludeVSCodeMarker=$true; Line=${describeBlockLineNumber}; Path='${scriptPath}' } ) )`; + } else { + pesterOption = "@{IncludeVSCodeMarker=$true}"; + } + const launchConfig = { request: "launch", type: "PowerShell", @@ -96,7 +105,7 @@ export class PesterTestsFeature implements IFeature { "-Script", `'${scriptPath}'`, "-PesterOption", - "@{IncludeVSCodeMarker=$true}", + pesterOption, ], internalConsoleOptions: "neverOpen", noDebug: (launchType === LaunchType.Run),