2020-03-24 11:08:37 -07:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2019-10-15 14:32:14 -07:00
|
|
|
# Licensed under the MIT License.
|
|
|
|
|
|
|
|
|
|
Describe 'Get-Error tests' -Tag CI {
|
2020-01-13 19:19:39 +00:00
|
|
|
It 'Get-Error resolves $error[0] and includes InnerException' {
|
2019-10-15 14:32:14 -07:00
|
|
|
try {
|
|
|
|
|
1/0
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$out = Get-Error | Out-String
|
|
|
|
|
$out | Should -BeLikeExactly '*InnerException*'
|
2019-11-18 19:54:58 -08:00
|
|
|
|
|
|
|
|
$err = Get-Error
|
2020-01-24 19:00:37 +00:00
|
|
|
$err | Should -BeOfType System.Management.Automation.ErrorRecord
|
2019-11-18 19:54:58 -08:00
|
|
|
$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
|
2020-01-24 19:00:37 +00:00
|
|
|
$err | Should -BeOfType System.Management.Automation.ErrorRecord
|
2019-11-18 19:54:58 -08:00
|
|
|
$err.PSObject.TypeNames | Should -Contain 'System.Management.Automation.ErrorRecord'
|
|
|
|
|
$err.PSObject.TypeNames | Should -Not -Contain 'System.Management.Automation.ErrorRecord#PSExtendedError'
|
2019-10-15 14:32:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2020-05-07 13:00:30 +01:00
|
|
|
Get-Item (New-Guid) -ErrorAction SilentlyContinue
|
2019-10-15 14:32:14 -07:00
|
|
|
}
|
|
|
|
|
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' {
|
2019-11-18 19:54:58 -08:00
|
|
|
$e = [Exception]::new('myexception')
|
|
|
|
|
$error.Insert(0, $e)
|
2019-10-15 14:32:14 -07:00
|
|
|
|
|
|
|
|
$out = Get-Error | Out-String
|
2019-11-18 19:54:58 -08:00
|
|
|
$out | Should -BeLikeExactly '*myexception*'
|
|
|
|
|
|
|
|
|
|
$err = Get-Error
|
2020-01-24 19:00:37 +00:00
|
|
|
$err | Should -BeOfType System.Exception
|
2019-11-18 19:54:58 -08:00
|
|
|
$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
|
2020-01-24 19:00:37 +00:00
|
|
|
$err | Should -BeOfType System.Exception
|
2019-11-18 19:54:58 -08:00
|
|
|
$err.PSObject.TypeNames | Should -Contain 'System.Exception'
|
|
|
|
|
$err.PSObject.TypeNames | Should -Not -Contain 'System.Exception#PSExtendedError'
|
2019-10-15 14:32:14 -07:00
|
|
|
}
|
2019-11-21 02:14:33 -08:00
|
|
|
|
|
|
|
|
It 'Get-Error will not modify the original error object' {
|
|
|
|
|
try {
|
|
|
|
|
1 / 0
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$null = Get-Error
|
|
|
|
|
|
2020-01-13 19:19:39 +00:00
|
|
|
$error[0].pstypenames | Should -Be System.Management.Automation.ErrorRecord, System.Object
|
2019-11-21 02:14:33 -08:00
|
|
|
}
|
2019-11-22 12:57:52 -08:00
|
|
|
|
|
|
|
|
It 'Get-Error adds ExceptionType for Exceptions' {
|
2022-06-03 09:25:24 -07:00
|
|
|
|
2019-11-22 12:57:52 -08:00
|
|
|
try {
|
2022-06-03 09:25:24 -07:00
|
|
|
throw [ArgumentException] 'myexception'
|
2019-11-22 12:57:52 -08:00
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$out = Get-Error | Out-String
|
2022-06-03 09:25:24 -07:00
|
|
|
$out | Should -Match "Type\s+:\s(.\[\d+m)?System.ArgumentException"
|
2019-11-22 12:57:52 -08:00
|
|
|
}
|
2021-07-16 03:15:16 -07:00
|
|
|
|
2021-08-09 14:01:57 -07:00
|
|
|
It 'Get-Error uses Error color for Message and PositionMessage members' {
|
2023-03-27 17:36:41 -07:00
|
|
|
|
|
|
|
|
if (-not $host.ui.SupportsVirtualTerminal) {
|
|
|
|
|
Set-ItResult -Skipped -Because 'Windows Server 2012 R2 does not support VT100 escape sequences'
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 03:15:16 -07:00
|
|
|
$suppressVT = $false
|
|
|
|
|
if (Test-Path env:/__SuppressAnsiEscapeSequences) {
|
|
|
|
|
$suppressVT = $true
|
|
|
|
|
$env:__SuppressAnsiEscapeSequences = $null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2021-08-16 14:17:07 -07:00
|
|
|
$originalRendering = $PSStyle.OutputRendering
|
|
|
|
|
$PSStyle.OutputRendering = 'Ansi'
|
2021-12-11 02:26:55 +00:00
|
|
|
$out = pwsh -noprofile -command '$PSStyle.OutputRendering = "ANSI"; try { 1/0 } catch { }; Get-Error' | Out-String
|
2021-07-16 03:15:16 -07:00
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
{
|
2021-08-16 14:17:07 -07:00
|
|
|
$PSStyle.OutputRendering = $originalRendering
|
2021-07-16 03:15:16 -07:00
|
|
|
if ($suppressVT) {
|
|
|
|
|
$env:__SuppressAnsiEscapeSequences = 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-13 05:00:20 -08:00
|
|
|
|
|
|
|
|
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."
|
|
|
|
|
}
|
2024-08-26 16:25:16 -07:00
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
}
|
2019-10-15 14:32:14 -07:00
|
|
|
}
|