Skip to content
This repository was archived by the owner on Apr 27, 2023. It is now read-only.

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

Open
mikerosile wants to merge 4 commits intoTylerLeonhardt:masterfrom
mikerosile:master
Open

Added support for Pester v5.2+ to resolve Issue 46#50
mikerosile wants to merge 4 commits intoTylerLeonhardt:masterfrom
mikerosile:master

Conversation

@mikerosile
Copy link
Copy Markdown

@mikerosile mikerosile commented Jun 2, 2021

This P.R. is to resolve #46

  • Added Warning if Pester below 5.2.0
  • Updated discovery code to work with Pester 5.2+

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.

Updated discvoery code to work with Pester 5.2+
@mikerosile mikerosile changed the title Added support for Pester v5.2+ Added support for Pester v5.2+ to resolve Issue 46 Jun 2, 2021
Copy link
Copy Markdown
Owner

@TylerLeonhardt TylerLeonhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Just a few comments.

Comment thread src/pesterTests.ts
Comment thread src/pesterTests.ts
Comment thread src/pesterTests.ts
Comment thread src/powershellScripts.ts Outdated
@JustinGrote
Copy link
Copy Markdown

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
Copy link
Copy Markdown
Owner

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

@JustinGrote
Copy link
Copy Markdown

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
Copy link
Copy Markdown
Owner

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

Copy link
Copy Markdown

@JustinGrote JustinGrote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread src/powershellScripts.ts Outdated
Comment thread src/powershellScripts.ts Outdated
Michael Rosile added 2 commits June 4, 2021 11:48
@mikerosile
Copy link
Copy Markdown
Author

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
Copy link
Copy Markdown

@TylerLeonhardt LGTM

Comment thread src/powershellScripts.ts Outdated
Comment thread src/powershellScripts.ts Outdated
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test discovery hanging with Pester 5.2.0

4 participants