Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
Get-Error.Tests.ps1173 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe 'Get-Error tests' -Tag CI {5    It 'Get-Error resolves $error[0] and includes InnerException' {6        try {7            1/08        }9        catch {10        }11 12        $out = Get-Error | Out-String13        $out | Should -BeLikeExactly '*InnerException*'14 15        $err = Get-Error16        $err | Should -BeOfType System.Management.Automation.ErrorRecord17        $err.PSObject.TypeNames | Should -Not -Contain 'System.Management.Automation.ErrorRecord'18        $err.PSObject.TypeNames | Should -Contain 'System.Management.Automation.ErrorRecord#PSExtendedError'19 20        # need to exercise the formatter to validate that the internal types are removed from the error object21        $null = $err | Out-String22        $err | Should -BeOfType System.Management.Automation.ErrorRecord23        $err.PSObject.TypeNames | Should -Contain 'System.Management.Automation.ErrorRecord'24        $err.PSObject.TypeNames | Should -Not -Contain 'System.Management.Automation.ErrorRecord#PSExtendedError'25    }26 27    It 'Get-Error -Newest `<count>` works: <scenario>' -TestCases @(28        @{ scenario = 'less than total'; count = 1; paramname = 'Newest' }29        @{ scenario = 'equal to total'; count = 2; paramname = 'Last' }30        @{ scenario = 'greater than total'; count = 9999; paramname = 'Newest' }31    ){32        param ($count, $paramname)33 34        try {35            1/036        }37        catch {38        }39 40        try {41            Get-Item (New-Guid) -ErrorAction SilentlyContinue42        }43        catch {44        }45 46        $params = @{ $paramname = $count }47 48        $out = Get-Error @params49 50        $expected = $count51        if ($count -eq 9999) {52            $expected = $error.Count53        }54 55        $out.Count | Should -Be $expected56    }57 58    It 'Get-Error -Newest with invalid value `<value>` should fail' -TestCases @(59        @{ value = 0 }60        @{ value = -2 }61    ){62        param($value)63 64        { Get-Error -Newest $value } | Should -Throw -ErrorId 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetErrorCommand'65    }66 67    It 'Get-Error will accept pipeline input' {68        try {69            1/070        }71        catch {72        }73 74        $out = $error[0] | Get-Error | Out-String75        $out | Should -BeLikeExactly '*-2146233087*'76    }77 78    It 'Get-Error will handle Exceptions' {79        $e = [Exception]::new('myexception')80        $error.Insert(0, $e)81 82        $out = Get-Error | Out-String83        $out | Should -BeLikeExactly '*myexception*'84 85        $err = Get-Error86        $err | Should -BeOfType System.Exception87        $err.PSObject.TypeNames | Should -Not -Contain 'System.Exception'88        $err.PSObject.TypeNames | Should -Contain 'System.Exception#PSExtendedError'89 90        # need to exercise the formatter to validate that the internal types are removed from the error object91        $null = $err | Out-String92        $err | Should -BeOfType System.Exception93        $err.PSObject.TypeNames | Should -Contain 'System.Exception'94        $err.PSObject.TypeNames | Should -Not -Contain 'System.Exception#PSExtendedError'95    }96 97    It 'Get-Error will not modify the original error object' {98        try {99            1 / 0100        }101        catch {102        }103 104        $null = Get-Error105 106        $error[0].pstypenames | Should -Be System.Management.Automation.ErrorRecord, System.Object107    }108 109    It 'Get-Error adds ExceptionType for Exceptions' {110 111        try {112            throw [ArgumentException] 'myexception'113        }114        catch {115        }116 117        $out = Get-Error | Out-String118        $out | Should -Match "Type\s+:\s(.\[\d+m)?System.ArgumentException"119    }120 121    It 'Get-Error uses Error color for Message and PositionMessage members' {122 123        if (-not $host.ui.SupportsVirtualTerminal) {124            Set-ItResult -Skipped -Because 'Windows Server 2012 R2 does not support VT100 escape sequences'125        }126 127        $suppressVT = $false128        if (Test-Path env:/__SuppressAnsiEscapeSequences) {129            $suppressVT = $true130            $env:__SuppressAnsiEscapeSequences = $null131        }132 133        try {134            $originalRendering = $PSStyle.OutputRendering135            $PSStyle.OutputRendering = 'Ansi'136            $out = pwsh -noprofile -command '$PSStyle.OutputRendering = "ANSI"; try { 1/0 } catch { }; Get-Error' | Out-String137 138            # need to escape the open square bracket so the regex works139            $resetColor = $PSStyle.Reset.Replace('[','\[')140            $errorColor = $PSStyle.Formatting.Error.Replace('[','\[')141            $out | Should -Match "Message +: ${resetColor}${errorColor}[A-z]+"142            # match the position message underline143            $out | Should -Match ".*?${errorColor}~~~"144        }145        finally146        {147            $PSStyle.OutputRendering = $originalRendering148            if ($suppressVT) {149                $env:__SuppressAnsiEscapeSequences = 1150            }151        }152    }153 154    It 'Get-Error works with strict mode' {155        $out = pwsh -noprofile -command 'Set-StrictMode -Version Latest; $PSStyle.OutputRendering = "PlainText"; 1/0; Get-Error' | Out-String156        $out | Should -Match "Message : Attempted to divide by zero."157    }158 159    It 'BoundParameters show as name/value pairs' {160        try {161            function test { [CmdletBinding()]param($A,$B) }162            $Param = @{ A = "First"; B = "Second"; C = 24 }163            test @Param164        }165        catch {166            # do nothing167        }168 169        $out = Get-Error | Out-String170        $out | Should -Match "BoundParameters\s+:\s+A : First\s+B : Second\s"171    }172}173