Skip to content

Commit 42f63fa

Browse files
authored
Migrate Pester version detection into an InovkePester stub script (#1776)
* Migrate Pester version detection into an InovkePester stub script * Move TestName warning into script * Improve stub script, move module loaded check to beginning * Address Codacy issue * Address PR feedback * Move TestName check before LineNumber in case of multiple blocks/line
0 parents  commit 42f63fa

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env pwsh
2+
3+
<#
4+
.SYNOPSIS
5+
Stub around Invoke-Pester command used by VSCode PowerShell extension.
6+
.DESCRIPTION
7+
The stub checks the version of Pester and if >= 4.6.0, invokes Pester
8+
using the LineNumber parameter (if specified). Otherwise, it invokes
9+
using the TestName parameter (if specified). If the All parameter
10+
is specified, then all the tests are invoked in the specifed file.
11+
Finally, if none of these three parameters are specified, all tests
12+
are invoked and a warning is issued indicating what the user can do
13+
to allow invocation of individual Describe blocks.
14+
.EXAMPLE
15+
PS C:\> .\InvokePesterStub.ps1 ~\project\test\foo.tests.ps1 -LineNumber 14
16+
Invokes a specific test by line number in the specified file.
17+
.EXAMPLE
18+
PS C:\> .\InvokePesterStub.ps1 ~\project\test\foo.tests.ps1 -TestName 'Foo Tests'
19+
Invokes a specific test by test name in the specified file.
20+
.EXAMPLE
21+
PS C:\> .\InvokePesterStub.ps1 ~\project\test\foo.tests.ps1 -All
22+
Invokes all tests in the specified file.
23+
.INPUTS
24+
None
25+
.OUTPUTS
26+
None
27+
#>
28+
param(
29+
# Specifies the path to the test script.
30+
[Parameter(Position=0, Mandatory)]
31+
[ValidateNotNullOrEmpty()]
32+
[string]
33+
$ScriptPath,
34+
35+
# Specifies the name of the test taken from the Describe block's name.
36+
[Parameter()]
37+
[string]
38+
$TestName,
39+
40+
# Specifies the starting line number of the DescribeBlock. This feature requires
41+
# Pester 4.6.0 or higher.
42+
[Parameter()]
43+
[ValidatePattern('\d*')]
44+
[string]
45+
$LineNumber,
46+
47+
# If specified, executes all the tests in the specified test script.
48+
[Parameter()]
49+
[switch]
50+
$All
51+
)
52+
53+
$pesterModule = Microsoft.PowerShell.Core\Get-Module Pester
54+
if (!$pesterModule) {
55+
Write-Output "Importing Pester module..."
56+
$pesterModule = Microsoft.PowerShell.Core\Import-Module Pester -ErrorAction Ignore -PassThru
57+
if (!$pesterModule) {
58+
# 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"
61+
return
62+
}
63+
}
64+
65+
if ($All) {
66+
Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true}
67+
}
68+
elseif ($TestName) {
69+
Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} -TestName $TestName
70+
}
71+
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})
74+
}
75+
else {
76+
# We get here when the TestName expression is of type ExpandableStringExpressionAst.
77+
# PSES will not attempt to "evaluate" the expression so it returns null for the TestName.
78+
Write-Warning "The Describe block's TestName cannot be evaluated. EXECUTING ALL TESTS instead."
79+
Write-Warning "To avoid this, install Pester >= 4.6.0 or remove any expressions in the TestName."
80+
81+
Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true}
82+
}

0 commit comments

Comments
 (0)