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

Clean up mocks on start #1035

Merged
merged 15 commits into from
May 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Functions/Describe.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ about_TestDrive
if ($null -eq (& $SafeCommands['Get-Variable'] -Name Pester -ValueOnly -ErrorAction $script:IgnoreErrorPreference))
{
# User has executed a test script directly instead of calling Invoke-Pester
Remove-MockFunctionsAndAliases
$Pester = New-PesterState -Path (& $SafeCommands['Resolve-Path'] .) -TestNameFilter $null -TagFilter @() -SessionState $PSCmdlet.SessionState
$script:mockTable = @{}
}
Expand Down
2 changes: 1 addition & 1 deletion Functions/It.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ function Get-ParameterDictionary
[scriptblock] $ScriptBlock
)

$guid = [guid]::NewGuid().Guid
$guid = [Guid]::NewGuid().Guid

try
{
Expand Down
21 changes: 20 additions & 1 deletion Functions/Mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ about_Mocking
CallHistory = @()
DynamicParamScriptBlock = $dynamicParamScriptBlock
Aliases = @()
BootstrapFunctionName = [Guid]::NewGuid().ToString()
BootstrapFunctionName = 'PesterMock_' + [Guid]::NewGuid().Guid
}

$mockTable["$ModuleName||$CommandName"] = $mock
Expand Down Expand Up @@ -1501,3 +1501,22 @@ function Test-IsClosure
$null -ne ($matches[1] -as [guid])
)
}

function Remove-MockFunctionsAndAliases {
# 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.
foreach ($alias in (& $script:SafeCommands['Get-Alias'] -Definition "PesterMock_*"))
{
& $script:SafeCommands['Remove-Item'] "alias:/$($alias.Name)"
}

foreach ($bootstrapFunction in (& $script:SafeCommands['Get-Command'] -Name "PesterMock_*"))
{
& $script:SafeCommands['Remove-Item'] "function:/$($bootstrapFunction.Name)"
}
}
2 changes: 2 additions & 0 deletions Pester.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ $script:SafeCommands = @{
'Export-ModuleMember' = Get-Command -Name Export-ModuleMember -Module Microsoft.PowerShell.Core @safeCommandLookupParameters
'ForEach-Object' = Get-Command -Name ForEach-Object -Module Microsoft.PowerShell.Core @safeCommandLookupParameters
'Format-Table' = Get-Command -Name Format-Table -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Get-Alias' = Get-Command -Name Get-Alias -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Get-ChildItem' = Get-Command -Name Get-ChildItem -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Get-Command' = Get-Command -Name Get-Command -Module Microsoft.PowerShell.Core @safeCommandLookupParameters
'Get-Content' = Get-Command -Name Get-Content -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
Expand Down Expand Up @@ -818,6 +819,7 @@ New-PesterOption
}

$script:mockTable = @{}
Remove-MockFunctionsAndAliases
$pester = New-PesterState -TestNameFilter $TestName -TagFilter ($Tag -split "\s") -ExcludeTagFilter ($ExcludeTag -split "\s") -SessionState $PSCmdlet.SessionState -Strict:$Strict -Show:$Show -PesterOption $PesterOption

try
Expand Down