MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Write-Debug tests" -Tags "CI" {4 It "Should not have added line breaks" {5 $text = "0123456789"6 try {7 while ($text.Length -lt [Console]::WindowWidth) {8 $text += $text9 }10 } catch {11 # Ignore errors if the console doesn't support WindowWidth12 }13 $origDebugPref = $DebugPreference14 $DebugPreference = "Continue"15 try {16 $out = Write-Debug $text 5>&117 $out | Should -BeExactly $text18 }19 finally {20 $DebugPreference = $origDebugPref21 }22 }23 24 It "Should not prompt the user" {25 # This script generates an error if Write-Debug prompts the user26 # (i.e. if $DebugPreference is set to Inquire, the old v1 way)27 $p = [Diagnostics.Process]::new()28 $p.StartInfo.FileName = (Get-Process -Id $PID).Path29 $encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes("Write-Debug -Message 'A debug message' -Debug"))30 $p.StartInfo.Arguments = "-EncodedCommand $encoded -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -OutputFormat text"31 $p.StartInfo.UseShellExecute = $false32 $p.StartInfo.RedirectStandardError = $true33 $p.Start() | Out-Null34 $out = $p.StandardError.ReadToEnd()35 $out | Should -BeNullOrEmpty36 }37 38 It "'-Debug' should not trigger 'ShouldProcess'" {39 $pwsh = [PowerShell]::Create()40 $pwsh.AddScript(@'41function Test-DebugWithConfirm42{43 [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')]44 Param ()45 46 PROCESS47 {48 Write-Debug -Message "Debug_Message1"49 If ($PSCmdlet.ShouldProcess('Doing the thing.','Proceed?','Ready to do the thing.'))50 {51 Write-Output 'success'52 }53 Write-Debug -Message "Debug_Message2"54 }55 56 END {}57}58'@)59 $pwsh.Invoke()60 $pwsh.Commands.Clear()61 $pwsh.Streams.ClearStreams()62 63 try {64 $result = $pwsh.AddScript("Test-DebugWithConfirm -Debug").Invoke()65 $result.Count | Should -BeExactly 166 $result[0] | Should -BeExactly 'success'67 68 $pwsh.Streams.Error.Count | Should -BeExactly 069 $pwsh.Streams.Debug.Count | Should -BeExactly 270 $pwsh.Streams.Debug[0] | Should -BeExactly 'Debug_Message1'71 $pwsh.Streams.Debug[1] | Should -BeExactly 'Debug_Message2'72 }73 finally {74 $pwsh.Dispose()75 }76 }77}78 