Added support for Pester v5.2+ to resolve Issue 46#50
Added support for Pester v5.2+ to resolve Issue 46#50mikerosile wants to merge 4 commits intoTylerLeonhardt:masterfrom
Conversation
Updated discvoery code to work with Pester 5.2+
TylerLeonhardt
left a comment
There was a problem hiding this comment.
Looking good! Just a few comments.
|
I did a similar 5.2 fix but then noticed this PR was present. Here's my version of the embedded script. #Requires -Modules @{ ModuleName="Pester";ModuleVersion="5.2.0" }
using namespace System.Collections.Generic
using namespace Pester
$VerbosePreference = 'Ignore'
$WarningPreference = 'Ignore'
$DebugPreference = 'Ignore'
function New-SuiteObject ([Block]$Block) {
[PSCustomObject]@{
type = 'suite'
id = $Block.ScriptBlock.File + ';' + $Block.StartLine
file = $Block.ScriptBlock.File
line = $Block.StartLine - 1
label = $Block.Name
children = [List[Object]]@()
}
}
function New-TestObject ([Test]$Test) {
[PSCustomObject]@{
type = 'test'
id = $Test.ScriptBlock.File + ';' + $Test.StartLine
file = $Test.ScriptBlock.File
line = $Test.StartLine - 1
label = $Test.Name
}
}
function fold ($children, $Block) {
foreach ($b in $Block.Blocks) {
$o = (New-SuiteObject $b)
$children.Add($o)
fold $o.children $b
}
$hashset = [HashSet[string]]::new()
foreach ($t in $Block.Tests) {
$key = "$($t.ExpandedPath):$($t.StartLine)"
if ($hashset.Contains($key)) {
continue
}
$children.Add((New-TestObject $t))
$hashset.Add($key) | Out-Null
}
$hashset.Clear() | Out-Null
}
$config = New-PesterConfiguration @{
Run = @{
SkipRun = $true
PassThru = $true
}
Output = @{
Verbosity = 'None'
}
}
$found = Invoke-Pester -Configuration $config
$testSuiteInfo = [PSCustomObject]@{
type = 'suite'
id = 'root'
label = 'Pester'
children = [Collections.Generic.List[Object]]@()
}
foreach ($file in $found.Containers.Blocks) {
$fileSuite = [PSCustomObject]@{
type = 'suite'
id = $file.BlockContainer.Item.FullName
file = $file.BlockContainer.Item.FullName
label = $file.BlockContainer.Item.Name
children = [Collections.Generic.List[Object]]@()
}
$testSuiteInfo.children.Add($fileSuite)
fold $fileSuite.children $file
}
$testSuiteInfo | ConvertTo-Json -Depth 100 |
|
Ideally we still support older versions of Pester 5 but I like eventually being 5.2+ only. |
|
OK, I was working on a PR to switch to esbuild and allow the .ps1 files to stand separately and then embed them at compile time, I'll wait until this work is done though. Looks something like this as a snippet out of pesterTests.ts (can't use EncodedCommand due to stupid AV and can't use stdin because you can't provide args): import getPesterTestScript from './Get-PesterTests.ps1'
// Replace the default path with our test root directory
// A better way would be to use parameters but we can't seem to use args and a command at the same time without using a file
const script = getPesterTestScript.replace('\$pwd',`\'${this.getTestRootDirectory()}\'`)
this.log.debug(script);
const ls = spawn(exePath, [
'-NonInteractive',
'-NoLogo',
'-NoProfile',
'-Command', script,
]); |
Love this idea!! |
JustinGrote
left a comment
There was a problem hiding this comment.
OK, I was working on a PR to switch to esbuild and allow the .ps1 files to stand separately and then embed them at compile time, I'll wait until this work is done though.
Love this idea!!
My goal was to get some testing and CI going, so this way the scripts could be unit tested with Pester (SO META).
Refactored test discovery code based on Pester version
|
I just pushed a revision to I like @JustinGrote 's use of the |
|
@TylerLeonhardt LGTM |
This P.R. is to resolve #46
Please review the changes to
pesterTests.ts, I added code here to check the version of the Pester module and display a warning if the version is below 5.2. There may be a better way or better place to call theshowWarningMessagefunction.