Skip to content

Commit

Permalink
Fix module import performance
Browse files Browse the repository at this point in the history
Require internal flag to add parameter help for
built-in Should-operators. Only used by command ref-script in
docs-repo and increased import time by 3-4sec in inline build.
See #2335
  • Loading branch information
fflaten committed Apr 9, 2023
1 parent 36b3164 commit 9ab0eff
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,17 @@ function Add-AssertionDynamicParameterSet {
$attribute = & $SafeCommands['New-Object'] Management.Automation.ParameterAttribute
$attribute.ParameterSetName = $AssertionEntry.Name

# Add synopsis as HelpMessage to show in online help for Should parameters.
$assertHelp = $commandInfo | & $SafeCommands['Get-Help']
# Ignore functions without synopsis defined (they show syntax)
if ($assertHelp.Synopsis -notmatch '^\s*__AssertionTest__((\s+\[+?-\w+)|$)') {
$attribute.HelpMessage = $assertHelp.Synopsis
# Code below is only used to generate command ref in docs-repo (website).
# Adds 3-4sec to module import when inline build and comment-help, so using a flag.
# https://github.com/pester/Pester/issues/2335.
# Remove if/when migrated to external help (markdown as source).
if ($env:PESTER_GENERATE_HELP_FOR_SHOULDOPERATORS -eq '1') {
# Add synopsis as HelpMessage to show in online help for Should parameters.
$assertHelp = $commandInfo | & $SafeCommands['Get-Help']
# Ignore functions without synopsis defined (they show syntax)
if ($assertHelp.Synopsis -notmatch '^\s*__AssertionTest__((\s+\[+?-\w+)|$)') {
$attribute.HelpMessage = $assertHelp.Synopsis
}
}

$attributeCollection = & $SafeCommands['New-Object'] Collections.ObjectModel.Collection[Attribute]
Expand Down Expand Up @@ -241,7 +247,8 @@ function Add-AssertionDynamicParameterSet {
$attribute.ParameterSetName = $AssertionEntry.Name
$attribute.Mandatory = $false
$attribute.Position = ($i++)
$attribute.HelpMessage = 'Depends on operator being used. See `Get-ShouldOperator -Name <Operator>` for help.'
# Only visible in command reference on https://pester.dev. Remove if/when migrated to external help (markdown as source).
$attribute.HelpMessage = 'Depends on operator being used. See `Get-ShouldOperator -Name <Operator>` or https://pester.dev/docs/assertions/ for help.'

$null = $dynamic.Attributes.Add($attribute)
}
Expand Down
7 changes: 7 additions & 0 deletions tst/functions/Add-ShouldOperator.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ i -PassThru:$PassThru {
Import-Module "$PSScriptRoot\..\..\bin\Pester.psd1"
${function:Add-ShouldOperator} = & (Get-Module Pester) { Get-Command Add-ShouldOperator }

# Enable help for operator switch-parameters in Should.
# Internal feature only used by generate-command-reference in docs-repo. Adds 3-4sec to module import time.
$env:PESTER_GENERATE_HELP_FOR_SHOULDOPERATORS = 1

t 'Adds empty HelpMessage for Should operator without synopsis' {
function WithoutSynopsis {
param($ActualValue, $Param1)
Expand All @@ -99,5 +103,8 @@ i -PassThru:$PassThru {
Add-ShouldOperator -Name WithSynopsis -Test $function:WithSynopsis
(Get-Command -Name Should).Parameters['WithSynopsis'].ParameterSets['WithSynopsis'].HelpMessage | Verify-Equal 'Here I am'
}

# Revert to default behavior to avoid import perf hit
$env:PESTER_GENERATE_HELP_FOR_SHOULDOPERATORS = 0
}
}

0 comments on commit 9ab0eff

Please sign in to comment.