forked from PowerShell/vscode-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvokePesterStub.ps1
executable file
·194 lines (170 loc) · 6.56 KB
/
InvokePesterStub.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Stub around Invoke-Pester command used by VSCode PowerShell extension.
.DESCRIPTION
The stub checks the version of Pester and if >= 4.6.0, invokes Pester
using the LineNumber parameter (if specified). Otherwise, it invokes
using the TestName parameter (if specified). If the All parameter
is specified, then all the tests are invoked in the specifed file.
Finally, if none of these three parameters are specified, all tests
are invoked and a warning is issued indicating what the user can do
to allow invocation of individual Describe blocks.
.EXAMPLE
PS C:\> .\InvokePesterStub.ps1 ~\project\test\foo.tests.ps1 -LineNumber 14
Invokes a specific test by line number in the specified file.
.EXAMPLE
PS C:\> .\InvokePesterStub.ps1 ~\project\test\foo.tests.ps1 -TestName 'Foo Tests'
Invokes a specific test by test name in the specified file.
.EXAMPLE
PS C:\> .\InvokePesterStub.ps1 ~\project\test\foo.tests.ps1 -All
Invokes all tests in the specified file.
.INPUTS
None
.OUTPUTS
None
#>
param(
# Specifies the path to the test script.
[Parameter(Position=0, Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$ScriptPath,
# Specifies the name of the test taken from the Describe block's name.
[Parameter()]
[string]
$TestName,
# Specifies the starting line number of the DescribeBlock. This feature requires
# Pester 4.6.0 or higher.
[Parameter()]
[ValidatePattern('\d*')]
[string]
$LineNumber,
# If specified, executes all the tests in the specified test script.
[Parameter()]
[switch]
$All,
[Parameter()]
[switch] $MinimumVersion5,
[Parameter(Mandatory)]
[string] $Output,
[Parameter()]
[string] $OutputPath
)
$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..."
if ($MinimumVersion5) {
$pesterModule = Microsoft.PowerShell.Core\Import-Module Pester -ErrorAction Ignore -PassThru -MinimumVersion 5.0.0
}
if (!$pesterModule) {
$pesterModule = Microsoft.PowerShell.Core\Import-Module Pester -ErrorAction Ignore -PassThru
}
if (!$pesterModule) {
Write-Warning "Failed to import Pester. You must install Pester module to run or debug Pester tests."
Write-Warning "$(if ($MinimumVersion5) {"Recommended version to install is Pester 5.0.0 or newer. "})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: Use Legacy Code Lens is disabled, but Pester $($pesterModule.Version) is loaded. Some of the code lens features might not work as expected."
}
function Get-InvokePesterParams {
$invokePesterParams = @{
Script = $ScriptPath
}
if ($pesterModule.Version -ge '3.4.0') {
# -PesterOption was introduced before 3.4.0, and VSCodeMarker in 4.0.3-rc,
# but because no-one checks the integrity of this hashtable we can call
# all of the versions down to 3.4.0 like this
$invokePesterParams.Add("PesterOption", @{ IncludeVSCodeMarker = $true })
}
if ($pesterModule.Version -ge '3.4.5') {
# -Show was introduced in 3.4.5
$invokePesterParams.Add("Show", $pester4Output)
}
return $invokePesterParams
}
if ($All) {
if ($pesterModule.Version -ge '5.0.0') {
$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 })
}
if ($OutputPath) {
$configuration.Add('TestResult', @{
Enabled = $true
OutputFormat = "NUnit2.5"
OutputPath = $OutputPath
})
}
Pester\Invoke-Pester -Configuration $configuration | Out-Null
}
else {
$invokePesterParams = Get-InvokePesterParams
Pester\Invoke-Pester @invokePesterParams
}
}
elseif (($LineNumber -match '\d+') -and ($pesterModule.Version -ge '4.6.0')) {
if ($pesterModule.Version -ge '5.0.0') {
$configuration = @{
Run = @{
Path = $ScriptPath
}
Filter = @{
Line = "${ScriptPath}:$LineNumber"
}
}
if ("FromPreference" -ne $Output) {
$configuration.Add('Output', @{ Verbosity = $Output })
}
if ($OutputPath) {
$configuration.Add('TestResult', @{
Enabled = $true
OutputFormat = "NUnit2.5"
OutputPath = $OutputPath
})
}
Pester\Invoke-Pester -Configuration $configuration | Out-Null
}
else {
Pester\Invoke-Pester -Script $ScriptPath -PesterOption (New-PesterOption -ScriptBlockFilter @{
IncludeVSCodeMarker=$true; Line=$LineNumber; Path=$ScriptPath}) -Show $pester4Output
}
}
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 {
$invokePesterParams = Get-InvokePesterParams
Pester\Invoke-Pester @invokePesterParams
}
}
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."
Write-Warning "To avoid this, install Pester >= 4.6.0 or remove any expressions in the TestName."
$invokePesterParams = Get-InvokePesterParams
Pester\Invoke-Pester @invokePesterParams
}