From b81c5ffc92670f16c3c6e4e801062f3980c76290 Mon Sep 17 00:00:00 2001 From: nohwnd Date: Mon, 27 Jan 2020 20:01:22 +0100 Subject: [PATCH 1/4] Run single test --- InvokePesterStub.ps1 | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/InvokePesterStub.ps1 b/InvokePesterStub.ps1 index 8b6f204cce..75ea6d4c06 100755 --- a/InvokePesterStub.ps1 +++ b/InvokePesterStub.ps1 @@ -63,16 +63,35 @@ if (!$pesterModule) { } if ($All) { - Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} -} -elseif ($TestName) { - Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} -TestName $TestName + if ($pesterModule.Version -ge '5.0.0') { + Pester\Invoke-Pester -Configuration @{ Run = $ScriptPath } | Out-Null + } + else { + Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} + } } elseif (($LineNumber -match '\d+') -and ($pesterModule.Version -ge '4.6.0')) { - Pester\Invoke-Pester -Script $ScriptPath -PesterOption (New-PesterOption -ScriptBlockFilter @{ - IncludeVSCodeMarker=$true; Line=$LineNumber; Path=$ScriptPath}) + if ($pesterModule.Version -ge '5.0.0') { + Pester\Invoke-Pester -Configuration @{ Run = $ScriptPath; Filter = @{ Line = "${ScriptPath}:$LineNumber"} } | Out-Null + } + else { + Pester\Invoke-Pester -Script $ScriptPath -PesterOption (New-PesterOption -ScriptBlockFilter @{ + IncludeVSCodeMarker=$true; Line=$LineNumber; Path=$ScriptPath}) + } +} +elseif ($TestName) { + if ($pesterModule.Version -ge '5.0.0') { + throw "Running tests by test name is unsafe. This should not trigger for Pester 5." + } + else { + Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} -TestName $TestName + } } else { + if ($pesterModule.Version -ge '5.0.0') { + throw "Running tests by expandable string is unsafe. This should not trigger for Pester 5." + } + # We get here when the TestName expression is of type ExpandableStringExpressionAst. # PSES will not attempt to "evaluate" the expression so it returns null for the TestName. Write-Warning "The Describe block's TestName cannot be evaluated. EXECUTING ALL TESTS instead." From 87558fe11b239137c6430e35d90d04a0e3c6b7af Mon Sep 17 00:00:00 2001 From: nohwnd Date: Sat, 8 Feb 2020 15:28:20 +0100 Subject: [PATCH 2/4] Add options for code lens and verbosity --- InvokePesterStub.ps1 | 65 +++++++++++++++++++++++++++++++------ package.json | 15 +++++++++ src/features/PesterTests.ts | 6 ++++ src/settings.ts | 13 ++++++++ 4 files changed, 89 insertions(+), 10 deletions(-) diff --git a/InvokePesterStub.ps1 b/InvokePesterStub.ps1 index 75ea6d4c06..7d472fece7 100755 --- a/InvokePesterStub.ps1 +++ b/InvokePesterStub.ps1 @@ -47,36 +47,81 @@ param( # If specified, executes all the tests in the specified test script. [Parameter()] [switch] - $All + $All, + + [Parameter()] + [switch] $MinimumVersion5, + + [Parameter(Mandatory)] + [string] $Output ) $pesterModule = Microsoft.PowerShell.Core\Get-Module Pester +# add one line, so the subsequent output is not shifted to the side +Write-Output '' if (!$pesterModule) { Write-Output "Importing Pester module..." - $pesterModule = Microsoft.PowerShell.Core\Import-Module Pester -ErrorAction Ignore -PassThru + $minimumVersion = if ($MinimumVersion5) { "5.0.0" } else { "0.0.0" } + $versionMessage = " version $minimumVersion" + $pesterModule = Microsoft.PowerShell.Core\Import-Module Pester -ErrorAction Ignore -PassThru -MinimumVersion $minimumVersion if (!$pesterModule) { # If we still don't have an imported Pester module, that is (most likely) because Pester is not installed. - Write-Warning "Failed to import the Pester module. You must install Pester to run or debug Pester tests." - Write-Warning "You can install Pester by executing: Install-Module Pester -Scope CurrentUser -Force" + Write-Warning "Failed to import Pester$(if ($MinimumVersion5){ $versionMessage }). You must install Pester module to run or debug Pester tests." + Write-Warning "You can install Pester by executing: Install-Module Pester $(if ($MinimumVersion5) {"-MinimumVersion 5.0.0" }) -Scope CurrentUser -Force" return } } +$pester4Output = switch ($Output) { + "None" { "None" } + "Minimal" { "Fails" } + default { "All" } +} + +if ($MinimumVersion5 -and $pesterModule.Version -lt "5.0.0") { + Write-Warning "Pester 5.0.0 or newer is required because setting PowerShell > Pester > Pester5 Code Lens is enabled, but Pester $($pesterModule.Version) is loaded. Some of the code lense features might not work as expected." +} + + if ($All) { if ($pesterModule.Version -ge '5.0.0') { - Pester\Invoke-Pester -Configuration @{ Run = $ScriptPath } | Out-Null + $configuration = @{ + Run = @{ + Path = $ScriptPath + } + } + # only override this if user asks us to do it, to allow Pester to pick up + # $PesterPreference from caller context and merge it with the configuration + # we provide below, this way user can specify his output (and other) settings + # using the standard [PesterConfiguration] object, and we can avoid providing + # settings for everything + if ("FromPreference" -ne $Output) { + $configuration.Add('Output', @{ Verbosity = $Output }) + } + Pester\Invoke-Pester -Configuration $configuration | Out-Null } else { - Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} + Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} -Show $pester4Output } } elseif (($LineNumber -match '\d+') -and ($pesterModule.Version -ge '4.6.0')) { if ($pesterModule.Version -ge '5.0.0') { - Pester\Invoke-Pester -Configuration @{ Run = $ScriptPath; Filter = @{ Line = "${ScriptPath}:$LineNumber"} } | Out-Null + $configuration = @{ + Run = @{ + Path = $ScriptPath + } + Filter = @{ + Line = "${ScriptPath}:$LineNumber" + } + } + if ("FromPreference" -ne $Output) { + $configuration.Add('Output', @{ Verbosity = $Output }) + } + Pester\Invoke-Pester -Configuration $configuration | Out-Null } else { Pester\Invoke-Pester -Script $ScriptPath -PesterOption (New-PesterOption -ScriptBlockFilter @{ - IncludeVSCodeMarker=$true; Line=$LineNumber; Path=$ScriptPath}) + IncludeVSCodeMarker=$true; Line=$LineNumber; Path=$ScriptPath}) -Show $pester4Output } } elseif ($TestName) { @@ -84,7 +129,7 @@ elseif ($TestName) { throw "Running tests by test name is unsafe. This should not trigger for Pester 5." } else { - Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} -TestName $TestName + Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} -TestName $TestName -Show $pester4Output } } else { @@ -97,5 +142,5 @@ else { Write-Warning "The Describe block's TestName cannot be evaluated. EXECUTING ALL TESTS instead." Write-Warning "To avoid this, install Pester >= 4.6.0 or remove any expressions in the TestName." - Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} + Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} -Show $pester4Output } diff --git a/package.json b/package.json index ad2840184b..b14950c02f 100644 --- a/package.json +++ b/package.json @@ -750,6 +750,21 @@ "type": "array", "default": null, "description": "An array of strings that enable experimental features in the PowerShell extension." + }, + "powershell.pester.pester5CodeLens": { + "type": "boolean", + "default": false, + "description": "Enables code lense that is specific to Pester 5. This enables Run Tests on every Describe / Context and It and other features." + }, + "powershell.pester.output": { + "type": "string", + "enum": [ + "FromPreference", + "Normal", + "Minimal" + ], + "default": "FromPreference", + "description": "Defines the verbosity of output to be used. For Pester5 the default value FromPreference, will use the Output settings from the $PesterPreference defined in the caller context, or use Normal if there is none. For Pester4 the Normal and FromPreference options map to All, and Minimal option maps to Fails." } } }, diff --git a/src/features/PesterTests.ts b/src/features/PesterTests.ts index ea2c5fea9f..de6bda54d6 100644 --- a/src/features/PesterTests.ts +++ b/src/features/PesterTests.ts @@ -109,6 +109,12 @@ export class PesterTestsFeature implements IFeature { launchConfig.args.push("-TestName", `'${testName}'`); } + if (settings.pester.pester5CodeLens) { + launchConfig.args.push("-MinimumVersion5"); + } + + launchConfig.args.push("-Output", `'${settings.pester.output}'`); + return launchConfig; } diff --git a/src/settings.ts b/src/settings.ts index a8e931aaf8..9148084995 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -94,6 +94,7 @@ export interface ISettings { integratedConsole?: IIntegratedConsoleSettings; bugReporting?: IBugReportingSettings; sideBar?: ISideBarSettings; + pester?: IPesterSettings; } export interface IStartAsLoginShellSettings { @@ -113,6 +114,11 @@ export interface ISideBarSettings { CommandExplorerVisibility?: boolean; } +export interface IPesterSettings { + pester5CodeLens?: boolean; + output?: string; +} + export function load(): ISettings { const configuration: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration( @@ -177,6 +183,11 @@ export function load(): ISettings { CommandExplorerVisibility: true, }; + const defaultPesterSettings: IPesterSettings = { + pester5CodeLens: false, + output: "FromPreference", + }; + return { startAutomatically: configuration.get("startAutomatically", true), @@ -212,6 +223,8 @@ export function load(): ISettings { configuration.get("bugReporting", defaultBugReportingSettings), sideBar: configuration.get("sideBar", defaultSideBarSettings), + pester: + configuration.get("pester", defaultPesterSettings), startAsLoginShell: // tslint:disable-next-line // We follow the same convention as VS Code - https://github.com/microsoft/vscode/blob/ff00badd955d6cfcb8eab5f25f3edc86b762f49f/src/vs/workbench/contrib/terminal/browser/terminal.contribution.ts#L105-L107 From a0c7370e48278e5cc1f5f14c93644ba77d187b20 Mon Sep 17 00:00:00 2001 From: nohwnd Date: Wed, 11 Mar 2020 17:19:15 +0100 Subject: [PATCH 3/4] Rename Pester options to enableLegacyCodeLens and outputVerbosity --- InvokePesterStub.ps1 | 2 +- package.json | 10 +++++----- src/features/PesterTests.ts | 4 ++-- src/settings.ts | 8 ++++---- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/InvokePesterStub.ps1 b/InvokePesterStub.ps1 index 7d472fece7..d9aa31a132 100755 --- a/InvokePesterStub.ps1 +++ b/InvokePesterStub.ps1 @@ -79,7 +79,7 @@ $pester4Output = switch ($Output) { } if ($MinimumVersion5 -and $pesterModule.Version -lt "5.0.0") { - Write-Warning "Pester 5.0.0 or newer is required because setting PowerShell > Pester > Pester5 Code Lens is enabled, but Pester $($pesterModule.Version) is loaded. Some of the code lense features might not work as expected." + Write-Warning "Pester 5.0.0 or newer is required because setting PowerShell > Pester: Enable Legacy Code Lens is disabled, but Pester $($pesterModule.Version) is loaded. Some of the code lense features might not work as expected." } diff --git a/package.json b/package.json index b14950c02f..e7d2e90bad 100644 --- a/package.json +++ b/package.json @@ -751,12 +751,12 @@ "default": null, "description": "An array of strings that enable experimental features in the PowerShell extension." }, - "powershell.pester.pester5CodeLens": { + "powershell.pester.enableLegacyCodeLens": { "type": "boolean", - "default": false, - "description": "Enables code lense that is specific to Pester 5. This enables Run Tests on every Describe / Context and It and other features." + "default": true, + "description": "Enable code lense that is compatible with Pester 4. Disabling this will show 'Run Tests' on all It, Describe and Context blocks, and will correctly work only with Pester 5 and newer." }, - "powershell.pester.output": { + "powershell.pester.outputVerbosity": { "type": "string", "enum": [ "FromPreference", @@ -764,7 +764,7 @@ "Minimal" ], "default": "FromPreference", - "description": "Defines the verbosity of output to be used. For Pester5 the default value FromPreference, will use the Output settings from the $PesterPreference defined in the caller context, or use Normal if there is none. For Pester4 the Normal and FromPreference options map to All, and Minimal option maps to Fails." + "description": "Defines the verbosity of output to be used. For Pester 5 and newer the default value FromPreference, will use the Output settings from the $PesterPreference defined in the caller context, and will default to Normal if there is none. For Pester 4 the FromPreference and Normal options map to All, and Minimal option maps to Fails." } } }, diff --git a/src/features/PesterTests.ts b/src/features/PesterTests.ts index de6bda54d6..b98aad4486 100644 --- a/src/features/PesterTests.ts +++ b/src/features/PesterTests.ts @@ -109,11 +109,11 @@ export class PesterTestsFeature implements IFeature { launchConfig.args.push("-TestName", `'${testName}'`); } - if (settings.pester.pester5CodeLens) { + if (!settings.pester.enableLegacyCodeLens) { launchConfig.args.push("-MinimumVersion5"); } - launchConfig.args.push("-Output", `'${settings.pester.output}'`); + launchConfig.args.push("-Output", `'${settings.pester.outputVerbosity}'`); return launchConfig; } diff --git a/src/settings.ts b/src/settings.ts index 9148084995..15bd8f23c2 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -115,8 +115,8 @@ export interface ISideBarSettings { } export interface IPesterSettings { - pester5CodeLens?: boolean; - output?: string; + enableLegacyCodeLens?: boolean; + outputVerbosity?: string; } export function load(): ISettings { @@ -184,8 +184,8 @@ export function load(): ISettings { }; const defaultPesterSettings: IPesterSettings = { - pester5CodeLens: false, - output: "FromPreference", + enableLegacyCodeLens: true, + outputVerbosity: "FromPreference", }; return { From ad9403e7b928fbf7ca282d20a1cf227b4afce3e8 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Thu, 12 Mar 2020 11:40:48 -0700 Subject: [PATCH 4/4] change to use --- src/features/PesterTests.ts | 2 +- src/settings.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/features/PesterTests.ts b/src/features/PesterTests.ts index b98aad4486..87872a13ca 100644 --- a/src/features/PesterTests.ts +++ b/src/features/PesterTests.ts @@ -109,7 +109,7 @@ export class PesterTestsFeature implements IFeature { launchConfig.args.push("-TestName", `'${testName}'`); } - if (!settings.pester.enableLegacyCodeLens) { + if (!settings.pester.useLegacyCodeLens) { launchConfig.args.push("-MinimumVersion5"); } diff --git a/src/settings.ts b/src/settings.ts index 15bd8f23c2..34ce5c356c 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -115,7 +115,7 @@ export interface ISideBarSettings { } export interface IPesterSettings { - enableLegacyCodeLens?: boolean; + useLegacyCodeLens?: boolean; outputVerbosity?: string; } @@ -184,7 +184,7 @@ export function load(): ISettings { }; const defaultPesterSettings: IPesterSettings = { - enableLegacyCodeLens: true, + useLegacyCodeLens: true, outputVerbosity: "FromPreference", };