Skip to content

Commit c5dd825

Browse files
authored
Fix module import performance regression (#2336)
1 parent 06ab106 commit c5dd825

24 files changed

+163
-51
lines changed

src/Main.ps1

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,43 @@ function Add-ShouldOperator {
119119
Add-AssertionDynamicParameterSet -AssertionEntry $entry
120120
}
121121

122+
function Set-ShouldOperatorHelpMessage {
123+
<#
124+
.SYNOPSIS
125+
Sets the helpmessage for a Should-operator. Used in Should's online help for the switch-parameter.
126+
.PARAMETER OperatorName
127+
The name of the assertion/operator.
128+
.PARAMETER HelpMessage
129+
Help message for switch-parameter for the operator in Should.
130+
.NOTES
131+
Internal function as it's only useful for built-in Should operators/assertion atm. to improve online docs.
132+
Can be merged into Add-ShouldOperator later if we'd like to make it pulic and include value in Get-ShouldOperator
133+
134+
https://github.com/pester/Pester/issues/2335
135+
#>
136+
[CmdletBinding()]
137+
param (
138+
[Parameter(Mandatory = $true, Position = 0)]
139+
[string] $OperatorName,
140+
[Parameter(Mandatory = $true, Position = 1)]
141+
[string] $HelpMessage
142+
)
143+
144+
end {
145+
$OperatorParam = $script:AssertionDynamicParams[$OperatorName]
146+
147+
if ($null -eq $OperatorParam) {
148+
throw "Should operator '$OperatorName' is not registered"
149+
}
150+
151+
foreach ($attr in $OperatorParam.Attributes) {
152+
if ($attr -is [System.Management.Automation.ParameterAttribute]) {
153+
$attr.HelpMessage = $HelpMessage
154+
}
155+
}
156+
}
157+
}
158+
122159
function Test-AssertionOperatorIsDuplicate {
123160
param (
124161
[psobject] $Operator
@@ -163,12 +200,6 @@ function Add-AssertionDynamicParameterSet {
163200
$attribute = & $SafeCommands['New-Object'] Management.Automation.ParameterAttribute
164201
$attribute.ParameterSetName = $AssertionEntry.Name
165202

166-
# Add synopsis as HelpMessage to show in online help for Should parameters.
167-
$assertHelp = $commandInfo | & $SafeCommands['Get-Help']
168-
# Ignore functions without synopsis defined (they show syntax)
169-
if ($assertHelp.Synopsis -notmatch '^\s*__AssertionTest__((\s+\[+?-\w+)|$)') {
170-
$attribute.HelpMessage = $assertHelp.Synopsis
171-
}
172203

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

246278
$null = $dynamic.Attributes.Add($attribute)
247279
}

src/functions/Pester.SessionState.Mock.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,9 @@ function Should-InvokeVerifiable ([switch] $Negate, [string] $Because) {
669669
-InternalName Should-InvokeVerifiable `
670670
-Test ${function:Should-InvokeVerifiable}
671671

672+
Set-ShouldOperatorHelpMessage -OperatorName InvokeVerifiable `
673+
-HelpMessage 'Checks if any Verifiable Mock has not been invoked. If so, this will throw an exception.'
674+
672675
function Assert-MockCalled {
673676
<#
674677
.SYNOPSIS
@@ -1034,6 +1037,9 @@ function Should-Invoke {
10341037
-InternalName Should-Invoke `
10351038
-Test ${function:Should-Invoke}
10361039

1040+
Set-ShouldOperatorHelpMessage -OperatorName Invoke `
1041+
-HelpMessage 'Checks if a Mocked command has been called a certain number of times and throws an exception if it has not.'
1042+
10371043
function Invoke-Mock {
10381044
[CmdletBinding()]
10391045
param (

src/functions/assertions/Be.ps1

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,15 @@ function NotShouldBeFailureMessage($ActualValue, $ExpectedValue, $Because) {
6363
return "Expected $(Format-Nicely $ExpectedValue) to be different from the actual value,$(if ($null -ne $Because) { Format-Because $Because }) but got the same value."
6464
}
6565

66-
& $script:SafeCommands['Add-ShouldOperator'] -Name Be `
66+
& $script:SafeCommands['Add-ShouldOperator'] -Name Be `
6767
-InternalName Should-Be `
6868
-Test ${function:Should-Be} `
6969
-Alias 'EQ' `
7070
-SupportsArrayInput
7171

72+
Set-ShouldOperatorHelpMessage -OperatorName Be `
73+
-HelpMessage 'Compares one object with another for equality and throws if the two objects are not the same.'
74+
7275
#BeExactly
7376
function Should-BeExactly($ActualValue, $ExpectedValue, $Because) {
7477
<#
@@ -133,12 +136,14 @@ function NotShouldBeExactlyFailureMessage($ActualValue, $ExpectedValue, $Because
133136
return "Expected $(Format-Nicely $ExpectedValue) to be different from the actual value,$(if ($null -ne $Because) { Format-Because $Because }) but got exactly the same value."
134137
}
135138

136-
& $script:SafeCommands['Add-ShouldOperator'] -Name BeExactly `
139+
& $script:SafeCommands['Add-ShouldOperator'] -Name BeExactly `
137140
-InternalName Should-BeExactly `
138141
-Test ${function:Should-BeExactly} `
139142
-Alias 'CEQ' `
140143
-SupportsArrayInput
141144

145+
Set-ShouldOperatorHelpMessage -OperatorName BeExactly `
146+
-HelpMessage 'Compares one object with another for equality and throws if the two objects are not the same. This comparison is case sensitive.'
142147

143148
#common functions
144149
function Get-CompareStringMessage {

src/functions/assertions/BeGreaterThan.ps1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,22 @@ function Should-BeLessOrEqual($ActualValue, $ExpectedValue, [switch] $Negate, [s
5858
}
5959
}
6060

61-
& $script:SafeCommands['Add-ShouldOperator'] -Name BeGreaterThan `
61+
& $script:SafeCommands['Add-ShouldOperator'] -Name BeGreaterThan `
6262
-InternalName Should-BeGreaterThan `
6363
-Test ${function:Should-BeGreaterThan} `
6464
-Alias 'GT'
6565

66-
& $script:SafeCommands['Add-ShouldOperator'] -Name BeLessOrEqual `
66+
Set-ShouldOperatorHelpMessage -OperatorName BeGreaterThan `
67+
-HelpMessage "Asserts that a number (or other comparable value) is greater than an expected value. Uses PowerShell's -gt operator to compare the two values."
68+
69+
& $script:SafeCommands['Add-ShouldOperator'] -Name BeLessOrEqual `
6770
-InternalName Should-BeLessOrEqual `
6871
-Test ${function:Should-BeLessOrEqual} `
6972
-Alias 'LE'
7073

74+
Set-ShouldOperatorHelpMessage -OperatorName BeLessOrEqual `
75+
-HelpMessage "Asserts that a number (or other comparable value) is lower than, or equal to an expected value. Uses PowerShell's -le operator to compare the two values."
76+
7177
#keeping tests happy
7278
function ShouldBeGreaterThanFailureMessage() {
7379
}

src/functions/assertions/BeIn.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@
3434
}
3535
}
3636

37-
& $script:SafeCommands['Add-ShouldOperator'] -Name BeIn `
37+
& $script:SafeCommands['Add-ShouldOperator'] -Name BeIn `
3838
-InternalName Should-BeIn `
3939
-Test ${function:Should-BeIn}
4040

41+
Set-ShouldOperatorHelpMessage -OperatorName BeIn `
42+
-HelpMessage "Asserts that a collection of values contain a specific value. Uses PowerShell's -contains operator to confirm."
4143

4244
function ShouldBeInFailureMessage() {
4345
}

src/functions/assertions/BeLessThan.ps1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,22 @@ function Should-BeGreaterOrEqual($ActualValue, $ExpectedValue, [switch] $Negate,
5858
}
5959
}
6060

61-
& $script:SafeCommands['Add-ShouldOperator'] -Name BeLessThan `
61+
& $script:SafeCommands['Add-ShouldOperator'] -Name BeLessThan `
6262
-InternalName Should-BeLessThan `
6363
-Test ${function:Should-BeLessThan} `
6464
-Alias 'LT'
6565

66-
& $script:SafeCommands['Add-ShouldOperator'] -Name BeGreaterOrEqual `
66+
Set-ShouldOperatorHelpMessage -OperatorName BeLessThan `
67+
-HelpMessage "Asserts that a number (or other comparable value) is lower than an expected value. Uses PowerShell's -lt operator to compare the two values."
68+
69+
& $script:SafeCommands['Add-ShouldOperator'] -Name BeGreaterOrEqual `
6770
-InternalName Should-BeGreaterOrEqual `
6871
-Test ${function:Should-BeGreaterOrEqual} `
6972
-Alias 'GE'
7073

74+
Set-ShouldOperatorHelpMessage -OperatorName BeGreaterOrEqual `
75+
-HelpMessage "Asserts that a number (or other comparable value) is greater than or equal to an expected value. Uses PowerShell's -ge operator to compare the two values."
76+
7177
#keeping tests happy
7278
function ShouldBeLessThanFailureMessage() {
7379
}

src/functions/assertions/BeLike.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,13 @@
4242
}
4343
}
4444

45-
& $script:SafeCommands['Add-ShouldOperator'] -Name BeLike `
45+
& $script:SafeCommands['Add-ShouldOperator'] -Name BeLike `
4646
-InternalName Should-BeLike `
4747
-Test ${function:Should-BeLike}
4848

49+
Set-ShouldOperatorHelpMessage -OperatorName BeLike `
50+
-HelpMessage "Asserts that the actual value matches a wildcard pattern using PowerShell's -like operator. This comparison is not case-sensitive."
51+
4952
function ShouldBeLikeFailureMessage() {
5053
}
5154
function NotShouldBeLikeFailureMessage() {

src/functions/assertions/BeLikeExactly.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@
4141
}
4242
}
4343

44-
& $script:SafeCommands['Add-ShouldOperator'] -Name BeLikeExactly `
44+
& $script:SafeCommands['Add-ShouldOperator'] -Name BeLikeExactly `
4545
-InternalName Should-BeLikeExactly `
4646
-Test ${function:Should-BeLikeExactly}
4747

48+
Set-ShouldOperatorHelpMessage -OperatorName BeLikeExactly `
49+
-HelpMessage "Asserts that the actual value matches a wildcard pattern using PowerShell's -like operator. This comparison is case-sensitive."
50+
4851
function ShouldBeLikeExactlyFailureMessage() {
4952
}
5053
function NotShouldBeLikeExactlyFailureMessage() {

src/functions/assertions/BeNullOrEmpty.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ function NotShouldBeNullOrEmptyFailureMessage ($Because) {
7272
return "Expected a value,$(Format-Because $Because) but got `$null or empty."
7373
}
7474

75-
& $script:SafeCommands['Add-ShouldOperator'] -Name BeNullOrEmpty `
75+
& $script:SafeCommands['Add-ShouldOperator'] -Name BeNullOrEmpty `
7676
-InternalName Should-BeNullOrEmpty `
7777
-Test ${function:Should-BeNullOrEmpty} `
7878
-SupportsArrayInput
79+
80+
Set-ShouldOperatorHelpMessage -OperatorName BeNullOrEmpty `
81+
-HelpMessage "Checks values for null or empty (strings). The static [String]::IsNullOrEmpty() method is used to do the comparison."

src/functions/assertions/BeOfType.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,14 @@ function Should-BeOfType($ActualValue, $ExpectedType, [switch] $Negate, [string]
6767
}
6868

6969

70-
& $script:SafeCommands['Add-ShouldOperator'] -Name BeOfType `
70+
& $script:SafeCommands['Add-ShouldOperator'] -Name BeOfType `
7171
-InternalName Should-BeOfType `
7272
-Test ${function:Should-BeOfType} `
7373
-Alias 'HaveType'
7474

75+
Set-ShouldOperatorHelpMessage -OperatorName BeOfType `
76+
-HelpMessage "Asserts that the actual value should be an object of a specified type (or a subclass of the specified type) using PowerShell's -is operator."
77+
7578
function ShouldBeOfTypeFailureMessage() {
7679
}
7780

0 commit comments

Comments
 (0)