MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe 'OutputRendering tests' -Tag 'CI' {5 BeforeAll {6 $originalDefaultParameterValues = $PSDefaultParameterValues.Clone()7 # Console host does not support VT100 escape sequences on Windows 2012R2 or earlier8 9 if (-not $host.ui.SupportsVirtualTerminal) {10 $PSDefaultParameterValues["it:skip"] = $true11 }12 }13 14 BeforeEach {15 if ($null -ne $PSStyle) {16 $oldOutputRendering = $PSStyle.OutputRendering17 }18 }19 20 AfterEach {21 if ($null -ne $PSStyle) {22 $PSStyle.OutputRendering = $oldOutputRendering23 }24 }25 26 AfterAll {27 $global:PSDefaultParameterValues = $originalDefaultParameterValues28 }29 30 It 'OutputRendering works for "<outputRendering>" to the host' -TestCases @(31 @{ outputRendering = 'host' ; ansi = $true }32 @{ outputRendering = 'ansi' ; ansi = $true }33 @{ outputRendering = 'plaintext'; ansi = $false }34 ) {35 param($outputRendering, $ansi)36 37 $out = pwsh -noprofile -command "`$PSStyle.OutputRendering = '$outputRendering'; write-host '$($PSStyle.Foreground.Green)hello'"38 39 if ($ansi) {40 $out | Should -BeLike "*`e*" -Because ($out | Format-Hex | Out-String)41 }42 else {43 $out | Should -Not -BeLike "*`e*" -Because ($out | Format-Hex | Out-String)44 }45 }46 47 It 'OutputRendering works for "<outputRendering>" to the pipeline' -TestCases @(48 @{ outputRendering = 'host' ; ansi = $false }49 @{ outputRendering = 'ansi' ; ansi = $true }50 @{ outputRendering = 'plaintext'; ansi = $false }51 ) {52 param($outputRendering, $ansi)53 54 $PSStyle.OutputRendering = $outputRendering55 $out = [pscustomobject] @{ key = "$($PSStyle.Foreground.Green)hello" } | Out-String56 57 if ($ansi) {58 $out | Should -BeLike "*`e*" -Because ($out | Format-Hex | Out-String)59 }60 else {61 $out | Should -Not -BeLike "*`e*" -Because ($out | Format-Hex | Out-String)62 }63 }64 65 # Error isn't covered here because it has custom formatting66 It 'OutputRendering is correct for <stream>' -TestCases @(67 @{ stream = 'Verbose' }68 @{ stream = 'Debug' }69 @{ stream = 'Warning' }70 ) {71 param($stream)72 73 if ($stream -ne 'Warning')74 {75 $switch = "-$stream"76 }77 78 $out = pwsh -noprofile -command "write-$stream $switch 'hello';'bye'"79 $out.Count | Should -Be 280 $out[0] | Should -BeExactly "$($PSStyle.Formatting.$stream)$($stream.ToUpper()): hello$($PSStyle.Reset)" -Because ($out[0] | Out-String | Format-hex)81 $out[1] | Should -BeExactly "bye"82 }83 84 It 'ToString(OutputRendering) works correctly' {85 $s = [System.Management.Automation.Internal.StringDecorated]::new($PSStyle.Foreground.Red + "Hello`e[m.")86 $s.IsDecorated | Should -BeTrue87 $s.ToString() | Should -BeExactly "$($PSStyle.Foreground.Red)Hello`e[m."88 $s.ToString([System.Management.Automation.OutputRendering]::ANSI) | Should -BeExactly "$($PSStyle.Foreground.Red)Hello`e[m."89 $s.ToString([System.Management.Automation.OutputRendering]::PlainText) | Should -BeExactly 'Hello.'90 { $s.ToString([System.Management.Automation.OutputRendering]::Host) } | Should -Throw -ErrorId 'ArgumentException'91 }92}93 