File size: 5,656 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe 'Get-Error tests' -Tag CI {
    It 'Get-Error resolves $error[0] and includes InnerException' {
        try {
            1/0
        }
        catch {
        }

        $out = Get-Error | Out-String
        $out | Should -BeLikeExactly '*InnerException*'

        $err = Get-Error
        $err | Should -BeOfType System.Management.Automation.ErrorRecord
        $err.PSObject.TypeNames | Should -Not -Contain 'System.Management.Automation.ErrorRecord'
        $err.PSObject.TypeNames | Should -Contain 'System.Management.Automation.ErrorRecord#PSExtendedError'

        # need to exercise the formatter to validate that the internal types are removed from the error object
        $null = $err | Out-String
        $err | Should -BeOfType System.Management.Automation.ErrorRecord
        $err.PSObject.TypeNames | Should -Contain 'System.Management.Automation.ErrorRecord'
        $err.PSObject.TypeNames | Should -Not -Contain 'System.Management.Automation.ErrorRecord#PSExtendedError'
    }

    It 'Get-Error -Newest `<count>` works: <scenario>' -TestCases @(
        @{ scenario = 'less than total'; count = 1; paramname = 'Newest' }
        @{ scenario = 'equal to total'; count = 2; paramname = 'Last' }
        @{ scenario = 'greater than total'; count = 9999; paramname = 'Newest' }
    ){
        param ($count, $paramname)

        try {
            1/0
        }
        catch {
        }

        try {
            Get-Item (New-Guid) -ErrorAction SilentlyContinue
        }
        catch {
        }

        $params = @{ $paramname = $count }

        $out = Get-Error @params

        $expected = $count
        if ($count -eq 9999) {
            $expected = $error.Count
        }

        $out.Count | Should -Be $expected
    }

    It 'Get-Error -Newest with invalid value `<value>` should fail' -TestCases @(
        @{ value = 0 }
        @{ value = -2 }
    ){
        param($value)

        { Get-Error -Newest $value } | Should -Throw -ErrorId 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetErrorCommand'
    }

    It 'Get-Error will accept pipeline input' {
        try {
            1/0
        }
        catch {
        }

        $out = $error[0] | Get-Error | Out-String
        $out | Should -BeLikeExactly '*-2146233087*'
    }

    It 'Get-Error will handle Exceptions' {
        $e = [Exception]::new('myexception')
        $error.Insert(0, $e)

        $out = Get-Error | Out-String
        $out | Should -BeLikeExactly '*myexception*'

        $err = Get-Error
        $err | Should -BeOfType System.Exception
        $err.PSObject.TypeNames | Should -Not -Contain 'System.Exception'
        $err.PSObject.TypeNames | Should -Contain 'System.Exception#PSExtendedError'

        # need to exercise the formatter to validate that the internal types are removed from the error object
        $null = $err | Out-String
        $err | Should -BeOfType System.Exception
        $err.PSObject.TypeNames | Should -Contain 'System.Exception'
        $err.PSObject.TypeNames | Should -Not -Contain 'System.Exception#PSExtendedError'
    }

    It 'Get-Error will not modify the original error object' {
        try {
            1 / 0
        }
        catch {
        }

        $null = Get-Error

        $error[0].pstypenames | Should -Be System.Management.Automation.ErrorRecord, System.Object
    }

    It 'Get-Error adds ExceptionType for Exceptions' {

        try {
            throw [ArgumentException] 'myexception'
        }
        catch {
        }

        $out = Get-Error | Out-String
        $out | Should -Match "Type\s+:\s(.\[\d+m)?System.ArgumentException"
    }

    It 'Get-Error uses Error color for Message and PositionMessage members' {

        if (-not $host.ui.SupportsVirtualTerminal) {
            Set-ItResult -Skipped -Because 'Windows Server 2012 R2 does not support VT100 escape sequences'
        }

        $suppressVT = $false
        if (Test-Path env:/__SuppressAnsiEscapeSequences) {
            $suppressVT = $true
            $env:__SuppressAnsiEscapeSequences = $null
        }

        try {
            $originalRendering = $PSStyle.OutputRendering
            $PSStyle.OutputRendering = 'Ansi'
            $out = pwsh -noprofile -command '$PSStyle.OutputRendering = "ANSI"; try { 1/0 } catch { }; Get-Error' | Out-String

            # need to escape the open square bracket so the regex works
            $resetColor = $PSStyle.Reset.Replace('[','\[')
            $errorColor = $PSStyle.Formatting.Error.Replace('[','\[')
            $out | Should -Match "Message +: ${resetColor}${errorColor}[A-z]+"
            # match the position message underline
            $out | Should -Match ".*?${errorColor}~~~"
        }
        finally
        {
            $PSStyle.OutputRendering = $originalRendering
            if ($suppressVT) {
                $env:__SuppressAnsiEscapeSequences = 1
            }
        }
    }

    It 'Get-Error works with strict mode' {
        $out = pwsh -noprofile -command 'Set-StrictMode -Version Latest; $PSStyle.OutputRendering = "PlainText"; 1/0; Get-Error' | Out-String
        $out | Should -Match "Message : Attempted to divide by zero."
    }

    It 'BoundParameters show as name/value pairs' {
        try {
            function test { [CmdletBinding()]param($A,$B) }
            $Param = @{ A = "First"; B = "Second"; C = 24 }
            test @Param
        }
        catch {
            # do nothing
        }

        $out = Get-Error | Out-String
        $out | Should -Match "BoundParameters\s+:\s+A : First\s+B : Second\s"
    }
}