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
53 changes: 53 additions & 0 deletions src/Pester.Runtime.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,59 @@ $flags = [System.Reflection.BindingFlags]'Instance,NonPublic'
$script:SessionStateInternalProperty = [System.Management.Automation.SessionState].GetProperty('Internal', $flags)
$script:ScriptBlockSessionStateInternalProperty = [System.Management.Automation.ScriptBlock].GetProperty('SessionStateInternal', $flags)
$script:ScriptBlockSessionStateProperty = [System.Management.Automation.ScriptBlock].GetProperty("SessionState", $flags)
# function for extracting StacktraceSystemLanguage
function Get-StackTraceLanguageFallBack {
param(
[Parameter(Mandatory = $true)]
[string]$StackTrace
)
$StackTraceLanguage = [PSCustomObject]@{
At = "at"
Line = "line"
}
$Regex = "(?<At>.*)\s(?<ScriptBlockOrFunction>\<\w+\>),\s(?<FileName>\<.+\>)\s*:\s(?<Line>\w+)\s(?<LineNumber>\w+)\z"
Copy link
Collaborator

@fflaten fflaten Jan 11, 2024

Choose a reason for hiding this comment

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

Suggested change
}
$Regex = "(?<At>.*)\s(?<ScriptBlockOrFunction>\<\w+\>),\s(?<FileName>\<.+\>)\s*:\s(?<Line>\w+)\s(?<LineNumber>\w+)\z"
}
$Regex = "(?<At>.*)\s(?<ScriptBlock>\<\w+\>),\s(?<NoFile>\<.+\>)\s*:\s(?<Line>.+)\s(?<LineNumber>\d+)\z"

Not sure if "line" could be multiple words, but regex should be consistent between this and Get-StackTraceLanguage.

Also consider comment to make it clear that this only matches final line in stacktrace with scriptblock-source, e.g. at <ScriptBlock>, <No file>: line 1

if ($StackTrace -match $Regex) {
$StackTraceLanguage.At = $Matches["At"]
$StackTraceLanguage.Line = $Matches["Line"]
}
Return $StackTraceLanguage
}

function Get-StackTraceLanguage {
#Full fallback scenario to the solution of PR #2276 in case of error
try {
if ($PSVersionTable.PSVersion.Major -ge 5) {
Copy link
Collaborator

@fflaten fflaten Jan 11, 2024

Choose a reason for hiding this comment

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

Consider reversing this. Easier to read and less nesting.
See related comment #2391 (comment)

# Required string resource missing prior to version 5 - use fallback
if ($PSVersionTable.PSVersion.Major -lt 5) {
    return (Get-StackTraceLanguageFallBack)
}

if ($PSVersionTable.PSVersion.Major -gt 5) {
    $StackTraceRessourceName = 'System.Management.Automation.resources.DebuggerStrings'
    .... # rest of PS 5+ code.

if ($PSVersionTable.PSVersion.Major -gt 5) {
$StackTraceRessourceName = 'System.Management.Automation.resources.DebuggerStrings'
}
else {
$StackTraceRessourceName = 'DebuggerStrings'
}
$R = [System.Resources.ResourceManager]::new($StackTraceRessourceName, [System.Reflection.Assembly]::GetAssembly([System.Management.Automation.Breakpoint]))
$Result = $r.GetString('StackTraceFormat') -match '^(?<At>.*) \{0\}, \{1\}: (?<Line>.*) \{2\}$'
if (!($Result)) {
throw 'StackTraceFormat in a unknown format'
}
else {
$StackTraceLanguage = [PSCustomObject]@{
At = $Matches["At"]
Line = $Matches["Line"]
}
}
}
else {
#Prior to version 5 there are no ressources in the Assembly available (System.Management.Automation)
throw 'Fallback for PSVersion 3/4'
Copy link
Member

Choose a reason for hiding this comment

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

Using exceptions for control flow is not ideal, this will always throw for powershell 4, if you just reduce the scope of the try to be in the positive leg of the if it should make the code no more complicated, and avoid this issue.

Copy link
Collaborator

@fflaten fflaten Jan 11, 2024

Choose a reason for hiding this comment

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

I agree. Please move fallback exception to be part of Get-StackTraceLanguageFallBack. That way we can remove the large try/catch here and just call the fallback if no result or PS <5.

}
}
catch {
#Call Fallbackfunction
$StackTraceLanguage = Get-StackTraceLanguageFallBack -StackTrace $($PSItem.ScriptStackTrace)
}
Return $StackTraceLanguage
}
#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
8 changes: 4 additions & 4 deletions src/functions/Output.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -529,13 +529,13 @@ 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]$isPesterFunction = '^{0}.*, .*/Pester.psm1: {1} [0-9]*$' -f $script:StackTraceLanguage.At, $script:StackTraceLanguage.Line
[String]$isShould = '^{0} (Should<End>|Invoke-Assertion), .*/Pester.psm1: {1} [0-9]*$' -f $script:StackTraceLanguage.At, $script:StackTraceLanguage.Line
# [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]*$'
[String]$isPesterFunction = '^{0} .*, .*\\Pester.psm1: {1} [0-9]*$' -f $script:StackTraceLanguage.At, $script:StackTraceLanguage.Line
[String]$isShould = '^{0} (Should<End>|Invoke-Assertion), .*\\Pester.psm1: {1} [0-9]*$' -f $script:StackTraceLanguage.At, $script:StackTraceLanguage.Line
}

# PESTER_BUILD
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