86 lines
3.1 KiB
PowerShell
86 lines
3.1 KiB
PowerShell
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
# Licensed under the MIT License.
|
|
|
|
Describe 'Tests for $ErrorView' -Tag CI {
|
|
|
|
It '$ErrorView is an enum' {
|
|
$ErrorView | Should -BeOfType [System.Management.Automation.ErrorView]
|
|
}
|
|
|
|
It '$ErrorView should have correct default value' {
|
|
$expectedDefault = 'ConciseView'
|
|
|
|
$ErrorView | Should -BeExactly $expectedDefault
|
|
}
|
|
|
|
Context 'ConciseView tests' {
|
|
|
|
It 'Cmdlet error should be one line of text' {
|
|
Get-Item (New-Guid) -ErrorVariable e -ErrorAction SilentlyContinue
|
|
($e | Out-String).Trim().Count | Should -Be 1
|
|
}
|
|
|
|
It 'Script error should contain path to script and line for error' {
|
|
$testScript = @'
|
|
[cmdletbinding()]
|
|
param()
|
|
$a = 1
|
|
123)
|
|
$b = 2
|
|
'@
|
|
|
|
$testScriptPath = Join-Path -Path $TestDrive -ChildPath 'test.ps1'
|
|
Set-Content -Path $testScriptPath -Value $testScript
|
|
$e = { & $testScriptPath } | Should -Throw -ErrorId 'UnexpectedToken' -PassThru
|
|
$e | Out-String | Should -BeLike "*${testScriptPath}:4*"
|
|
# validate line number is shown
|
|
$e | Out-String | Should -BeLike '* 4 *'
|
|
}
|
|
|
|
It "Remote errors show up correctly" {
|
|
Start-Job -ScriptBlock { get-item (new-guid) } | Wait-Job | Receive-Job -ErrorVariable e -ErrorAction SilentlyContinue
|
|
($e | Out-String).Trim().Count | Should -Be 1
|
|
}
|
|
|
|
It "Activity shows up correctly for scriptblocks" {
|
|
$e = pwsh -noprofile -command 'Write-Error 'myError' -ErrorAction SilentlyContinue; $error[0] | Out-String'
|
|
[string]::Join('', $e).Trim() | Should -BeLike "*Write-Error:*myError*" # wildcard due to VT100
|
|
}
|
|
|
|
It "Function shows up correctly" {
|
|
function test-myerror { [cmdletbinding()] param() write-error 'myError' }
|
|
|
|
$e = pwsh -noprofile -command 'function test-myerror { [cmdletbinding()] param() write-error "myError" }; test-myerror -ErrorAction SilentlyContinue; $error[0] | Out-String'
|
|
[string]::Join('', $e).Trim() | Should -BeLike "*test-myerror:*myError*" # wildcard due to VT100
|
|
}
|
|
|
|
It "Pester Should shows test file and not pester" {
|
|
$testScript = '1 + 1 | Should -Be 3'
|
|
$testScriptPath = Join-Path -Path $TestDrive -ChildPath 'mytest.ps1'
|
|
Set-Content -Path $testScriptPath -Value $testScript
|
|
$e = { & $testScriptPath } | Should -Throw -ErrorId 'PesterAssertionFailed' -PassThru
|
|
$e | Out-String | Should -BeLike "*$testScriptPath*"
|
|
$e | Out-String | Should -Not -BeLike '*pester*'
|
|
}
|
|
}
|
|
|
|
Context 'NormalView tests' {
|
|
|
|
It 'Error shows up when using strict mode' {
|
|
try {
|
|
$ErrorView = 'NormalView'
|
|
Set-StrictMode -Version 2
|
|
throw 'Oops!'
|
|
}
|
|
catch {
|
|
$e = $_ | Out-String
|
|
}
|
|
finally {
|
|
Set-StrictMode -Off
|
|
}
|
|
|
|
$e | Should -BeLike '*Oops!*'
|
|
}
|
|
}
|
|
}
|