Skip to content

Commit 9299f0b

Browse files
Fixed crash when running pester older than 3.4.5 (#2888)
* Fixed crash when running pester older than 3.4.5 Calling Invoke-Pester with the -show param was breaking tests if running a version of pester older than 3.4.5 Moved logic for building spalt to call invoke-pester into a function and only added the show parameter if running pester 3.4.5 or newer * Update InvokePesterStub.ps1 Co-authored-by: Tyler James Leonhardt <[email protected]> * Update InvokePesterStub.ps1 Co-authored-by: Tyler James Leonhardt <[email protected]> Co-authored-by: Tyler James Leonhardt <[email protected]>
1 parent bee2c2d commit 9299f0b

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

InvokePesterStub.ps1

+27-14
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,30 @@ $pester4Output = switch ($Output) {
8787
}
8888

8989
if ($MinimumVersion5 -and $pesterModule.Version -lt "5.0.0") {
90-
Write-Warning "Pester 5.0.0 or newer is required because setting PowerShell > Pester: Use Legacy Code Lens is disabled, but Pester $($pesterModule.Version) is loaded. Some of the code lense features might not work as expected."
90+
Write-Warning "Pester 5.0.0 or newer is required because setting PowerShell > Pester: Use Legacy Code Lens is disabled, but Pester $($pesterModule.Version) is loaded. Some of the code lens features might not work as expected."
9191
}
9292

9393

94+
function Get-InvokePesterParams {
95+
$invokePesterParams = @{
96+
Script = $ScriptPath
97+
}
98+
99+
if ($pesterModule.Version -ge '3.4.0') {
100+
# -PesterOption was introduced before 3.4.0, and VSCodeMarker in 4.0.3-rc,
101+
# but because no-one checks the integrity of this hashtable we can call
102+
# all of the versions down to 3.4.0 like this
103+
$invokePesterParams.Add("PesterOption", @{ IncludeVSCodeMarker = $true })
104+
}
105+
106+
if ($pesterModule.Version -ge '3.4.5') {
107+
# -Show was introduced in 3.4.5
108+
$invokePesterParams.Add("Show", $pester4Output)
109+
}
110+
111+
return $invokePesterParams
112+
}
113+
94114
if ($All) {
95115
if ($pesterModule.Version -ge '5.0.0') {
96116
$configuration = @{
@@ -116,18 +136,9 @@ if ($All) {
116136
}
117137
Pester\Invoke-Pester -Configuration $configuration | Out-Null
118138
}
119-
elseif ($pesterModule.Version -ge '3.4.5') {
120-
# -Show was introduced in 3.4.5
121-
Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} -Show $pester4Output
122-
}
123-
elseif ($pesterModule.Version -ge '3.4.0') {
124-
# -PesterOption was introduced before 3.4.0, and VSCodeMarker in 4.0.3-rc,
125-
# but because no-one checks the integrity of this hashtable we can call all of the versions
126-
# down to 3.4.0 like this
127-
Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true}
128-
}
129139
else {
130-
Pester\Invoke-Pester -Script $ScriptPath
140+
$invokePesterParams = Get-InvokePesterParams
141+
Pester\Invoke-Pester @invokePesterParams
131142
}
132143
}
133144
elseif (($LineNumber -match '\d+') -and ($pesterModule.Version -ge '4.6.0')) {
@@ -164,7 +175,8 @@ elseif ($TestName) {
164175
throw "Running tests by test name is unsafe. This should not trigger for Pester 5."
165176
}
166177
else {
167-
Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} -TestName $TestName -Show $pester4Output
178+
$invokePesterParams = Get-InvokePesterParams
179+
Pester\Invoke-Pester @invokePesterParams
168180
}
169181
}
170182
else {
@@ -177,5 +189,6 @@ else {
177189
Write-Warning "The Describe block's TestName cannot be evaluated. EXECUTING ALL TESTS instead."
178190
Write-Warning "To avoid this, install Pester >= 4.6.0 or remove any expressions in the TestName."
179191

180-
Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} -Show $pester4Output
192+
$invokePesterParams = Get-InvokePesterParams
193+
Pester\Invoke-Pester @invokePesterParams
181194
}

0 commit comments

Comments
 (0)