Files

98 lines
3.1 KiB
PowerShell
Raw Permalink Normal View History

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Get-PSCallStack DRT Unit Tests" -Tags "CI" {
2016-06-01 02:29:44 -07:00
BeforeAll {
$scriptFileName = "GetTryCatchCallStack.ps1"
$scriptFilePath = Join-Path $TestDrive -ChildPath $scriptFileName
}
2017-01-16 13:31:14 -08:00
It "Verifies that the script block of a catch clause does not show up on the call stack" {
2016-06-01 02:29:44 -07:00
$fileStream = @"
function foo()
{
try
{
throw 1
}
catch
{
bar
}
}
function bar()
{
try
{
throw 1
}
catch
{
try
{
throw 1
}
catch
{
Get-PSCallStack
}
}
}
foo
"@
$fileStream > $scriptFilePath
$results = & "$scriptFilePath"
$results.Count | Should -BeGreaterThan 3
$results[0].Command | Should -BeExactly "bar"
$results[0].ScriptName | Should -Be $scriptFilePath
$results[0].ScriptLineNumber | Should -Be 27
$results[0].InvocationInfo.ScriptLineNumber | Should -Be 9
2019-12-02 23:43:35 +05:00
$results[0].Location | Should -Match $scriptFileName
2016-06-01 02:29:44 -07:00
$results[1].Command | Should -BeExactly "foo"
$results[1].ScriptName | Should -Be $scriptFilePath
$results[1].ScriptLineNumber | Should -Be 9
$results[1].InvocationInfo.ScriptLineNumber | Should -Be 32
2019-12-02 23:43:35 +05:00
$results[1].Location | Should -Match $scriptFileName
2016-06-01 02:29:44 -07:00
#InvocationInfo.ScriptLineNumber: Gets the line number of the script that contains the command
$results[2].Command | Should -Be $scriptFileName
$results[2].ScriptName | Should -Be $scriptFilePath
$results[2].ScriptLineNumber | Should -Be 32
$results[2].InvocationInfo.ScriptLineNumber | Should -Be 46
2019-12-02 23:43:35 +05:00
$results[2].Location | Should -Match $scriptFileName
2016-06-01 02:29:44 -07:00
}
It "Verify that the script block of a trap statement shows up on the call stack" {
$fileStream = @"
trap
{
Get-PSCallStack
continue
}
throw 1
"@
$fileStream > $scriptFilePath
$results = & "$scriptFilePath"
$results.Count | Should -BeGreaterThan 2
$results[0].Command | Should -Be $scriptFileName
$results[0].ScriptName | Should -Be $scriptFilePath
$results[0].ScriptLineNumber | Should -Be 3
2019-12-02 23:43:35 +05:00
$results[0].InvocationInfo.ScriptLineNumber | Should -Be 80
2016-06-01 02:29:44 -07:00
$results[1].Command | Should -Be $scriptFileName
$results[1].ScriptName | Should -Be $scriptFilePath
$results[1].ScriptLineNumber | Should -Be 7
2019-12-02 23:43:35 +05:00
$results[1].InvocationInfo.ScriptLineNumber | Should -Be 80
}
It "Get-PSCallStack returns Arguments" {
& { (Get-PSCallStack)[0].Arguments } 'foo' | Should -Match 'foo'
& { param ($x) (Get-PSCallStack)[0].Arguments } 'foo' | Should -Match 'foo'
& { (Get-PSCallStack)[0].Arguments } 'foo' 'bar' | Should -Match 'foo, bar'
2016-06-01 02:29:44 -07:00
}
2016-07-25 14:01:41 -07:00
}