TylerLeonhardt / vscode-pester-test-adapter

MIT License
33 stars 13 forks source link

Added support for Pester v5.2+ to resolve Issue 46 #50

Open mikerosile opened 3 years ago

mikerosile commented 3 years ago

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 the showWarningMessage function.

JustinGrote commented 3 years ago

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
TylerLeonhardt commented 3 years ago

Ideally we still support older versions of Pester 5 but I like eventually being 5.2+ only.

JustinGrote commented 3 years ago

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,
]);
TylerLeonhardt commented 3 years ago

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!!

mikerosile commented 3 years ago

I just pushed a revision to main.ts, which checks for the Pester module during activation ( function activate() ). This change should allow for calling Get-Module Pester elsewhere without worry of failure.

I like @JustinGrote 's use of the #Requires statement(s), and separation of PowerShell scripts to their own included files. As @JustinGrote mentioned, he was awaiting this P.R. to be resolved so I have not made too many other changes to the code, focusing primarily on Pester 5.2+ support and deprecation warning(s) of older versions.

JustinGrote commented 3 years ago

@TylerLeonhardt LGTM