File size: 23,077 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 | # Copyright (c) Microsoft Corporation.
# 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
}
It 'Exceptions not thrown do not get formatted as ErrorRecord' {
$exp = [System.Exception]::new('test') | Out-String
$exp | Should -BeLike '*Message : *test*'
}
Context 'ConciseView tests' {
BeforeEach {
$testScriptPath = Join-Path -Path $TestDrive -ChildPath 'test.ps1'
$testModulePath = Join-Path -Path $TestDrive -ChildPath 'test.psm1'
}
AfterEach {
Remove-Item -Path $testScriptPath -Force -ErrorAction SilentlyContinue
}
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
$e = { & $testScriptPath } | Should -Throw -ErrorId 'UnexpectedToken' -PassThru | Out-String
$e | Should -BeLike "*${testScriptPath}:4*"
# validate line number is shown
$e | 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 = & "$PSHOME/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 = & "$PSHOME/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'
Set-Content -Path $testScriptPath -Value $testScript
$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 *'
}
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*"
}
It 'Position message does not contain line information' {
$e = & "$PSHOME/pwsh" -noprofile -command 'foreach abc' 2>&1 | Out-String
$e | Should -Not -BeNullOrEmpty
$e | Should -Not -BeLike '*At line*'
}
It "Error shows if `$PSModuleAutoLoadingPreference is set to 'none'" {
$e = & "$PSHOME/pwsh" -noprofile -command '$PSModuleAutoLoadingPreference = "none"; cmdletThatDoesntExist' 2>&1 | Out-String
$e | Should -BeLike '*cmdletThatDoesntExist*'
}
It 'Error shows for advanced function' {
# need to have it virtually interactive so that InvocationInfo.MyCommand is empty
$e = '[cmdletbinding()]param()$pscmdlet.writeerror([System.Management.Automation.ErrorRecord]::new(([System.NotImplementedException]::new("myTest")),"stub","notimplemented","command"))' | pwsh -noprofile -file - 2>&1
$e = $e | Where-Object { $_ -is [System.Management.Automation.ErrorRecord] } | Out-String
$e | Should -Not -BeNullOrEmpty
# need to see if ANSI escape sequences are in the output as ANSI is disabled for CI
if ($e.Contains("`e")) {
$e | Should -BeLike "*: `e*myTest*"
} else {
$e | Should -BeLike '*: myTest*'
}
}
It "Error containing '<type>' are rendered correctly for scripts" -TestCases @(
@{ type = 'CRLF'; newline = "`r`n" }
@{ type = 'LF' ; newline = "`n" }
) {
param($newline)
Set-Content -Path $testScriptPath -Value "throw 'hello${newline}there'"
$e = & "$PSHOME/pwsh" -noprofile -file $testScriptPath 2>&1 | Out-String
$e.Split("o${newline}t").Count | Should -Be 1 -Because 'Error message should not contain newline'
}
It 'Script module error should not show line information' {
$testModule = @'
function Invoke-Error() {
throw 'oops'
}
'@
Set-Content -Path $testModulePath -Value $testModule
$e = & "$PSHOME/pwsh" -noprofile -command "Import-Module '$testModulePath'; Invoke-Error" 2>&1 | Out-String
$e | Should -Not -BeNullOrEmpty
$e | Should -Not -BeLike '*Line*'
}
It 'Parser error shows line information' {
$testScript = '$psstyle.outputrendering = "plaintext"; 1 ++ 1'
$e = & "$PSHOME/pwsh" -noprofile -command $testScript 2>&1 | Out-String
$e | Should -Not -BeNullOrEmpty
$e = $e.Split([Environment]::NewLine)
$e[0] | Should -BeLike 'ParserError:*'
$e[1] | Should -BeLike 'Line *' -Because ($e | Out-String)
$e[2] | Should -BeLike '*|*1 ++ 1*'
}
It 'Faux remote parser error shows concise message' {
Start-Job { [cmdletbinding()]param() $e = [System.Management.Automation.ErrorRecord]::new([System.Exception]::new('hello'), 1, 'ParserError', $null); $pscmdlet.ThrowTerminatingError($e) } | Wait-Job | Receive-Job -ErrorVariable e -ErrorAction SilentlyContinue
$e | Out-String | Should -BeLike '*ParserError*'
}
It 'Parser TargetObject shows Line information' {
$expected = (@(
": "
"Line |"
" 1 | This is the line with the error"
" | Test Parser Error"
) -join ([Environment]::NewLine)).TrimEnd()
$e = {
[CmdletBinding()]
param ()
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.Exception]::new('Test Parser Error'),
'ParserErrorText',
[System.Management.Automation.ErrorCategory]::ParserError,
@{
Line = 1
LineText = 'This is the line with the error'
}
)
)
} | Should -Throw -PassThru
$actual = ($e | Out-String).TrimEnd()
$actual | Should -BeExactly $expected
}
It 'Parser TargetObject shows File information' {
$expected = (@(
": MyFile.ps1"
"Line |"
" 1 | This is the line with the error"
" | Test Parser Error"
) -join ([Environment]::NewLine)).TrimEnd()
$e = {
[CmdletBinding()]
param ()
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.Exception]::new('Test Parser Error'),
'ParserErrorText',
[System.Management.Automation.ErrorCategory]::ParserError,
@{
File = 'MyFile.ps1'
Line = 1
LineText = 'This is the line with the error'
}
)
)
} | Should -Throw -PassThru
$actual = ($e | Out-String).TrimEnd()
$actual | Should -BeExactly $expected
}
It 'Parser TargetObject has StartColumn' {
$expected = (@(
": "
"Line |"
" 5 | This is the line with the error"
" | ~~~~~~~~~~~~~~"
" | Test Parser Error"
) -join ([Environment]::NewLine)).TrimEnd()
$e = {
[CmdletBinding()]
param ()
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.Exception]::new('Test Parser Error'),
'ParserErrorText',
[System.Management.Automation.ErrorCategory]::ParserError,
@{
Line = 5
LineText = 'This is the line with the error'
StartColumn = 18
}
)
)
} | Should -Throw -PassThru
$actual = ($e | Out-String).TrimEnd()
$actual | Should -BeExactly $expected
}
It 'Parser TargetObject has StartColumn and EndColumn' {
$expected = (@(
": "
"Line |"
" 5 | This is the line with the error"
" | ~~~~"
" | Test Parser Error"
) -join ([Environment]::NewLine)).TrimEnd()
$e = {
[CmdletBinding()]
param ()
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.Exception]::new('Test Parser Error'),
'ParserErrorText',
[System.Management.Automation.ErrorCategory]::ParserError,
@{
Line = 5
LineText = 'This is the line with the error'
StartColumn = 18
EndColumn = 22
}
)
)
} | Should -Throw -PassThru
$actual = ($e | Out-String).TrimEnd()
$actual | Should -BeExactly $expected
}
It 'Parser TargetObject has StartColumn at end of the line' {
$expected = (@(
": "
"Line |"
" 5 | This is the line with the error"
" | ~"
" | Test Parser Error"
) -join ([Environment]::NewLine)).TrimEnd()
$e = {
[CmdletBinding()]
param ()
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.Exception]::new('Test Parser Error'),
'ParserErrorText',
[System.Management.Automation.ErrorCategory]::ParserError,
@{
Line = 5
LineText = 'This is the line with the error'
StartColumn = 31
}
)
)
} | Should -Throw -PassThru
$actual = ($e | Out-String).TrimEnd()
$actual | Should -BeExactly $expected
}
It 'Parser TargetObject has StartColumn at end of the line with EndColumn' {
$expected = (@(
": "
"Line |"
" 5 | This is the line with the error"
" | ~"
" | Test Parser Error"
) -join ([Environment]::NewLine)).TrimEnd()
$e = {
[CmdletBinding()]
param ()
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.Exception]::new('Test Parser Error'),
'ParserErrorText',
[System.Management.Automation.ErrorCategory]::ParserError,
@{
Line = 5
LineText = 'This is the line with the error'
StartColumn = 31
EndColumn = 32
}
)
)
} | Should -Throw -PassThru
$actual = ($e | Out-String).TrimEnd()
$actual | Should -BeExactly $expected
}
It 'Parser TargetObject ignores EndColumn if no StartColumn' {
$expected = (@(
": "
"Line |"
" 1 | This is the line with the error"
" | Test Parser Error"
) -join ([Environment]::NewLine)).TrimEnd()
$e = {
[CmdletBinding()]
param ()
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.Exception]::new('Test Parser Error'),
'ParserErrorText',
[System.Management.Automation.ErrorCategory]::ParserError,
@{
Line = 1
LineText = 'This is the line with the error'
EndColumn = 22
}
)
)
} | Should -Throw -PassThru
$actual = ($e | Out-String).TrimEnd()
$actual | Should -BeExactly $expected
}
It 'Parser TargetObject converts StartColumn and EndColumn from string' {
$expected = (@(
": "
"Line |"
" 5 | This is the line with the error"
" | ~~~~"
" | Test Parser Error"
) -join ([Environment]::NewLine)).TrimEnd()
$e = {
[CmdletBinding()]
param ()
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.Exception]::new('Test Parser Error'),
'ParserErrorText',
[System.Management.Automation.ErrorCategory]::ParserError,
@{
Line = 5
LineText = 'This is the line with the error'
StartColumn = "18"
EndColumn = "22"
}
)
)
} | Should -Throw -PassThru
$actual = ($e | Out-String).TrimEnd()
$actual | Should -BeExactly $expected
}
It 'Parser TargetObject ignores StartColumn if it cannot be converted' {
$expected = (@(
": "
"Line |"
" 1 | This is the line with the error"
" | Test Parser Error"
) -join ([Environment]::NewLine)).TrimEnd()
$e = {
[CmdletBinding()]
param ()
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.Exception]::new('Test Parser Error'),
'ParserErrorText',
[System.Management.Automation.ErrorCategory]::ParserError,
@{
Line = 1
LineText = 'This is the line with the error'
StartColumn = 'abc'
EndColumn = 22
}
)
)
} | Should -Throw -PassThru
$actual = ($e | Out-String).TrimEnd()
$actual | Should -BeExactly $expected
}
It 'Parser TargetObject ignores EndColumn if it cannot be converted' {
$expected = (@(
": "
"Line |"
" 5 | This is the line with the error"
" | ~~~~~~~~~~~~~~"
" | Test Parser Error"
) -join ([Environment]::NewLine)).TrimEnd()
$e = {
[CmdletBinding()]
param ()
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.Exception]::new('Test Parser Error'),
'ParserErrorText',
[System.Management.Automation.ErrorCategory]::ParserError,
@{
Line = 5
LineText = 'This is the line with the error'
StartColumn = 18
EndColumn = 'abc'
}
)
)
} | Should -Throw -PassThru
$actual = ($e | Out-String).TrimEnd()
$actual | Should -BeExactly $expected
}
It 'Parser TargetObject ignores StartColumn with invalid value <Value>' -TestCases @(
@{ Value = -1 }
@{ Value = 0 }
@{ Value = 32 } # Beyond end of line
) {
param ($Value)
$expected = (@(
": "
"Line |"
" 1 | This is the line with the error"
" | Test Parser Error"
) -join ([Environment]::NewLine)).TrimEnd()
$e = {
[CmdletBinding()]
param ()
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.Exception]::new('Test Parser Error'),
'ParserErrorText',
[System.Management.Automation.ErrorCategory]::ParserError,
@{
Line = 1
LineText = 'This is the line with the error'
StartColumn = $Value
}
)
)
} | Should -Throw -PassThru
$actual = ($e | Out-String).TrimEnd()
$actual | Should -BeExactly $expected
}
It 'Parser TargetObject ignores EndColumn with invalid value <Value>' -TestCases @(
@{ Value = -1 }
@{ Value = 0 }
@{ Value = 17 } # Before StartColumn
@{ Value = 18 } # Equal to StartColumn
@{ Value = 33 } # Beyond end of line + 1
) {
param ($Value)
$expected = (@(
": "
"Line |"
" 1 | This is the line with the error"
" | ~~~~~~~~~~~~~~"
" | Test Parser Error"
) -join ([Environment]::NewLine)).TrimEnd()
$e = {
[CmdletBinding()]
param ()
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.Exception]::new('Test Parser Error'),
'ParserErrorText',
[System.Management.Automation.ErrorCategory]::ParserError,
@{
Line = 1
LineText = 'This is the line with the error'
StartColumn = 18
EndColumn = $Value
}
)
)
} | Should -Throw -PassThru
$actual = ($e | Out-String).TrimEnd()
$actual | Should -BeExactly $expected
}
It 'Exception thrown from Enumerator.MoveNext in a pipeline shows information' {
$e = {
$l = [System.Collections.Generic.List[string]] @('one', 'two')
$l | ForEach-Object { $null = $l.Remove($_) }
} | Should -Throw -ErrorId 'BadEnumeration' -PassThru | Out-String
$e | Should -BeLike 'InvalidOperation:*'
}
It 'Displays a RecommendedAction if present in the ErrorRecord' {
$testScript = 'Write-Error -Message ''TestError'' -RecommendedAction ''TestAction'''
$e = & "$PSHOME/pwsh" -noprofile -command $testScript 2>&1 | Out-String
$e | Should -BeLike "*$([Environment]::NewLine) Recommendation: TestAction*"
}
}
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!*'
}
}
Context 'DetailedView tests' {
It 'Detailed error is rendered' {
try {
$ErrorView = 'DetailedView'
throw 'Oops!'
} catch {
# an extra newline gets added by the formatting system so we remove them
$e = ($_ | Out-String).Trim([Environment]::NewLine.ToCharArray())
}
$e | Should -BeExactly (Get-Error | Out-String).Trim([Environment]::NewLine.ToCharArray())
}
}
}
|