diff --git a/src/Pester.Runtime.ps1 b/src/Pester.Runtime.ps1 index 07c23d57b..7df31a8f5 100644 --- a/src/Pester.Runtime.ps1 +++ b/src/Pester.Runtime.ps1 @@ -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 "(?.[^G]?)\s+Get-StackTraceLanguage(?.).+:\s+(?.\S+)\s\d+") { + @{ + At = $Matches["at"] + Separator = $Matches["separator"] + Line = $Matches["line"] + } + } +} +#defining script variable +$script:StackTraceLanguage = Get-StackTraceLanguage + if (notDefined PesterPreference) { $PesterPreference = [PesterConfiguration]::Default } diff --git a/src/functions/Output.ps1 b/src/functions/Output.ps1 index 53712bb26..d61a82a28 100644 --- a/src/functions/Output.ps1 +++ b/src/functions/Output.ps1 @@ -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|Invoke-Assertion), .*/Pester.psm1: line [0-9]*$' - # [String]$pattern6 = '^at , (|.*/Pester.psm1): line [0-9]*$' - } - else { - [String]$isPesterFunction = '^at .*, .*\\Pester.psm1: line [0-9]*$' - [String]$isShould = '^at (Should|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|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|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. diff --git a/src/functions/assertions/PesterThrow.ps1 b/src/functions/assertions/PesterThrow.ps1 index 9ac8b9a03..9e7322192 100644 --- a/src/functions/assertions/PesterThrow.ps1 +++ b/src/functions/assertions/PesterThrow.ps1 @@ -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 {