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
12 changes: 5 additions & 7 deletions src/System.Management.Automation/engine/MshCommandRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2992,21 +2992,19 @@ internal ConfirmImpact ConfirmPreference
{
// WhatIf not relevant, it never gets this far in that case
if (Confirm)
return ConfirmImpact.Low;
if (Debug)
{
if (IsConfirmFlagSet) // -Debug -Confirm:$false
return ConfirmImpact.None;
return ConfirmImpact.Low;
}

if (IsConfirmFlagSet) // -Confirm:$false
if (IsConfirmFlagSet)
{
// -Confirm:$false
return ConfirmImpact.None;
}

if (!_isConfirmPreferenceCached)
{
bool defaultUsed = false;
_confirmPreference = Context.GetEnumPreference(SpecialVariables.ConfirmPreferenceVarPath, _confirmPreference, out defaultUsed);
_confirmPreference = Context.GetEnumPreference(SpecialVariables.ConfirmPreferenceVarPath, _confirmPreference, out _);
_isConfirmPreferenceCached = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,44 @@ Describe "Write-Debug tests" -Tags "CI" {
$out = $p.StandardError.ReadToEnd()
$out | Should -BeNullOrEmpty
}

It "'-Debug' should not trigger 'ShouldProcess'" {
$pwsh = [PowerShell]::Create()
$pwsh.AddScript(@'
function Test-DebugWithConfirm
{
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')]
Param ()

PROCESS
{
Write-Debug -Message "Debug_Message1"
If ($PSCmdlet.ShouldProcess('Doing the thing.','Proceed?','Ready to do the thing.'))
{
Write-Output 'success'
}
Write-Debug -Message "Debug_Message2"
}

END {}
}
'@)
$pwsh.Invoke()
$pwsh.Commands.Clear()
$pwsh.Streams.ClearStreams()

try {
$result = $pwsh.AddScript("Test-DebugWithConfirm -Debug").Invoke()
$result.Count | Should -BeExactly 1
$result[0] | Should -BeExactly 'success'

$pwsh.Streams.Error.Count | Should -BeExactly 0
$pwsh.Streams.Debug.Count | Should -BeExactly 2
$pwsh.Streams.Debug[0] | Should -BeExactly 'Debug_Message1'
$pwsh.Streams.Debug[1] | Should -BeExactly 'Debug_Message2'
}
finally {
$pwsh.Dispose()
}
}
}
Loading