|
| 1 | +filter GetGoogleWorkflowPositionMessage { |
| 2 | + [CmdletBinding()] |
| 3 | + [OUtputType([string])] |
| 4 | + param( |
| 5 | + [Parameter(ValueFromPipeline)] |
| 6 | + [System.Management.Automation.ErrorRecord] |
| 7 | + $InputObject |
| 8 | + ) |
| 9 | + $InvocationInfo = $InputObject.InvocationInfo |
| 10 | + # Handle case where there is a TargetObject from a Pester `Should` assertion failure and we can show the error at the target rather than the script source |
| 11 | + # Note that in some versions, this is a Dictionary<,> and in others it's a hashtable. So we explicitly cast to a shared interface in the method invocation |
| 12 | + # to force using `IDictionary.Contains`. Hashtable does have it's own `ContainKeys` as well, but if they ever opt to use a custom `IDictionary`, that may not. |
| 13 | + $useTargetObject = $null -ne $InputObject.TargetObject -and |
| 14 | + $InputObject.TargetObject -is [System.Collections.IDictionary] -and |
| 15 | + ([System.Collections.IDictionary]$InputObject.TargetObject).Contains('Line') -and |
| 16 | + ([System.Collections.IDictionary]$InputObject.TargetObject).Contains('LineText') |
| 17 | + |
| 18 | + $file = if ($useTargetObject) { |
| 19 | + "$($InputObject.TargetObject.File)" |
| 20 | + } elseif (.ScriptName) { |
| 21 | + "$($InvocationInfo.ScriptName)" |
| 22 | + } |
| 23 | + |
| 24 | + $line = if ($useTargetObject) { |
| 25 | + $InputObject.TargetObject.Line |
| 26 | + } else { |
| 27 | + $InvocationInfo.ScriptLineNumber |
| 28 | + } |
| 29 | + |
| 30 | + if ($useTargetObject) { |
| 31 | + "file=$file,line=$line" |
| 32 | + } else { |
| 33 | + $column = $InvocationInfo.OffsetInLine |
| 34 | + |
| 35 | + $Length = $InvocationInfo.PositionMessage.Split($newline)[-1].Substring(1).Trim().Length |
| 36 | + $endColumn = $column + $Length |
| 37 | + "file=$file,line=$line,col=$column,endColumn=$endColumn" |
| 38 | + } |
| 39 | +} |
0 commit comments