Skip to content

Commit c65eb9f

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 42f63fa commit c65eb9f

File tree

1 file changed

+75
-11
lines changed

1 file changed

+75
-11
lines changed

module/PowerShellEditorServices/InvokePesterStub.ps1

Lines changed: 75 additions & 11 deletions
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
}

0 commit comments

Comments
 (0)