Skip to content

Commit c64e24e

Browse files
committed
Add a bunch of stuff that should be in the "Strings" module
1 parent 82f2d6f commit c64e24e

21 files changed

+588
-287
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
uses: actions/upload-artifact@v4
5656
with:
5757
name: PesterTests
58-
path: ${{github.workspace}}/Tests
58+
path: ${{github.workspace}}/tests
5959
- name: Upload RequiredModules.psd1
6060
uses: actions/upload-artifact@v4
6161
with:

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
output/
22
Modules/
3+
# Ignore version number folders (I put my module in these so I can use it)
4+
/[0-9]*/

Build.build.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ $ErrorView = 'DetailedView'
3131

3232
# The name of the module to publish
3333
$script:PSModuleName = "TerminalBlocks"
34-
$script:RequiredCodeCoverage = 0.85
34+
# Pester doesn't measure the code coverage properly, because we test things by running in a new session
35+
$script:RequiredCodeCoverage = 0.0
3536
# Use Env because then Earthly can override it
3637
$Env:OUTPUT_ROOT ??= Join-Path $BuildRoot Modules
3738

Earthfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ARG --global EARTHLY_BUILD_SHA
88
ARG --global EARTHLY_GIT_BRANCH
99
# These are my common paths, used in my shared /Tasks repo
1010
ARG --global OUTPUT_ROOT=/Modules
11-
ARG --global TEST_ROOT=/Tests
11+
ARG --global TEST_ROOT=/tests
1212
ARG --global TEMP_ROOT=/temp
1313
# These are my common build args, used in my shared /Tasks repo
1414
ARG --global MODULE_NAME=ErrorView
@@ -59,7 +59,7 @@ test:
5959
# BUILD +build
6060
FROM +build
6161
# Copy the test files here, so we can avoid rebuilding when iterating on tests
62-
COPY --if-exists --dir Tests/ ScriptAnalyzerSettings.psd1 /work
62+
COPY --if-exists --dir $TEST_ROOT ScriptAnalyzerSettings.psd1 /work
6363
RUN ["pwsh", "-Command", "Invoke-Build", "-Task", "Test", "-File", "Build.build.ps1"]
6464

6565
# SAVE ARTIFACT [--keep-ts] [--keep-own] [--if-exists] [--force] <src> [<artifact-dest-path>] [AS LOCAL <local-path>]

source/ErrorView.format.ps1xml

Lines changed: 1 addition & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,6 @@
66
<OutOfBand />
77
<ViewSelectedBy>
88
<TypeName>System.Management.Automation.ErrorRecord</TypeName>
9-
</ViewSelectedBy>
10-
<CustomControl>
11-
<CustomEntries>
12-
<CustomEntry>
13-
<CustomItem>
14-
<ExpressionBinding>
15-
<ScriptBlock>
16-
<![CDATA[
17-
Set-StrictMode -Off
18-
$ErrorActionPreference = 'Stop'
19-
trap { 'Error found in error view definition: ' + $_.Exception.Message }
20-
21-
if ($formatter = @(Get-Command "ConvertTo-$($ErrorView -replace "View$")ErrorView" -ListImported -ErrorAction Ignore -ParameterName InputObject -ParameterType [System.Management.Automation.ErrorRecord])) {
22-
& ($formatter[0]) -InputObject $_
23-
} else {
24-
ConvertTo-NormalErrorView $_
25-
}
26-
]]>
27-
</ScriptBlock>
28-
</ExpressionBinding>
29-
</CustomItem>
30-
</CustomEntry>
31-
</CustomEntries>
32-
</CustomControl>
33-
</View>
34-
<View>
35-
<Name>ExceptionInstance</Name>
36-
<OutOfBand />
37-
<ViewSelectedBy>
389
<TypeName>System.Exception</TypeName>
3910
</ViewSelectedBy>
4011
<CustomControl>
@@ -43,56 +14,7 @@
4314
<CustomItem>
4415
<ExpressionBinding>
4516
<ScriptBlock>
46-
<![CDATA[
47-
Set-StrictMode -Off
48-
$ErrorActionPreference = 'Stop'
49-
trap { 'Error found in error view definition: ' + $_.Exception.Message }
50-
51-
if ($_.ErrorRecord) {
52-
$Record = $_.ErrorRecord
53-
if ($formatter = @(Get-Command "ConvertTo-$($ErrorView -replace "View$")ErrorView" -ListImported -ErrorAction Ignore -ParameterName InputObject -ParameterType [System.Management.Automation.ErrorRecord])) {
54-
& ($formatter[0]) -InputObject $Record
55-
} else {
56-
ConvertTo-NormalErrorView $Record
57-
}
58-
} else {
59-
if ($formatter = @(Get-Command "ConvertTo-$($ErrorView -replace "View$")ExceptionView" -ListImported -ErrorAction Ignore -ParameterName InputObject -ParameterType [System.Exception])) {
60-
& ($formatter[0]) -InputObject $_
61-
} else {
62-
ConvertTo-NormalExceptionView $_
63-
}
64-
}
65-
]]>
66-
</ScriptBlock>
67-
</ExpressionBinding>
68-
</CustomItem>
69-
</CustomEntry>
70-
</CustomEntries>
71-
</CustomControl>
72-
</View>
73-
<View>
74-
<Name>InformationRecord</Name>
75-
<OutOfBand />
76-
<ViewSelectedBy>
77-
<TypeName>System.Management.Automation.InformationRecord</TypeName>
78-
</ViewSelectedBy>
79-
<CustomControl>
80-
<CustomEntries>
81-
<CustomEntry>
82-
<CustomItem>
83-
<ExpressionBinding>
84-
<ScriptBlock>
85-
$_.MessageData | Format-List * | Out-String
86-
</ScriptBlock>
87-
</ExpressionBinding>
88-
<ExpressionBinding>
89-
<ScriptBlock>
90-
"Tags: " + @($_.Tags -join ", ")
91-
</ScriptBlock>
92-
</ExpressionBinding>
93-
<ExpressionBinding>
94-
<ScriptBlock>
95-
$_ | Select-Object * -ExcludeProperty Tags, MessageData | Format-List * -Force | Out-String
17+
Format-Error -View $global:ErrorView -InputObject $_
9618
</ScriptBlock>
9719
</ExpressionBinding>
9820
</CustomItem>

source/postfix.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
if ($script:ErrorView) {
2-
Set-ErrorView $ErrorView
1+
if ($ErrorViewArgument) {
2+
Set-ErrorView $ErrorViewArgument
33
} elseif ($Env:GITHUB_ACTIONS -or $Env:TF_BUILD) {
44
Set-ErrorView "DetailedErrorView"
55
} else {

source/prefix.ps1

Lines changed: 41 additions & 2 deletions
Large diffs are not rendered by default.

source/private/GetConciseMessage.ps1

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ filter GetConciseMessage {
55
[System.Management.Automation.ErrorRecord]
66
$InputObject
77
)
8-
$err = $InputObject
98
$posmsg = ''
109
$headerWhitespace = ''
1110
$message = ''
@@ -14,44 +13,45 @@ filter GetConciseMessage {
1413
# 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
1514
# Note that in some versions, this is a Dictionary&lt;,&gt; and in others it's a hashtable. So we explicitly cast to a shared interface in the method invocation
1615
# 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.
17-
$useTargetObject = $null -ne $err.TargetObject -and
18-
$err.TargetObject -is [System.Collections.IDictionary] -and
19-
([System.Collections.IDictionary]$err.TargetObject).Contains('Line') -and
20-
([System.Collections.IDictionary]$err.TargetObject).Contains('LineText')
16+
$useTargetObject = $null -ne $InputObject.TargetObject -and
17+
$InputObject.TargetObject -is [System.Collections.IDictionary] -and
18+
([System.Collections.IDictionary]$InputObject.TargetObject).Contains('Line') -and
19+
([System.Collections.IDictionary]$InputObject.TargetObject).Contains('LineText')
2120

2221
# The checks here determine if we show line detailed error information:
2322
# - check if `ParserError` and comes from PowerShell which eventually results in a ParseException, but during this execution it's an ErrorRecord
24-
$isParseError = $err.CategoryInfo.Category -eq 'ParserError' -and
25-
$err.Exception -is [System.Management.Automation.ParentContainsErrorRecordException]
23+
$isParseError = $InputObject.CategoryInfo.Category -eq 'ParserError' -and
24+
$InputObject.Exception -is [System.Management.Automation.ParentContainsErrorRecordException]
2625

2726
# - check if invocation is a script or multiple lines in the console
28-
$isMultiLineOrExternal = $myinv.ScriptName -or $myinv.ScriptLineNumber -gt 1
27+
$Invocation = $InputObject.InvocationInfo
28+
$isMultiLineOrExternal = $Invocation.ScriptName -or $Invocation.ScriptLineNumber -gt 1
2929

3030
# - check that it's not a script module as expectation is that users don't want to see the line of error within a module
3131
$shouldShowLineDetail = ($isParseError -or $isMultiLineOrExternal) -and
32-
$myinv.ScriptName -notmatch '\.psm1$'
32+
$Invocation.ScriptName -notmatch '\.psm1$'
3333

3434
if ($useTargetObject -or $shouldShowLineDetail) {
3535

3636
if ($useTargetObject) {
37-
$posmsg = "${resetcolor}$($err.TargetObject.File)${newline}"
38-
} elseif ($myinv.ScriptName) {
37+
$posmsg = "${resetcolor}$($InputObject.TargetObject.File)${newline}"
38+
} elseif ($Invocation.ScriptName) {
3939
if ($env:TERM_PROGRAM -eq 'vscode') {
4040
# If we are running in vscode, we know the file:line:col links are clickable so we use this format
41-
$posmsg = "${resetcolor}$($myinv.ScriptName):$($myinv.ScriptLineNumber):$($myinv.OffsetInLine)${newline}"
41+
$posmsg = "${resetcolor}$($Invocation.ScriptName):$($Invocation.ScriptLineNumber):$($Invocation.OffsetInLine)${newline}"
4242
} else {
43-
$posmsg = "${resetcolor}$($myinv.ScriptName):$($myinv.ScriptLineNumber)${newline}"
43+
$posmsg = "${resetcolor}$($Invocation.ScriptName):$($Invocation.ScriptLineNumber)${newline}"
4444
}
4545
} else {
4646
$posmsg = "${newline}"
4747
}
4848

4949
if ($useTargetObject) {
50-
$scriptLineNumber = $err.TargetObject.Line
51-
$scriptLineNumberLength = $err.TargetObject.Line.ToString().Length
50+
$scriptLineNumber = $InputObject.TargetObject.Line
51+
$scriptLineNumberLength = $InputObject.TargetObject.Line.ToString().Length
5252
} else {
53-
$scriptLineNumber = $myinv.ScriptLineNumber
54-
$scriptLineNumberLength = $myinv.ScriptLineNumber.ToString().Length
53+
$scriptLineNumber = $Invocation.ScriptLineNumber
54+
$scriptLineNumberLength = $Invocation.ScriptLineNumber.ToString().Length
5555
}
5656

5757
if ($scriptLineNumberLength -gt 4) {
@@ -72,7 +72,7 @@ filter GetConciseMessage {
7272
$offsetLength = 0
7373
$offsetInLine = 0
7474
} else {
75-
$positionMessage = $myinv.PositionMessage.Split($newline)
75+
$positionMessage = $Invocation.PositionMessage.Split($newline)
7676
$line = $positionMessage[1].Substring(1) # skip the '+' at the start
7777
$highlightLine = $positionMessage[$positionMessage.Count - 1].Substring(1)
7878
$offsetLength = $highlightLine.Trim().Length
@@ -96,23 +96,23 @@ filter GetConciseMessage {
9696
$message = "${prefix}"
9797
}
9898

99-
if (! $err.ErrorDetails -or ! $err.ErrorDetails.Message) {
100-
if ($err.CategoryInfo.Category -eq 'ParserError' -and $err.Exception.Message.Contains("~$newline")) {
99+
if (! $InputObject.ErrorDetails -or ! $InputObject.ErrorDetails.Message) {
100+
if ($InputObject.CategoryInfo.Category -eq 'ParserError' -and $InputObject.Exception.Message.Contains("~$newline")) {
101101
# need to parse out the relevant part of the pre-rendered positionmessage
102-
$message += $err.Exception.Message.split("~$newline")[1].split("${newline}${newline}")[0]
103-
} elseif ($err.Exception) {
104-
$message += $err.Exception.Message
105-
} elseif ($err.Message) {
106-
$message += $err.Message
102+
$message += $InputObject.Exception.Message.split("~$newline")[1].split("${newline}${newline}")[0]
103+
} elseif ($InputObject.Exception) {
104+
$message += $InputObject.Exception.Message
105+
} elseif ($InputObject.Message) {
106+
$message += $InputObject.Message
107107
} else {
108-
$message += $err.ToString()
108+
$message += $InputObject.ToString()
109109
}
110110
} else {
111-
$message += $err.ErrorDetails.Message
111+
$message += $InputObject.ErrorDetails.Message
112112
}
113113

114114
# if rendering line information, break up the message if it's wider than the console
115-
if ($myinv -and $myinv.ScriptName -or $err.CategoryInfo.Category -eq 'ParserError') {
115+
if ($Invocation -and $Invocation.ScriptName -or $InputObject.CategoryInfo.Category -eq 'ParserError') {
116116
$prefixLength = [System.Management.Automation.Internal.StringDecorated]::new($prefix).ContentLength
117117
$prefixVtLength = $prefix.Length - $prefixLength
118118

@@ -152,22 +152,22 @@ filter GetConciseMessage {
152152
$posmsg += "${errorColor}" + $message
153153

154154
$reason = 'Error'
155-
if ($err.Exception -and $err.Exception.WasThrownFromThrowStatement) {
155+
if ($InputObject.Exception -and $InputObject.Exception.WasThrownFromThrowStatement) {
156156
$reason = 'Exception'
157157
# MyCommand can be the script block, so we don't want to show that so check if it's an actual command
158-
} elseif ($myinv.MyCommand -and $myinv.MyCommand.Name -and (Get-Command -Name $myinv.MyCommand -ErrorAction Ignore)) {
159-
$reason = $myinv.MyCommand
160-
} elseif ($err.CategoryInfo.Activity) {
158+
} elseif ($Invocation.MyCommand -and $Invocation.MyCommand.Name -and (Get-Command -Name $Invocation.MyCommand -ErrorAction Ignore)) {
159+
$reason = $Invocation.MyCommand
160+
} elseif ($InputObject.CategoryInfo.Activity) {
161161
# If it's a scriptblock, better to show the command in the scriptblock that had the error
162-
$reason = $err.CategoryInfo.Activity
163-
} elseif ($myinv.MyCommand) {
164-
$reason = $myinv.MyCommand
165-
} elseif ($myinv.InvocationName) {
166-
$reason = $myinv.InvocationName
167-
} elseif ($err.CategoryInfo.Category) {
168-
$reason = $err.CategoryInfo.Category
169-
} elseif ($err.CategoryInfo.Reason) {
170-
$reason = $err.CategoryInfo.Reason
162+
$reason = $InputObject.CategoryInfo.Activity
163+
} elseif ($Invocation.MyCommand) {
164+
$reason = $Invocation.MyCommand
165+
} elseif ($Invocation.InvocationName) {
166+
$reason = $Invocation.InvocationName
167+
} elseif ($InputObject.CategoryInfo.Category) {
168+
$reason = $InputObject.CategoryInfo.Category
169+
} elseif ($InputObject.CategoryInfo.Reason) {
170+
$reason = $InputObject.CategoryInfo.Reason
171171
}
172172

173173
"${errorColor}${reason}: ${posmsg}${resetcolor}"

source/public/Get-ErrorPrefix.ps1 renamed to source/private/GetErrorPrefix.ps1

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
filter Get-ErrorPrefix {
1+
filter GetErrorPrefix {
22
[CmdletBinding()]
33
param(
44
[Parameter(ValueFromPipeline)]
55
$InputObject
66
)
77
if (@('NativeCommandErrorMessage', 'NativeCommandError') -notcontains $_.FullyQualifiedErrorId) {
88
if ($InputObject -is [System.Exception]) {
9-
$InputObject.GetType().FullName + " : "
9+
$InputObject.GetType().FullName + ' : '
1010
} else {
1111
$myinv = $InputObject.InvocationInfo
1212
if ($myinv -and $myinv.MyCommand) {
@@ -15,15 +15,13 @@ filter Get-ErrorPrefix {
1515
if ($myinv.MyCommand.Path) {
1616
$myinv.MyCommand.Path + ' : '
1717
}
18-
1918
break
2019
}
2120

2221
([System.Management.Automation.CommandTypes]::Script) {
2322
if ($myinv.MyCommand.ScriptBlock) {
2423
$myinv.MyCommand.ScriptBlock.ToString() + ' : '
2524
}
26-
2725
break
2826
}
2927
default {
@@ -34,7 +32,6 @@ filter Get-ErrorPrefix {
3432
} else {
3533
$myinv.InvocationName + ' : '
3634
}
37-
3835
break
3936
}
4037
}

source/private/GetListRecursive.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,4 @@ function GetListRecursive {
163163
}
164164

165165
$output.ToString()
166-
Write-Information "EXIT GetListRecursive END $($InputObject.GetType().FullName) $indent $depth (of $maxDepth)" -Tags 'Trace', 'Enter', 'GetListRecursive'
167166
}

0 commit comments

Comments
 (0)