Skip to content

Commit 9051b99

Browse files
Run single Pester test (#2441)
* Run single test * Add options for code lens and verbosity * Rename Pester options to enableLegacyCodeLens and outputVerbosity * change to use Co-authored-by: Tyler Leonhardt <[email protected]>
1 parent ea82375 commit 9051b99

File tree

4 files changed

+109
-11
lines changed

4 files changed

+109
-11
lines changed

InvokePesterStub.ps1

+75-11
Original file line numberDiff line numberDiff line change
@@ -47,36 +47,100 @@ param(
4747
# If specified, executes all the tests in the specified test script.
4848
[Parameter()]
4949
[switch]
50-
$All
50+
$All,
51+
52+
[Parameter()]
53+
[switch] $MinimumVersion5,
54+
55+
[Parameter(Mandatory)]
56+
[string] $Output
5157
)
5258

5359
$pesterModule = Microsoft.PowerShell.Core\Get-Module Pester
60+
# add one line, so the subsequent output is not shifted to the side
61+
Write-Output ''
5462
if (!$pesterModule) {
5563
Write-Output "Importing Pester module..."
56-
$pesterModule = Microsoft.PowerShell.Core\Import-Module Pester -ErrorAction Ignore -PassThru
64+
$minimumVersion = if ($MinimumVersion5) { "5.0.0" } else { "0.0.0" }
65+
$versionMessage = " version $minimumVersion"
66+
$pesterModule = Microsoft.PowerShell.Core\Import-Module Pester -ErrorAction Ignore -PassThru -MinimumVersion $minimumVersion
5767
if (!$pesterModule) {
5868
# If we still don't have an imported Pester module, that is (most likely) because Pester is not installed.
59-
Write-Warning "Failed to import the Pester module. You must install Pester to run or debug Pester tests."
60-
Write-Warning "You can install Pester by executing: Install-Module Pester -Scope CurrentUser -Force"
69+
Write-Warning "Failed to import Pester$(if ($MinimumVersion5){ $versionMessage }). You must install Pester module to run or debug Pester tests."
70+
Write-Warning "You can install Pester by executing: Install-Module Pester $(if ($MinimumVersion5) {"-MinimumVersion 5.0.0" }) -Scope CurrentUser -Force"
6171
return
6272
}
6373
}
6474

65-
if ($All) {
66-
Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true}
75+
$pester4Output = switch ($Output) {
76+
"None" { "None" }
77+
"Minimal" { "Fails" }
78+
default { "All" }
6779
}
68-
elseif ($TestName) {
69-
Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} -TestName $TestName
80+
81+
if ($MinimumVersion5 -and $pesterModule.Version -lt "5.0.0") {
82+
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."
83+
}
84+
85+
86+
if ($All) {
87+
if ($pesterModule.Version -ge '5.0.0') {
88+
$configuration = @{
89+
Run = @{
90+
Path = $ScriptPath
91+
}
92+
}
93+
# only override this if user asks us to do it, to allow Pester to pick up
94+
# $PesterPreference from caller context and merge it with the configuration
95+
# we provide below, this way user can specify his output (and other) settings
96+
# using the standard [PesterConfiguration] object, and we can avoid providing
97+
# settings for everything
98+
if ("FromPreference" -ne $Output) {
99+
$configuration.Add('Output', @{ Verbosity = $Output })
100+
}
101+
Pester\Invoke-Pester -Configuration $configuration | Out-Null
102+
}
103+
else {
104+
Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} -Show $pester4Output
105+
}
70106
}
71107
elseif (($LineNumber -match '\d+') -and ($pesterModule.Version -ge '4.6.0')) {
72-
Pester\Invoke-Pester -Script $ScriptPath -PesterOption (New-PesterOption -ScriptBlockFilter @{
73-
IncludeVSCodeMarker=$true; Line=$LineNumber; Path=$ScriptPath})
108+
if ($pesterModule.Version -ge '5.0.0') {
109+
$configuration = @{
110+
Run = @{
111+
Path = $ScriptPath
112+
}
113+
Filter = @{
114+
Line = "${ScriptPath}:$LineNumber"
115+
}
116+
}
117+
if ("FromPreference" -ne $Output) {
118+
$configuration.Add('Output', @{ Verbosity = $Output })
119+
}
120+
Pester\Invoke-Pester -Configuration $configuration | Out-Null
121+
}
122+
else {
123+
Pester\Invoke-Pester -Script $ScriptPath -PesterOption (New-PesterOption -ScriptBlockFilter @{
124+
IncludeVSCodeMarker=$true; Line=$LineNumber; Path=$ScriptPath}) -Show $pester4Output
125+
}
126+
}
127+
elseif ($TestName) {
128+
if ($pesterModule.Version -ge '5.0.0') {
129+
throw "Running tests by test name is unsafe. This should not trigger for Pester 5."
130+
}
131+
else {
132+
Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} -TestName $TestName -Show $pester4Output
133+
}
74134
}
75135
else {
136+
if ($pesterModule.Version -ge '5.0.0') {
137+
throw "Running tests by expandable string is unsafe. This should not trigger for Pester 5."
138+
}
139+
76140
# We get here when the TestName expression is of type ExpandableStringExpressionAst.
77141
# PSES will not attempt to "evaluate" the expression so it returns null for the TestName.
78142
Write-Warning "The Describe block's TestName cannot be evaluated. EXECUTING ALL TESTS instead."
79143
Write-Warning "To avoid this, install Pester >= 4.6.0 or remove any expressions in the TestName."
80144

81-
Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true}
145+
Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} -Show $pester4Output
82146
}

package.json

+15
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,21 @@
760760
"type": "array",
761761
"default": null,
762762
"description": "An array of strings that enable experimental features in the PowerShell extension."
763+
},
764+
"powershell.pester.enableLegacyCodeLens": {
765+
"type": "boolean",
766+
"default": true,
767+
"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."
768+
},
769+
"powershell.pester.outputVerbosity": {
770+
"type": "string",
771+
"enum": [
772+
"FromPreference",
773+
"Normal",
774+
"Minimal"
775+
],
776+
"default": "FromPreference",
777+
"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."
763778
}
764779
}
765780
},

src/features/PesterTests.ts

+6
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ export class PesterTestsFeature implements IFeature {
109109
launchConfig.args.push("-TestName", `'${testName}'`);
110110
}
111111

112+
if (!settings.pester.useLegacyCodeLens) {
113+
launchConfig.args.push("-MinimumVersion5");
114+
}
115+
116+
launchConfig.args.push("-Output", `'${settings.pester.outputVerbosity}'`);
117+
112118
return launchConfig;
113119
}
114120

src/settings.ts

+13
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export interface ISettings {
9494
integratedConsole?: IIntegratedConsoleSettings;
9595
bugReporting?: IBugReportingSettings;
9696
sideBar?: ISideBarSettings;
97+
pester?: IPesterSettings;
9798
}
9899

99100
export interface IStartAsLoginShellSettings {
@@ -113,6 +114,11 @@ export interface ISideBarSettings {
113114
CommandExplorerVisibility?: boolean;
114115
}
115116

117+
export interface IPesterSettings {
118+
useLegacyCodeLens?: boolean;
119+
outputVerbosity?: string;
120+
}
121+
116122
export function load(): ISettings {
117123
const configuration: vscode.WorkspaceConfiguration =
118124
vscode.workspace.getConfiguration(
@@ -177,6 +183,11 @@ export function load(): ISettings {
177183
CommandExplorerVisibility: true,
178184
};
179185

186+
const defaultPesterSettings: IPesterSettings = {
187+
useLegacyCodeLens: true,
188+
outputVerbosity: "FromPreference",
189+
};
190+
180191
return {
181192
startAutomatically:
182193
configuration.get<boolean>("startAutomatically", true),
@@ -212,6 +223,8 @@ export function load(): ISettings {
212223
configuration.get<IBugReportingSettings>("bugReporting", defaultBugReportingSettings),
213224
sideBar:
214225
configuration.get<ISideBarSettings>("sideBar", defaultSideBarSettings),
226+
pester:
227+
configuration.get<IPesterSettings>("pester", defaultPesterSettings),
215228
startAsLoginShell:
216229
// tslint:disable-next-line
217230
// 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

0 commit comments

Comments
 (0)