Skip to content
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
4 changes: 4 additions & 0 deletions src/Main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ function Invoke-Pester {
[PesterConfiguration] $Configuration
)
begin {
# Prevent $WhatIfPreference from leaking into Pester internals (#2585).
# When the caller sets $WhatIfPreference = $true, it propagates to child
# scopes and breaks commands that Pester relies on (New-Item, Remove-Item, etc.).
$WhatIfPreference = $false
$start = [DateTime]::Now
# this will inherit to child scopes and allow Describe / Context to run directly from a file or command line
$invokedViaInvokePester = $true
Expand Down
26 changes: 26 additions & 0 deletions tst/Pester.RSpec.InNewProcess.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,30 @@ i -PassThru:$PassThru {
}
}
}

b '$WhatIfPreference does not break Pester' {
t 'Invoke-Pester succeeds when $WhatIfPreference is $true' {
$sb = {
$WhatIfPreference = $true

$PesterPreference = [PesterConfiguration]::Default
$PesterPreference.Output.Verbosity = 'None'

$container = New-PesterContainer -ScriptBlock {
Describe 'd1' {
It 'i1' {
1 | Should -Be 1
}
}
}
$r = Invoke-Pester -Container $container -PassThru
if ($r.FailedCount -ne 0) { throw "Expected 0 failures, got $($r.FailedCount)" }
if ($r.PassedCount -ne 1) { throw "Expected 1 passed, got $($r.PassedCount)" }
}

$output = Invoke-InNewProcess $sb
$whatIfLines = $output | Select-String -Pattern 'What if:'
@($whatIfLines).Count | Verify-Equal 0
}
}
}
Loading