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

Do not cleanup previous mocks in inner Invoke-Pester #2337

Merged
merged 2 commits into from
May 6, 2023
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
11 changes: 6 additions & 5 deletions src/Main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -638,16 +638,17 @@ function Invoke-Pester {
# this will inherit to child scopes and allow Describe / Context to run directly from a file or command line
$invokedViaInvokePester = $true

if ($null -eq $state) {
# Cleanup any leftover mocks from previous runs, but only if we are not running in a nested Pester-run
# todo: move mock cleanup to BeforeAllBlockContainer when there is any?
Remove-MockFunctionsAndAliases -SessionState $PSCmdlet.SessionState
}

# this will inherit to child scopes and allow Pester to run in Pester, not checking if this is
# already defined because we want a clean state for this Invoke-Pester even if it runs inside another
# testrun (which calls Invoke-Pester itself)
$state = New-PesterState

# TODO: Remove all references to mock table, there should not be many.
$script:mockTable = @{}
# todo: move mock cleanup to BeforeAllBlockContainer when there is any
Remove-MockFunctionsAndAliases -SessionState $PSCmdlet.SessionState

# store CWD so we can revert any changes at the end
$initialPWD = $pwd.Path
}
Expand Down
6 changes: 4 additions & 2 deletions src/functions/Pester.SessionState.Mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,9 @@ function Invoke-Mock {
# if we are targeting a module use the behaviors for the current module, but if there is no default the fall back to the non-module default behavior.
# do not fallback to non-module filtered behaviors. This is here for safety, and for compatibility when doing Mock Remove-Item {}, and then mocking in module
# then the default mock for Remove-Item should be effective.
$behaviors = if ($targettingAModule) {

# using @() to always get array. This avoids null error in Invoke-MockInternal when no behaviors where found (if-else unwraps the lists)
$behaviors = @(if ($targettingAModule) {
# we have default module behavior add it to the filtered behaviors if there are any
if ($null -ne $moduleDefaultBehavior) {
$moduleBehaviors.Add($moduleDefaultBehavior)
Expand All @@ -1217,7 +1219,7 @@ function Invoke-Mock {
}

$nonModuleBehaviors
}
})

$callHistory = (Get-MockDataForCurrentScope).CallHistory

Expand Down
38 changes: 35 additions & 3 deletions tst/functions/Mock.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1287,9 +1287,6 @@ Describe 'Dot Source Test' {
}

It "Doesn't call the mock with any other parameters" {
InPesterModuleScope {
fflaten marked this conversation as resolved.
Show resolved Hide resolved
$global:calls = $mockTable['||Test-Path'].CallHistory
}
Should -Invoke Test-Path -Exactly 0 -ParameterFilter { $Path -ne 'Test' } -Scope Describe
}
}
Expand Down Expand Up @@ -3138,3 +3135,38 @@ Describe 'Mocking in manifest modules' {
Should -Invoke -CommandName 'myManifestPrivateFunction' -ModuleName $moduleName -Exactly -Times 1
}
}

Describe 'Mocking with nested Pester runs' {
BeforeAll {
Mock Get-Date { 1 }

$innerRun = Invoke-Pester -Container (New-PesterContainer -ScriptBlock {
Describe 'inner' {
It 'local mock works' {
Mock Get-Command { 2 }
Get-Command | Should -Be 2
}

It 'outer mock is not available' {
Get-Date | Should -Not -Be 1
}
}
}) -Output None -PassThru
}

It 'Mocks in outer run works after nested Invoke-Pester' {
# https://github.com/pester/Pester/issues/2074
Get-Date | Should -Be 1
# Outer mock should not have been called from nested run
Should -Invoke Get-Date -Exactly -Times 1
}

It 'Mocking works in nested run' {
$innerRun.Result | Should -Be 'Passed'
$innerRun.PassedCount | Should -Be 2
}

It 'Mocks in nested run do not leak to outside' {
Get-Command Get-ChildItem | Should -Not -Be 2
}
}