Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Make Plugins public

Open nohwnd opened this issue 4 years ago • 3 comments

Review and improve the api of Pester plugins, to be well factored. Think about how each extension point could be used, and how we could extend the api in the future.

Then expose the plugins publicly, and think about how others could register theirs. Especially when they would like to inject them in the middle of our plugins, or replace ours with theirs. And how they would get config.

nohwnd avatar Jul 02 '21 06:07 nohwnd

replace ours with theirs. And how they would get config.

The config part is important 👍 We soon (if not already) have configuration options to disable most built-in plugins (only Mock left in 5.3.0?

So third party plugins should rely on their own config-options.

fflaten avatar Jul 02 '21 13:07 fflaten

As requested in #2020, just to let you know I'm now using the additionalPlugins to workaround code coverage configuration. It ain't pretty, and perhaps I'm missing something but I cannot find a way to make get StartLine/EndLine/... from Invoke-Pester into Enter-CoverageAnalysis; the legacy parameter set doesn't accept a hashtable for -CodeCoverage (well it does but it turns it into the string "System.Collections.HashTable") and PesterConfiguration.CodeCoverage only has Paths and enforces that to be a string array. So now I'm doing something like this:

$ccPlug = & (Get-Module Pester) {Get-CoveragePlugin}
$ccPlugStart = $ccPlug.RunStart
$ccPlugEnd = $ccPlug.End
$ccPlug.RunStart = {
  param($Context)
  $Context.Configuration['Coverage'] = @{
    Enabled = $True
    OutputFormat = 'JaCoCo'
    OutputPath = (Join-Path $PSScriptRoot 'coverage.xml')
    OutputEncoding = 'UTF8'
    Path = 'MyFile.ps1'
    StartLine = 0
    EndLine = 150
    ExcludeTests = $True
    RecursePaths = $True
    CoveragePercentTarget = 75
    UseBreakpoints = $True
    UseSingleHitBreakpoints = $True
  }
  & $ccPlugStart $Context
}
$ccPlug.End = {
  param($Context)
  & $ccPlugEnd $Context
  # Make pester do the post-processing now.
  $Context.TestRun.Configuration.CodeCoverage.Enabled = $True
}

& (Get-Module Pester) {
  $Script:additionalPlugins = $ccPlug
}

Notes:

  • ideally I'd like to specify an array of path/startline/endline and while Enter-CoverageAnalysis supports that since it takes such array, the way Get-CoveragePlugin calls it only supports one single configuration object: Enter-CoverageAnalysis -CodeCoverage $config -Logger $logger -UseBreakpoints $config.UseBreakpoints
  • https://github.com/pester/Pester/search?q=ExcludeTests is declared but not used anywhere? Get-CoverageInfoFromUserInput only refers to IncludeTests

After writing the above I realize it's perhaps better to report this as a bunch separate issues, but I'm new to Pester so perhaps all of this is known already? Otherwise let me know how to proceed.

stinos avatar Nov 08 '21 12:11 stinos

For reference, the Pester Tests adapter utilizes this API to report test results in real time so it doesn't have to wait for the final output or interpret text output. https://github.com/pester/vscode-adapter/blob/ebfe917658cf2a310ea0520d42b57ef54d54fb26/Scripts/PesterInterface.ps1#L60-L80

JustinGrote avatar Jul 03 '23 05:07 JustinGrote