MegaBites-AI/Windows-powershell
0308
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Get-PSCallStack DRT Unit Tests" -Tags "CI" {4 BeforeAll {5 $scriptFileName = "GetTryCatchCallStack.ps1"6 $scriptFilePath = Join-Path $TestDrive -ChildPath $scriptFileName7 }8 It "Verifies that the script block of a catch clause does not show up on the call stack" {9 $fileStream = @"10 function foo()11 {12 try13 {14 throw 115 }16 catch17 {18 bar19 }20 }21 22 function bar()23 {24 try25 {26 throw 127 }28 catch29 {30 try31 {32 throw 133 }34 catch35 {36 Get-PSCallStack37 }38 }39 }40 41 foo42"@43 44 $fileStream > $scriptFilePath45 46 $results = & "$scriptFilePath"47 $results.Count | Should -BeGreaterThan 348 $results[0].Command | Should -BeExactly "bar"49 $results[0].ScriptName | Should -Be $scriptFilePath50 $results[0].ScriptLineNumber | Should -Be 2751 $results[0].InvocationInfo.ScriptLineNumber | Should -Be 952 $results[0].Location | Should -Match $scriptFileName53 54 $results[1].Command | Should -BeExactly "foo"55 $results[1].ScriptName | Should -Be $scriptFilePath56 $results[1].ScriptLineNumber | Should -Be 957 $results[1].InvocationInfo.ScriptLineNumber | Should -Be 3258 $results[1].Location | Should -Match $scriptFileName59 60 #InvocationInfo.ScriptLineNumber: Gets the line number of the script that contains the command61 $results[2].Command | Should -Be $scriptFileName62 $results[2].ScriptName | Should -Be $scriptFilePath63 $results[2].ScriptLineNumber | Should -Be 3264 $results[2].InvocationInfo.ScriptLineNumber | Should -Be 4665 $results[2].Location | Should -Match $scriptFileName66 }67 68 It "Verify that the script block of a trap statement shows up on the call stack" {69 $fileStream = @"70 trap71 {72 Get-PSCallStack73 continue74 }75 76 throw 177"@78 79 $fileStream > $scriptFilePath80 $results = & "$scriptFilePath"81 $results.Count | Should -BeGreaterThan 282 $results[0].Command | Should -Be $scriptFileName83 $results[0].ScriptName | Should -Be $scriptFilePath84 $results[0].ScriptLineNumber | Should -Be 385 $results[0].InvocationInfo.ScriptLineNumber | Should -Be 8086 87 $results[1].Command | Should -Be $scriptFileName88 $results[1].ScriptName | Should -Be $scriptFilePath89 $results[1].ScriptLineNumber | Should -Be 790 $results[1].InvocationInfo.ScriptLineNumber | Should -Be 8091 }92 93 It "Get-PSCallStack returns Arguments" {94 & { (Get-PSCallStack)[0].Arguments } 'foo' | Should -Match 'foo'95 & { param ($x) (Get-PSCallStack)[0].Arguments } 'foo' | Should -Match 'foo'96 & { (Get-PSCallStack)[0].Arguments } 'foo' 'bar' | Should -Match 'foo, bar'97 }98}99 