Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FixStacktraceLanguage #2391

Closed
wants to merge 10 commits into from
Closed
14 changes: 14 additions & 0 deletions src/Pester.Runtime.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ $script:SessionStateInternalProperty = [System.Management.Automation.SessionStat
$script:ScriptBlockSessionStateInternalProperty = [System.Management.Automation.ScriptBlock].GetProperty('SessionStateInternal', $flags)
$script:ScriptBlockSessionStateProperty = [System.Management.Automation.ScriptBlock].GetProperty("SessionState", $flags)

function Get-StackTraceLanguage {
$err = try { throw "err" } catch { $_ }
$firstFrame = ($err.ScriptStackTrace -split [System.Environment]::NewLine, 2 )[0]
if ($firstFrame -match "(?<at>.[^G]?)\s+Get-StackTraceLanguage(?<separator>.).+:\s+(?<line>.\S+)\s\d+") {
@{
At = $Matches["at"]
Separator = $Matches["separator"]
Line = $Matches["line"]
}
}
}
Comment on lines +62 to +72
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed your code to parse it from exception that we throw, as originally suggested. This should be cheaper and easier to replicate than relying on internal resource names because we rely on public api.

#defining script variable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#defining script variable

$script:StackTraceLanguage = Get-StackTraceLanguage

if (notDefined PesterPreference) {
$PesterPreference = [PesterConfiguration]::Default
}
Expand Down
22 changes: 3 additions & 19 deletions src/functions/Output.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -539,25 +539,9 @@ function ConvertTo-FailureLines {
}
else {
# omit the lines internal to Pester
if ((GetPesterOS) -ne 'Windows') {
[String]$isPesterFunction = '^at .*, .*/Pester.psm1: line [0-9]*$'
[String]$isShould = '^at (Should<End>|Invoke-Assertion), .*/Pester.psm1: line [0-9]*$'
# [String]$pattern6 = '^at <ScriptBlock>, (<No file>|.*/Pester.psm1): line [0-9]*$'
}
else {
[String]$isPesterFunction = '^at .*, .*\\Pester.psm1: line [0-9]*$'
[String]$isShould = '^at (Should<End>|Invoke-Assertion), .*\\Pester.psm1: line [0-9]*$'
}

# PESTER_BUILD
if ($true) {
# no code
# non inlined scripts will have different paths just omit everything from the src folder
$path = [regex]::Escape(($PSScriptRoot | & $SafeCommands["Split-Path"]))
[String]$isPesterFunction = "^at .*, .*$path.*: line [0-9]*$"
[String]$isShould = "^at (Should<End>|Invoke-Assertion), .*$path.*: line [0-9]*$"
}
# end PESTER_BUILD
$path = [regex]::Escape(($PSScriptRoot | & $SafeCommands["Split-Path"]))
[String]$isPesterFunction = "^{0}.*{1} .*$path.*: {2} [0-9]*$" -f $script:StackTraceLanguage.At, $script:StackTraceLanguage.Separator, $script:StackTraceLanguage.Line
[String]$isShould = "^{0} (Should<End>|Invoke-Assertion){1} .*$path.*: {2} [0-9]*$" -f $script:StackTraceLanguage.At, $script:StackTraceLanguage.Separator, $script:StackTraceLanguage.Line

# reducing the stack trace so we see only stack trace until the current It block and not up until the invocation of the
# whole test script itself. This is achieved by shortening the stack trace when any Runtime function is hit.
Expand Down
2 changes: 1 addition & 1 deletion src/functions/assertions/PesterThrow.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function Get-DoValuesMatch($ActualValue, $ExpectedValue) {
function Get-ExceptionLineInfo($info) {
# $info.PositionMessage has a leading blank line that we need to account for in PowerShell 2.0
$positionMessage = $info.PositionMessage -split '\r?\n' -match '\S' -join [System.Environment]::NewLine
return ($positionMessage -replace "^At ", "from ")
return ($positionMessage -replace "^$($script:StackTraceLanguage.At) ", "from ")
}

function ShouldThrowFailureMessage {
Expand Down
Loading