2020-03-24 19:08:37 +01:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2019-10-05 01:52:57 +02:00
|
|
|
# Licensed under the MIT License.
|
|
|
|
|
|
|
|
Describe 'Tests for $ErrorView' -Tag CI {
|
|
|
|
|
|
|
|
It '$ErrorView is an enum' {
|
2020-01-24 20:00:37 +01:00
|
|
|
$ErrorView | Should -BeOfType System.Management.Automation.ErrorView
|
2019-10-05 01:52:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It '$ErrorView should have correct default value' {
|
2019-12-11 00:41:43 +01:00
|
|
|
$expectedDefault = 'ConciseView'
|
2019-10-05 01:52:57 +02:00
|
|
|
|
|
|
|
$ErrorView | Should -BeExactly $expectedDefault
|
|
|
|
}
|
|
|
|
|
2019-12-21 05:45:44 +01:00
|
|
|
It 'Exceptions not thrown do not get formatted as ErrorRecord' {
|
|
|
|
$exp = [System.Exception]::new('test') | Out-String
|
|
|
|
$exp | Should -BeLike "*Message : test*"
|
|
|
|
}
|
|
|
|
|
2019-10-05 01:52:57 +02:00
|
|
|
Context 'ConciseView tests' {
|
2020-01-30 20:03:39 +01:00
|
|
|
BeforeEach {
|
|
|
|
$testScriptPath = Join-Path -Path $TestDrive -ChildPath 'test.ps1'
|
|
|
|
}
|
|
|
|
|
|
|
|
AfterEach {
|
|
|
|
Remove-Item -Path $testScriptPath -Force -ErrorAction SilentlyContinue
|
|
|
|
}
|
2019-10-05 01:52:57 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
'@
|
|
|
|
|
|
|
|
Set-Content -Path $testScriptPath -Value $testScript
|
2020-01-30 20:03:39 +01:00
|
|
|
$e = { & $testScriptPath } | Should -Throw -ErrorId 'UnexpectedToken' -PassThru | Out-String
|
|
|
|
$e | Should -BeLike "*${testScriptPath}:4*"
|
2019-10-05 01:52:57 +02:00
|
|
|
# validate line number is shown
|
2020-01-30 20:03:39 +01:00
|
|
|
$e | Should -BeLike '* 4 *'
|
2019-10-05 01:52:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Remote errors show up correctly" {
|
2020-05-07 14:00:30 +02:00
|
|
|
Start-Job -ScriptBlock { Get-Item (New-Guid) } | Wait-Job | Receive-Job -ErrorVariable e -ErrorAction SilentlyContinue
|
2019-10-05 01:52:57 +02:00
|
|
|
($e | Out-String).Trim().Count | Should -Be 1
|
|
|
|
}
|
2019-11-10 12:21:10 +01:00
|
|
|
|
|
|
|
It "Activity shows up correctly for scriptblocks" {
|
2020-01-05 08:17:41 +01:00
|
|
|
$e = & "$PSHOME/pwsh" -noprofile -command 'Write-Error 'myError' -ErrorAction SilentlyContinue; $error[0] | Out-String'
|
2019-11-10 12:21:10 +01:00
|
|
|
[string]::Join('', $e).Trim() | Should -BeLike "*Write-Error:*myError*" # wildcard due to VT100
|
|
|
|
}
|
|
|
|
|
|
|
|
It "Function shows up correctly" {
|
2020-05-07 14:00:30 +02:00
|
|
|
function test-myerror { [cmdletbinding()] param() Write-Error 'myError' }
|
2019-11-10 12:21:10 +01:00
|
|
|
|
2020-01-05 08:17:41 +01:00
|
|
|
$e = & "$PSHOME/pwsh" -noprofile -command 'function test-myerror { [cmdletbinding()] param() write-error "myError" }; test-myerror -ErrorAction SilentlyContinue; $error[0] | Out-String'
|
2019-11-10 12:21:10 +01:00
|
|
|
[string]::Join('', $e).Trim() | Should -BeLike "*test-myerror:*myError*" # wildcard due to VT100
|
|
|
|
}
|
2019-12-10 18:50:51 +01:00
|
|
|
|
|
|
|
It "Pester Should shows test file and not pester" {
|
|
|
|
$testScript = '1 + 1 | Should -Be 3'
|
2020-01-30 20:03:39 +01:00
|
|
|
|
2019-12-10 18:50:51 +01:00
|
|
|
Set-Content -Path $testScriptPath -Value $testScript
|
2020-01-30 20:03:39 +01:00
|
|
|
$e = { & $testScriptPath } | Should -Throw -ErrorId 'PesterAssertionFailed' -PassThru | Out-String
|
|
|
|
$e | Should -BeLike "*$testScriptPath*"
|
|
|
|
$e | Should -Not -BeLike '*pester*'
|
|
|
|
}
|
|
|
|
|
|
|
|
It "Long lines should be rendered correctly with indentation" {
|
|
|
|
$testscript = @'
|
|
|
|
$myerrors = [System.Collections.ArrayList]::new()
|
|
|
|
Copy-Item (New-Guid) (New-Guid) -ErrorVariable +myerrors -ErrorAction SilentlyContinue
|
|
|
|
$error[0]
|
|
|
|
'@
|
|
|
|
|
|
|
|
Set-Content -Path $testScriptPath -Value $testScript
|
|
|
|
$e = & $testScriptPath | Out-String
|
|
|
|
$e | Should -BeLike "*${testScriptPath}:2*"
|
|
|
|
# validate line number is shown
|
|
|
|
$e | Should -BeLike '* 2 *'
|
2019-12-10 18:50:51 +01:00
|
|
|
}
|
2020-02-20 04:38:28 +01:00
|
|
|
|
|
|
|
It "Long exception message gets rendered" {
|
|
|
|
|
|
|
|
$msg = "1234567890"
|
|
|
|
while ($msg.Length -le $Host.UI.RawUI.WindowSize.Width)
|
|
|
|
{
|
|
|
|
$msg += $msg
|
|
|
|
}
|
|
|
|
|
|
|
|
$e = { throw "$msg" } | Should -Throw $msg -PassThru | Out-String
|
|
|
|
$e | Should -BeLike "*$msg*"
|
|
|
|
}
|
2020-02-26 17:07:57 +01:00
|
|
|
|
|
|
|
It "Position message does not contain line information" {
|
|
|
|
|
|
|
|
$e = & "$PSHOME/pwsh" -noprofile -command "foreach abc" | Out-String
|
|
|
|
$e | Should -Not -BeLike "*At line*"
|
|
|
|
}
|
2019-10-05 01:52:57 +02:00
|
|
|
}
|
2019-12-17 19:43:47 +01:00
|
|
|
|
|
|
|
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!*'
|
|
|
|
}
|
|
|
|
}
|
2019-10-05 01:52:57 +02:00
|
|
|
}
|