Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Write-Error.Tests.ps1141 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Write-Error Tests" -Tags "CI" {4    It "Should be works with command: write-error myerrortext" {5        $e = Write-Error myerrortext 2>&16        $e | Should -BeOfType System.Management.Automation.ErrorRecord7 8        #Exception verification9        $e.Exception | Should -BeOfType Microsoft.PowerShell.Commands.WriteErrorException10        $e.Exception.Message | Should -Be 'myerrortext'11        $e.Exception.Data.Count | Should -Be 012        $e.Exception.InnerException | Should -BeNullOrEmpty13 14        #ErrorCategoryInfo verification15        $e.CategoryInfo | Should -Not -BeNullOrEmpty16        $e.CategoryInfo.Category | Should -Be 'NotSpecified'17        $e.CategoryInfo.Activity | Should -Be 'Write-Error'18        $e.CategoryInfo.Reason | Should -Be 'WriteErrorException'19        $e.CategoryInfo.TargetName | Should -BeNullOrEmpty20        $e.CategoryInfo.TargetType | Should -BeNullOrEmpty21        $e.CategoryInfo.GetMessage() | Should -Be 'NotSpecified: (:) [Write-Error], WriteErrorException'22 23        #ErrorDetails verification24        $e.ErrorDetails | Should -BeNullOrEmpty25 26        #FullyQualifiedErrorId verification27        $e.FullyQualifiedErrorId | Should -BeExactly 'Microsoft.PowerShell.Commands.WriteErrorException'28 29        #InvocationInfo verification30        $e.InvocationInfo | Should -Not -BeNullOrEmpty31        $e.InvocationInfo.MyCommand.Name | Should -BeNullOrEmpty32    }33 34    It "Should be works with all parameters" {35        $exception = New-Object -TypeName System.ArgumentNullException -ArgumentList paramname36        $e = Write-Error -Message myerrortext -Exception $exception -ErrorId myerrorid -Category syntaxerror -TargetObject TargetObject -CategoryActivity myactivity -CategoryReason myreason -CategoryTargetName mytargetname -CategoryTargetType mytargettype -RecommendedAction myrecommendedaction 2>&137        $e | Should -Not -BeNullOrEmpty38        $e | Should -BeOfType System.Management.Automation.ErrorRecord39 40        #Exception verification41        $e.Exception | Should -BeOfType System.ArgumentNullException42        $e.Exception.ParamName | Should -Be 'paramname'43        $e.Exception.Data.Count | Should -Be 044        $e.Exception.InnerException | Should -BeNullOrEmpty45 46        #TargetObject verification47        $e.TargetObject | Should -Be 'TargetObject'48 49        #FullyQualifiedErrorId verification50        $e.FullyQualifiedErrorId | Should -BeExactly 'myerrorid'51 52        #ErrorCategoryInfo verification53        $e.CategoryInfo | Should -Not -BeNullOrEmpty54        $e.CategoryInfo.Category | Should -Be 'SyntaxError'55        $e.CategoryInfo.Activity | Should -Be 'myactivity'56        $e.CategoryInfo.Reason | Should -Be 'myreason'57        $e.CategoryInfo.TargetName | Should -Be 'mytargetname'58        $e.CategoryInfo.TargetType | Should -Be 'mytargettype'59        $e.CategoryInfo.GetMessage() | Should -Be 'SyntaxError: (mytargetname:mytargettype) [myactivity], myreason'60 61        #ErrorDetails verification62        $e.ErrorDetails | Should -Not -BeNullOrEmpty63        $e.ErrorDetails.Message | Should -Be 'myerrortext'64        $e.ErrorDetails.RecommendedAction | Should -Be 'myrecommendedaction'65 66        #InvocationInfo verification67        $e.InvocationInfo | Should -Not -BeNullOrEmpty68        $e.InvocationInfo.MyCommand.Name | Should -BeNullOrEmpty69    }70 71    It "Should be works with all parameters" {72        $e = Write-Error -Activity fooAct -Reason fooReason -TargetName fooTargetName -TargetType fooTargetType -Message fooMessage 2>&173        $e.CategoryInfo.Activity | Should -Be 'fooAct'74        $e.CategoryInfo.Reason | Should -Be 'fooReason'75        $e.CategoryInfo.TargetName | Should -Be 'fooTargetName'76        $e.CategoryInfo.TargetType | Should -Be 'fooTargetType'77        $e.CategoryInfo.GetMessage() | Should -Be 'NotSpecified: (fooTargetName:fooTargetType) [fooAct], fooReason'78    }79 80    It "Should be able to throw with -ErrorAction stop" {81    	{ Write-Error "test throw" -ErrorAction Stop } | Should -Throw82    }83 84    It "Should throw a non-terminating error" {85        { Write-Error "test throw" -ErrorAction SilentlyContinue } | Should -Not -Throw86    }87 88    It "Should trip an exception using the exception switch" {89        { Write-Error -Exception -Message "test throw" } | Should -Throw -ErrorId "MissingArgument,Microsoft.PowerShell.Commands.WriteErrorCommand"90    }91 92    It "Should output the error message to the `$error automatic variable" {93        $theError = "Error: Too many input values."94        Write-Error -Message $theError -Category InvalidArgument -ErrorAction SilentlyContinue95 96        [string]$error[0] | Should -Be $theError97    }98 99    It "ErrorRecord should not be truncated or have inserted newlines when redirected from another process" {100        $longtext = "0123456789"101        try{102            while ($longtext.Length -lt [console]::WindowWidth) {103                $longtext += $longtext104            }105        } catch {106            # Ignore if the console is doesn't support WindowWidth107        }108        $PSNativeCommandUseErrorActionPreference = $false109        $result = & "$PSHOME/pwsh" -noprofile -command "`$ErrorView = 'NormalView'; Write-Error -Message '$longtext'" 2>&1110        $result.Count | Should -BeExactly 3 -Because ($result | Out-String)111        $result[0] | Should -Match $longtext112    }113 114    It "Should be able to pass ErrorRecord to parameter position 0." {115        try {116            [int]::parse('foo')117        } catch { }118 119        (Write-Error $Error[0] 2>&1).Exception.GetType().FullName |120            Should -Be System.Management.Automation.MethodInvocationException121    }122 123    It "Should be able to pass Exception to parameter position 0." {124        try {125            [int]::parse('foo')126        } catch { }127 128        (Write-Error $Error[0].Exception.InnerException 2>&1).Exception.GetType().FullName |129            Should -Be System.FormatException130    }131 132    It "Should be able to pass string to parameter position 0." {133        try {134            [int]::parse('foo')135        } catch { }136 137        (Write-Error "$($Error[0])" 2>&1).Exception.GetType().FullName |138            Should -Be Microsoft.PowerShell.Commands.WriteErrorException139    }140}141