-
Notifications
You must be signed in to change notification settings - Fork 513
Migrate Pester version detection into an InovkePester stub script #1776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7ec908b
Migrate Pester version detection into an InovkePester stub script
rkeithhill c916b54
Move TestName warning into script
rkeithhill a257cfa
Improve stub script, move module loaded check to beginning
rkeithhill ca00075
Address Codacy issue
rkeithhill 127062f
Address PR feedback
rkeithhill dda63b9
Move TestName check before LineNumber in case of multiple blocks/line
rkeithhill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#!/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 | ||
) | ||
|
||
$pesterModule = Microsoft.PowerShell.Core\Get-Module Pester | ||
if (!$pesterModule) { | ||
Write-Output "Importing Pester module..." | ||
$pesterModule = Microsoft.PowerShell.Core\Import-Module Pester -ErrorAction Ignore -PassThru | ||
if (!$pesterModule) { | ||
# If we still don't have an imported Pester module, that is (most likely) because Pester is not installed. | ||
Write-Warning "Failed to import the Pester module. You must install Pester to run or debug Pester tests." | ||
Write-Warning "You can install Pester by executing: Install-Module Pester -Scope CurrentUser -Force" | ||
return | ||
} | ||
} | ||
|
||
if ($All) { | ||
Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} | ||
} | ||
elseif ($TestName) { | ||
Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} -TestName $TestName | ||
} | ||
elseif (($LineNumber -match '\d+') -and ($pesterModule.Version -ge '4.6.0')) { | ||
rkeithhill marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Pester\Invoke-Pester -Script $ScriptPath -PesterOption (New-PesterOption -ScriptBlockFilter @{ | ||
IncludeVSCodeMarker=$true; Line=$LineNumber; Path=$ScriptPath}) | ||
} | ||
else { | ||
# 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." | ||
|
||
Pester\Invoke-Pester -Script $ScriptPath -PesterOption @{IncludeVSCodeMarker=$true} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.