Skip to content

Execute interpolated Pester describe blocks if PSES signals that it is possible #1713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/features/PesterTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ 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?, pesterSupportsLineNumber?) => {
this.launchTests(uriString, runInDebugger, describeBlockName,
describeBlockLineNumber, pesterSupportsLineNumber);
});
}

Expand All @@ -54,11 +55,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, pesterSupportsLineNumber = false) {
// PSES passes null for the describeBlockName to signal that it can't evaluate the TestName.
if (!describeBlockName) {
if (!describeBlockName && !pesterSupportsLineNumber) {
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");

Expand All @@ -68,17 +70,19 @@ 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, pesterSupportsLineNumber);

if (describeBlockName) {
if (describeBlockName && !pesterSupportsLineNumber) {
launchConfig.args.push("-TestName");
launchConfig.args.push(`'${describeBlockName}'`);
}

this.launch(launchConfig);
}

private createLaunchConfig(uriString: string, launchType: LaunchType) {
private createLaunchConfig(uriString: string, launchType: LaunchType,
describeBlockLineNumber?: number, pesterSupportsLineNumber = false) {
const uri = vscode.Uri.parse(uriString);
const currentDocument = vscode.window.activeTextEditor.document;
const settings = Settings.load();
Expand All @@ -87,6 +91,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 (pesterSupportsLineNumber) {
pesterOption = "(New-PesterOption -ScriptBlockFilter " +
`@( @{ IncludeVSCodeMarker=$true; Line=${describeBlockLineNumber}; Path='${scriptPath}' } ) )`;
} else {
pesterOption = "@{IncludeVSCodeMarker=$true}";
}

const launchConfig = {
request: "launch",
type: "PowerShell",
Expand All @@ -96,7 +108,7 @@ export class PesterTestsFeature implements IFeature {
"-Script",
`'${scriptPath}'`,
"-PesterOption",
"@{IncludeVSCodeMarker=$true}",
pesterOption,
],
internalConsoleOptions: "neverOpen",
noDebug: (launchType === LaunchType.Run),
Expand Down