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

Fix issue pester#2062 "Stacktrace is not filtered in non-english system languages" #2276

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/functions/Output.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ function Write-PesterHostMessage {
$message = "$($message -replace '(?m)^', "$fg$bg")$($ANSIcodes.ResetAll)"

& $SafeCommands['Write-Host'] -Object $message -NoNewLine:$NoNewLine
} else {
}
else {
if ($RenderMode -eq 'Plaintext') {
if ($PSBoundParameters.ContainsKey('ForegroundColor')) {
$null = $PSBoundParameters.Remove('ForegroundColor')
Expand Down Expand Up @@ -526,14 +527,39 @@ function ConvertTo-FailureLines {
$lines.Trace += $traceLines
}
else {

## Exceptions may be localized depending on the system used.
## We generate an exception on purpose and catch it in order to parse the format
function GetLocalizedStackTraceElements {
try {
throw 'Generate exception on purpose'
}
catch {
$regex = "(?<At>.*)\s(?<ScriptBlockOrFunction>\<\w+\>),\s(?<FileName>\<.+\>)\s*:\s(?<Line>\w+)\s(?<LineNumber>\w+)\z"
if ($PSItem.ScriptStackTrace -match $regex) {
$LocalizedAt = $Matches["At"]
$LocalizedLine = $Matches["Line"]
}
else {
$LocalizedAt = "at"
$LocalizedLine = "line"
}
}
return @{'LocalizedAt' = $LocalizedAt; 'LocalizedLine' = $LocalizedLine }
}

$internal_localizestacktrace = GetLocalizedStackTraceElements
$internal_localizedAt = $internal_localizestacktrace.LocalizedAt
$internal_localizedLine = $internal_localizestacktrace.LocalizedLine

# 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]$isPesterFunction = '^{0} .*, .*\\Pester.psm1\s*: {1} [0-9]*$' -f $internal_localizedAt, $internal_localizedLine
[String]$isShould = '^at (Should<End>|Invoke-Assertion), .*\\Pester.psm1: line [0-9]*$'
}

Expand All @@ -542,7 +568,7 @@ function ConvertTo-FailureLines {
# 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]$isPesterFunction = "^{0} .*, .*{1}.*: {2} [0-9]*$" -f $internal_localizedAt, $path, $internal_localizedLine
[String]$isShould = "^at (Should<End>|Invoke-Assertion), .*$path.*: line [0-9]*$"
}
# end PESTER_BUILD
Expand Down