-
-
Notifications
You must be signed in to change notification settings - Fork 476
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
FixStacktraceLanguage #2391
Changes from 8 commits
0bdea48
b2410c4
ce24250
59c9c93
086fddf
19ca5e9
4d0a432
8c25c0a
f3d27b1
f352060
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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" | ||||
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) { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider reversing this. Easier to read and less nesting. # 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' | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. Please move fallback exception to be part of |
||||
} | ||||
} | ||||
catch { | ||||
#Call Fallbackfunction | ||||
$StackTraceLanguage = Get-StackTraceLanguageFallBack -StackTrace $($PSItem.ScriptStackTrace) | ||||
} | ||||
Return $StackTraceLanguage | ||||
} | ||||
#defining script variable | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
$script:StackTraceLanguage = Get-StackTraceLanguage | ||||
|
||||
if (notDefined PesterPreference) { | ||||
$PesterPreference = [PesterConfiguration]::Default | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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