Skip to content

Commit a3d2070

Browse files
authored
Make initial Mock cleanup more performant (#2332)
Initial Mock cleanup optimized by querying just for functions of given name rather than all types of commands from all modules.
1 parent 4b29eda commit a3d2070

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

src/functions/Mock.ps1

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,31 +1580,33 @@ function Remove-MockFunctionsAndAliases ($SessionState) {
15801580
# So before putting Pester state in place we should make sure that all Pester mocks are gone
15811581
# by deleting every alias pointing to a function that starts with PesterMock_. Then we also delete the
15821582
# bootstrap function.
1583+
#
1584+
# Avoid using Get-Command to find mock functions, it is slow. https://github.com/pester/Pester/discussions/2331
15831585
$Get_Alias = $script:SafeCommands['Get-Alias']
1584-
$Get_Command = $script:SafeCommands['Get-Command']
1586+
$Get_ChildItem = $script:SafeCommands['Get-ChildItem']
15851587
$Remove_Item = $script:SafeCommands['Remove-Item']
15861588
foreach ($alias in (& $Get_Alias -Definition "PesterMock_*")) {
15871589
& $Remove_Item "alias:/$($alias.Name)"
15881590
}
15891591

1590-
foreach ($bootstrapFunction in (& $Get_Command -Name "PesterMock_*")) {
1591-
& $Remove_Item "function:/$($bootstrapFunction.Name)"
1592+
foreach ($bootstrapFunction in (& $Get_ChildItem -Name "function:/PesterMock_*")) {
1593+
& $Remove_Item "function:/$($bootstrapFunction)" -Recurse -Force -Confirm:$false
15921594
}
15931595

15941596
$ScriptBlock = {
1595-
param ($Get_Alias, $Get_Command, $Remove_Item)
1597+
param ($Get_Alias, $Get_ChildItem, $Remove_Item)
15961598
foreach ($alias in (& $Get_Alias -Definition "PesterMock_*")) {
15971599
& $Remove_Item "alias:/$($alias.Name)"
15981600
}
15991601

1600-
foreach ($bootstrapFunction in (& $Get_Command -Name "PesterMock_*")) {
1601-
& $Remove_Item "function:/$($bootstrapFunction.Name)"
1602+
foreach ($bootstrapFunction in (& $Get_ChildItem -Name "function:/PesterMock_*")) {
1603+
& $Remove_Item "function:/$($bootstrapFunction)" -Recurse -Force -Confirm:$false
16021604
}
16031605
}
16041606

16051607
# clean up in caller session state
16061608
Set-ScriptBlockScope -SessionState $SessionState -ScriptBlock $ScriptBlock
1607-
& $ScriptBlock $Get_Alias $Get_Command $Remove_Item
1609+
& $ScriptBlock $Get_Alias $Get_ChildItem $Remove_Item
16081610

16091611
# clean up also in all loaded script and manifest modules
16101612
$modules = & $script:SafeCommands['Get-Module']
@@ -1618,7 +1620,7 @@ function Remove-MockFunctionsAndAliases ($SessionState) {
16181620
# https://github.com/PowerShell/PowerShell/blob/658837323599ab1c7a81fe66fcd43f7420e4402b/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs#L51-L55
16191621
# https://github.com/pester/Pester/issues/1921
16201622
if ('Script', 'Manifest' -contains $module.ModuleType -and $null -ne $module.SessionState) {
1621-
& ($module) $ScriptBlock $Get_Alias $Get_Command $Remove_Item
1623+
& ($module) $ScriptBlock $Get_Alias $Get_ChildItem $Remove_Item
16221624
}
16231625
}
16241626
}
@@ -1823,13 +1825,13 @@ function Repair-EnumParameters {
18231825
# https://github.com/PowerShell/PowerShell/issues/17546
18241826
$ast = [System.Management.Automation.Language.Parser]::ParseInput("param($ParamBlock)", [ref]$null, [ref]$null)
18251827
$brokenValidateRange = $ast.FindAll({
1826-
param($node)
1827-
$node -is [System.Management.Automation.Language.AttributeAst] -and
1828-
$node.TypeName.Name -match '(?:ValidateRange|System\.Management\.Automation\.ValidateRangeAttribute)$' -and
1829-
$node.NamedArguments.Count -gt 0 -and
1830-
# triple checking for broken argument - it won't have a value/expression
1831-
$node.NamedArguments.ExpressionOmitted -notcontains $false
1832-
}, $false)
1828+
param($node)
1829+
$node -is [System.Management.Automation.Language.AttributeAst] -and
1830+
$node.TypeName.Name -match '(?:ValidateRange|System\.Management\.Automation\.ValidateRangeAttribute)$' -and
1831+
$node.NamedArguments.Count -gt 0 -and
1832+
# triple checking for broken argument - it won't have a value/expression
1833+
$node.NamedArguments.ExpressionOmitted -notcontains $false
1834+
}, $false)
18331835

18341836
if ($brokenValidateRange.Count -eq 0) {
18351837
# No errors found. Return original string

0 commit comments

Comments
 (0)