Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make initial Mock cleanup more performant #2332

Merged
merged 4 commits into from
Apr 1, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 15 additions & 22 deletions src/functions/Mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1573,33 +1573,26 @@ function Test-IsClosure {
}

function Remove-MockFunctionsAndAliases ($SessionState) {
# when a test is terminated (e.g. by stopping at a breakpoint and then stoping the execution of the script)
# the aliases and bootstrap functions for the currently mocked functions will remain in place
# When a test is terminated (e.g. by stopping at a breakpoint and then stoping the execution of the script)
# the aliases and bootstrap functions for the currently mocked functions will remain in place.
# Then on subsequent runs the bootstrap function will be picked up instead of the real command,
# because there is still an alias associated with it, and the test will fail.
# So before putting Pester state in place we should make sure that all Pester mocks are gone
# by deleting every alias pointing to a function that starts with PesterMock_. Then we also delete the
# bootstrap function.
# by deleting every alias pointing to a function that starts with PesterMock_.
# As a perf optimization we don't delete the bootstrap functions because running Get-Command
# in every loaded module has significant overhead (https://github.com/pester/Pester/discussions/2331)
# and the PesterMock_ function won't be picked up without an alias.
$Get_Alias = $script:SafeCommands['Get-Alias']
$Get_Command = $script:SafeCommands['Get-Command']
$Remove_Item = $script:SafeCommands['Remove-Item']
foreach ($alias in (& $Get_Alias -Definition "PesterMock_*")) {
& $Remove_Item "alias:/$($alias.Name)"
}
nohwnd marked this conversation as resolved.
Show resolved Hide resolved

foreach ($bootstrapFunction in (& $Get_Command -Name "PesterMock_*")) {
& $Remove_Item "function:/$($bootstrapFunction.Name)"
}

$ScriptBlock = {
param ($Get_Alias, $Get_Command, $Remove_Item)
param ($Get_Alias, $Remove_Item)
foreach ($alias in (& $Get_Alias -Definition "PesterMock_*")) {
& $Remove_Item "alias:/$($alias.Name)"
}

foreach ($bootstrapFunction in (& $Get_Command -Name "PesterMock_*")) {
& $Remove_Item "function:/$($bootstrapFunction.Name)"
}
}

# clean up in caller session state
Expand All @@ -1618,7 +1611,7 @@ function Remove-MockFunctionsAndAliases ($SessionState) {
# https://github.com/PowerShell/PowerShell/blob/658837323599ab1c7a81fe66fcd43f7420e4402b/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs#L51-L55
# https://github.com/pester/Pester/issues/1921
if ('Script', 'Manifest' -contains $module.ModuleType -and $null -ne $module.SessionState) {
& ($module) $ScriptBlock $Get_Alias $Get_Command $Remove_Item
& ($module) $ScriptBlock $Get_Alias $Remove_Item
}
}
}
Expand Down Expand Up @@ -1823,13 +1816,13 @@ function Repair-EnumParameters {
# https://github.com/PowerShell/PowerShell/issues/17546
$ast = [System.Management.Automation.Language.Parser]::ParseInput("param($ParamBlock)", [ref]$null, [ref]$null)
$brokenValidateRange = $ast.FindAll({
nohwnd marked this conversation as resolved.
Show resolved Hide resolved
param($node)
$node -is [System.Management.Automation.Language.AttributeAst] -and
$node.TypeName.Name -match '(?:ValidateRange|System\.Management\.Automation\.ValidateRangeAttribute)$' -and
$node.NamedArguments.Count -gt 0 -and
# triple checking for broken argument - it won't have a value/expression
$node.NamedArguments.ExpressionOmitted -notcontains $false
}, $false)
param($node)
$node -is [System.Management.Automation.Language.AttributeAst] -and
$node.TypeName.Name -match '(?:ValidateRange|System\.Management\.Automation\.ValidateRangeAttribute)$' -and
$node.NamedArguments.Count -gt 0 -and
# triple checking for broken argument - it won't have a value/expression
$node.NamedArguments.ExpressionOmitted -notcontains $false
}, $false)

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