Private/Tests/Get-ErrorInfo.Tests.ps1

<#
 
# Get-ErrorInfo (UTest)
 
- **Hashtags** UTest Pester
- **Version** 2020.12.21
 
#>


BeforeAll {
    . ($PSCommandPath.Replace('.Tests.ps1', '.ps1')).Replace('\Private\Tests\', '\Public\')
}

AfterAll {
    "<[ START SCRIPT-ANALYZER ..." | Write-Warning
    Invoke-ScriptAnalyzer -Path ($PSCommandPath.Replace('.Tests.ps1', '.ps1')).Replace('\Private\Tests\', '\Public\') -Severity Error, Warning, Information, ParseError | ForEach-Object -Process {"{0,40} - {1,4} - {2}" -f $_.ScriptName, $_.Line, $_.RuleName, $_.Message | Write-Warning }
    "... ENDE SCRIPT-ANALYZER ]>" | Write-Warning
}

# TODO: $e = $Error[0] ; Get-ErrorInfo -InputObject $e
# TODO: $Error | Get-ErrorInfo

Describe "Get-ErrorInfo Test" {
    Context "Parameter-Test" {
        It "Parameter -InputObject ist vom Typ System.Object" {
            Get-Command -Name Get-ErrorInfo | Should -HaveParameter InputObject -Type 'Object'
        }
        It "Parameter -CmdletName validiert mit ValidateNotNullOrEmpty" {
            $target = (Get-Command -Name Get-ErrorInfo).Parameters['InputObject']
            $target = $target.Attributes.Where({$_ -is [System.Management.Automation.ValidateNotNullOrEmptyAttribute]})
            $target | should -Not -BeNullOrEmpty
        }
        It "Parameter -CmdletName lässt die Pipeline-Verarbeitung nach ByValue zu." {
            $target = (Get-Command -Name Get-ErrorInfo).Parameters['InputObject']
            $target = $target.Attributes.Where({$_ -is [System.Management.Automation.ParameterAttribute]})
            $target.ValueFromPipeline | should -BeTrue
        }
        It "Parameter -CmdletName lässt die Pipeline-Verarbeitung nach ByPropertyName NICHT zu." {
            $target = (Get-Command -Name Get-ErrorInfo).Parameters['InputObject']
            $target = $target.Attributes.Where({$_ -is [System.Management.Automation.ParameterAttribute]})
            $target.ValueFromPipelineByPropertyName | should -BeFalse
        }
        It "Throw => Get-ErrorInfo -InputObject $null" {
            { Get-ErrorInfo -InputObject $null } | Should -Throw -ErrorId 'ParameterArgumentValidationError,Get-ErrorInfo'
        }
        It "Throw => Get-ErrorInfo -InputObject ''" {
            { Get-ErrorInfo -InputObject '' } | Should -Throw -ErrorId 'ParameterArgumentValidationError,Get-ErrorInfo'
        }
        It "Throw => Get-ErrorInfo -InputObject 'NichtErrorTyp'" {
            { Get-ErrorInfo -InputObject 'NichtErrorTyp' } | Should -Throw -ErrorId 'ParameterArgumentValidationError,Get-ErrorInfo'
        }
    }
    Context "Diverse ErrorRecords analysieren" {
        It "Korrekte Fehler-Analyse für: Get-ChildItem -Path C:\MichGibtEsNicht" {
            $Error.Clear()
            Get-ChildItem -Path C:\MichGibtEsNicht -ErrorAction SilentlyContinue -ErrorVariable gciError | Out-Null
            $gciError | Get-ErrorInfo | Out-String | Select-String -Pattern @"
Analyse Error 1[!p
    Exception : System.Management.Automation.ItemNotFoundException
    Exception.Message : Der Pfad "C:\MichGibtEsNicht" kann nicht gefunden werden, da er nicht vorhanden ist.
    FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
    CategoryInfo : Category: ObjectNotFound Activity: Get-ChildItem Reason: ItemNotFoundException TargetName: C:\MichGibtEsNicht TargetType: String
        InnerException : NULL
"@
 -SimpleMatch | should -HaveCount 1
        }
        It "Korrekte Fehler-Analyse für: Get-Process -FileVersionInfo -ErrorAction Stop" {
            $Error.Clear()
            try {
                Get-Process -FileVersionInfo -ErrorAction Stop | Out-Null
            }
            catch { }
            Get-ErrorInfo | Out-String | Select-String -Pattern @"
Analyse Error 1[!p
    Exception : Microsoft.PowerShell.Commands.ProcessCommandException
    Exception.Message : Die Dateiversionsinformationen des Prozesses "armsvc" können nicht aufgezählt werden.
    FullyQualifiedErrorId : CouldnotEnumerateFileVer,Microsoft.PowerShell.Commands.GetProcessCommand
    CategoryInfo : Category: PermissionDenied Activity: Get-Process Reason: ProcessCommandException TargetName: System.Diagnostics.Process (armsvc) TargetType: Process
        InnerException : System.ComponentModel.Win32Exception
                            Zugriff verweigert
            InnerException: NULL
"@
 -SimpleMatch | should -HaveCount 1
        }
        It "Korrekte Fehler-Analyse für: Get-Process -FileVersionInfo -ErrorAction Stop" {
            $Error.Clear()
            try {
                1/0
            }
            catch { }
            Get-ErrorInfo | Out-String | Select-String -Pattern @"
Analyse Error 1[!p
    Exception : System.Management.Automation.RuntimeException
    Exception.Message : Es wurde versucht, durch 0 (null) zu teilen.
    FullyQualifiedErrorId : RuntimeException
    CategoryInfo : Category: NotSpecified Activity:  Reason: RuntimeException TargetName:  TargetType: 
        InnerException : System.DivideByZeroException
                            Es wurde versucht, durch 0 (null) zu teilen.
            InnerException: NULL
"@
 -SimpleMatch | should -HaveCount 1
        }
        It "Korrekte Fehler-Analyse für: Get-ChildItem -Path ./MichGibtEsNicht" {
            $Error.Clear()
            try {
                Invoke-WebRequest -Uri 'https://www.bing.com/NoRealUri.aspx' -ErrorAction SilentlyContinue | Out-Null
            }
            catch { }

            Get-ErrorInfo | Out-String | Select-String -Pattern @"
Analyse Error 1[!p
    Exception : System.Net.WebException
    Exception.Message : Der Remoteserver hat einen Fehler zurückgegeben: (404) Nicht gefunden.
    FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
    CategoryInfo : Category: InvalidOperation Activity: Invoke-WebRequest Reason: WebException TargetName: System.Net.HttpWebRequest TargetType: HttpWebRequest
        InnerException : NULL
"@
 -SimpleMatch | should -HaveCount 1
        }
    }
}