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 all 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
27 changes: 18 additions & 9 deletions src/features/PesterTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand All @@ -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. " +
Copy link
Member

@TylerLeonhardt TylerLeonhardt Jan 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like this is a little more actionable?

Suggested change
"This Describe block's TestName parameter cannot be evaluated in versions of Pester before 4.6.0. " +
"This Describe block's TestName could not be parsed. Either try again with Pester 4.6.0 or higher, or remove any inline parameters in your TestName." +

Thoughts @rkeithhill?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, something like this but it might ne even better if the ending is more like or remove any variable expressions in the Describe block name

`Would you like to ${runInDebugger ? "debug" : "run"} all the tests in this file?`,
"Yes", "No");

Expand All @@ -68,17 +69,17 @@ 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}'`);
}

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();
Expand All @@ -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",
Expand All @@ -96,7 +105,7 @@ export class PesterTestsFeature implements IFeature {
"-Script",
`'${scriptPath}'`,
"-PesterOption",
"@{IncludeVSCodeMarker=$true}",
pesterOption,
],
internalConsoleOptions: "neverOpen",
noDebug: (launchType === LaunchType.Run),
Expand Down