Note that there are some explanatory texts on larger screens.

plurals
  1. POIs Powershell's stack trace broken?
    primarykey
    data
    text
    <p>I am writing a simple unit test harness in powershell</p> <p>I designed the harness such that its assert functions takes a script-block as a parameter to allow the harness to run the code from within the assert function and treat any exceptions are thrown as a test failure.</p> <p>If the tests fail I want to return line in the unit test in which the test fails. My plan was to do this by taking a stacktrace (Get-PSCallStack) at the start of each assert method and use the information for the second stack-frame which I assume should correspond to the line where the assert function was called.</p> <p>In practice, I found that the information that powershell gave back seemed wrong. The second stack-frame refers to the the correct file as expected but always gives the line number at which I called Get-PSCallStack in the assert method. Sometimes this number might be even higher than the number of lines in the file given (i.e. Location is given as "ScriptFile.ps1 line 88" but the file only has 20 lines).</p> <p>Is there a problem with the stack trace in powershell or is there something I am not understanding here?</p> <p><strong>Edit</strong></p> <p>As requested I am posting an example which should produce the same results</p> <p>Tester.ps1</p> <pre><code>#File 1 (Tester.ps1) #Creates the tester object $tester = (New-Object PSObject); $tester | Add-Member -MemberType ScriptMethod -Name AssertTrue -Value { param($expression); $stackFrame = (GEt-PSCallStack)[1]; try{ $result = &amp;$expression; if($result -eq $true){ $this.LogPass(); }else{ $this.LogFailure("Evaluation Failed expected ""$true"" got ""$false""", $stackFrame); } }catch [Exception]{ $this.LogFailure("Unexpected exception encountered", $stackFrame); } } $tester | Add-Member -MemberType ScriptMethod -Name LogPass -Value { #Do nothing }; $tester | Add-Member -MemberType ScriptMethod -Name LogFailure -Value { param($message, $stackFrame); "Failure Encounterd"; "Command: $($stackFrame.Command)" "Location: $($stackFrame.Location)"; "Message: $message"; } return $tester; </code></pre> <p>TestCase.ps1</p> <pre><code>#File 2 (TestCase.ps1) #Runs the tests using the tester object $tester = &amp;(Resolve-Path "Tester.ps1"); function TestFailure($tester){ $expression = {$false}; $tester.AssertTrue($expression); } TestFailure($tester); </code></pre> <p>The assert is called on Line 7 of TestCase.ps1 and the call stack is captured on line 9 of Tester.ps1</p> <p>This prints</p> <pre><code>Failure Encounterd Command: TestFailure Location: Tester.ps1: Line 9 Message: Evaluation Failed expected "True" got "False" </code></pre> <p>The command is correct but both the file and the line are wrong</p> <p>The next frame of the stack trace correctly describes where TestFailure() is called with its location as "TestCase.ps1: Line 11"</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload