-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ps1
More file actions
236 lines (203 loc) · 9.35 KB
/
main.ps1
File metadata and controls
236 lines (203 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#Requires -Modules GitHub
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSAvoidUsingWriteHost', '',
Justification = 'Outputs to GitHub Actions logs.'
)]
[CmdletBinding()]
param()
$owner = $env:GITHUB_REPOSITORY_OWNER
$repo = $env:GITHUB_REPOSITORY_NAME
$runId = $env:GITHUB_RUN_ID
$files = Get-GitHubArtifact -Owner $owner -Repository $repo -WorkflowRunID $runId -Name '*-TestResults' |
Save-GitHubArtifact -Path 'TestResults' -Force -Expand -PassThru | Get-ChildItem -Recurse -Filter *.json | Sort-Object Name -Unique
LogGroup 'List files' {
$files.Name | Out-String
}
$sourceCodeTestSuites = $env:PSMODULE_GET_PESTERTESTRESULTS_INPUT_SourceCodeTestSuites | ConvertFrom-Json
$psModuleTestSuites = $env:PSMODULE_GET_PESTERTESTRESULTS_INPUT_PSModuleTestSuites | ConvertFrom-Json
$moduleTestSuites = $env:PSMODULE_GET_PESTERTESTRESULTS_INPUT_ModuleTestSuites | ConvertFrom-Json
LogGroup 'Expected test suites' {
# Build an array of expected test suite objects
$expectedTestSuites = @()
# SourceCodeTestSuites: expected file names start with "SourceCode-"
foreach ($suite in $sourceCodeTestSuites) {
$expectedTestSuites += [pscustomobject]@{
Name = "PSModuleTest-SourceCode-$($suite.OSName)-TestResult-Report"
Category = 'SourceCode'
OSName = $suite.OSName
TestName = $null
}
$expectedTestSuites += [pscustomobject]@{
Name = "PSModuleLint-SourceCode-$($suite.OSName)-TestResult-Report"
Category = 'SourceCode'
OSName = $suite.OSName
TestName = $null
}
}
# PSModuleTestSuites: expected file names start with "Module-"
foreach ($suite in $psModuleTestSuites) {
$expectedTestSuites += [pscustomobject]@{
Name = "PSModuleTest-Module-$($suite.OSName)-TestResult-Report"
Category = 'PSModuleTest'
OSName = $suite.OSName
TestName = $null
}
$expectedTestSuites += [pscustomobject]@{
Name = "PSModuleLint-Module-$($suite.OSName)-TestResult-Report"
Category = 'PSModuleTest'
OSName = $suite.OSName
TestName = $null
}
}
# ModuleTestSuites: expected file names use the TestName as prefix
foreach ($suite in $moduleTestSuites) {
$expectedTestSuites += [pscustomobject]@{
Name = "$($suite.TestName)-$($suite.OSName)-TestResult-Report"
Category = 'ModuleTest'
OSName = $suite.OSName
TestName = $suite.TestName
}
}
$expectedTestSuites = $expectedTestSuites | Sort-Object Category, Name
$expectedTestSuites | Format-Table | Out-String
}
$isFailure = $false
$testResults = [System.Collections.Generic.List[psobject]]::new()
$failedTests = [System.Collections.Generic.List[string]]::new()
$unexecutedTests = [System.Collections.Generic.List[string]]::new()
$missingResultFiles = [System.Collections.Generic.List[string]]::new()
$totalErrors = 0
foreach ($expected in $expectedTestSuites) {
$file = $files | Where-Object { $_.BaseName -eq $expected.Name }
$result = if ($file) {
$object = $file | Get-Content | ConvertFrom-Json
[pscustomobject]@{
Result = $object.Result
Executed = $object.Executed
ResultFilePresent = $true
Tests = [int]([math]::Round(($object | Measure-Object -Sum -Property TotalCount).Sum))
Passed = [int]([math]::Round(($object | Measure-Object -Sum -Property PassedCount).Sum))
Failed = [int]([math]::Round(($object | Measure-Object -Sum -Property FailedCount).Sum))
NotRun = [int]([math]::Round(($object | Measure-Object -Sum -Property NotRunCount).Sum))
Inconclusive = [int]([math]::Round(($object | Measure-Object -Sum -Property InconclusiveCount).Sum))
Skipped = [int]([math]::Round(($object | Measure-Object -Sum -Property SkippedCount).Sum))
}
} else {
[pscustomobject]@{
Result = $null
Executed = $null
ResultFilePresent = $false
Tests = $null
Passed = $null
Failed = $null
NotRun = $null
Inconclusive = $null
Skipped = $null
}
}
# Determine if there’s any failure for this single test file
$hasTestsValue = $null -ne $result.Tests
$hasPassedValue = $null -ne $result.Passed
$hasFailedValue = $null -ne $result.Failed
$hasInconclusiveValue = $null -ne $result.Inconclusive
$hasNotRunValue = $null -ne $result.NotRun
$testFailure = (
$result.Result -ne 'Passed' -or
$result.Executed -ne $true -or
$result.ResultFilePresent -eq $false -or
($hasTestsValue -and $result.Tests -eq 0) -or
($hasPassedValue -and $hasTestsValue -and $result.Tests -gt 0 -and $result.Passed -eq 0) -or
($hasFailedValue -and $result.Failed -gt 0) -or
($hasInconclusiveValue -and $result.Inconclusive -gt 0) -or
($hasNotRunValue -and $result.NotRun -gt 0)
)
if ($testFailure) {
$conclusion = 'Failed'
$color = $PSStyle.Foreground.Red
$isFailure = $true
} else {
$conclusion = 'Passed'
$color = $PSStyle.Foreground.Green
}
$result | Add-Member -NotePropertyName 'Conclusion' -NotePropertyValue $conclusion
$reset = $PSStyle.Reset
$logGroupName = $expected.Name -replace '-TestResult-Report.*', ''
LogGroup " - $color$logGroupName$reset" {
$failureReasons = [System.Collections.Generic.List[string]]::new()
if ($result.ResultFilePresent -eq $false) {
$missingResultFiles.Add($expected.Name)
$null = $failureReasons.Add("Result file '$($expected.Name)' was not found in the artifacts.")
}
if ($result.Executed -eq $false) {
$unexecutedTests.Add($expected.Name)
$null = $failureReasons.Add("Test execution flag is false in file: $($expected.Name)")
}
if ($result.Result -and $result.Result -ne 'Passed') {
if ($failedTests -notcontains $expected.Name) {
$failedTests.Add($expected.Name)
}
$null = $failureReasons.Add("Test result value '$($result.Result)' indicates failure in file: $($expected.Name)")
}
if ($hasTestsValue -and $result.Tests -eq 0) {
$null = $failureReasons.Add("No tests were reported in file: $($expected.Name)")
}
if ($hasPassedValue -and $hasTestsValue -and $result.Tests -gt 0 -and $result.Passed -eq 0) {
$null = $failureReasons.Add("No tests passed according to file: $($expected.Name)")
}
if ($hasFailedValue -and $result.Failed -gt 0) {
if ($failedTests -notcontains $expected.Name) {
$failedTests.Add($expected.Name)
}
$null = $failureReasons.Add("$($result.Failed) tests failed in file: $($expected.Name)")
}
if ($hasInconclusiveValue -and $result.Inconclusive -gt 0) {
$null = $failureReasons.Add("$($result.Inconclusive) tests were inconclusive in file: $($expected.Name)")
}
if ($hasNotRunValue -and $result.NotRun -gt 0) {
$null = $failureReasons.Add("$($result.NotRun) tests were not run in file: $($expected.Name)")
}
foreach ($reason in $failureReasons) {
Write-GitHubError $reason
$totalErrors++
}
Write-Host "$($result | Format-Table | Out-String)"
}
if ($result.ResultFilePresent) {
$testResults.Add($result)
}
}
Write-Output ('─' * 50)
$total = [pscustomobject]@{
Tests = [int]([math]::Round(($testResults | Measure-Object -Sum -Property Tests).Sum))
Passed = [int]([math]::Round(($testResults | Measure-Object -Sum -Property Passed).Sum))
Failed = [int]([math]::Round(($testResults | Measure-Object -Sum -Property Failed).Sum))
NotRun = [int]([math]::Round(($testResults | Measure-Object -Sum -Property NotRun).Sum))
Inconclusive = [int]([math]::Round(($testResults | Measure-Object -Sum -Property Inconclusive).Sum))
Skipped = [int]([math]::Round(($testResults | Measure-Object -Sum -Property Skipped).Sum))
}
$color = if ($isFailure) { $PSStyle.Foreground.Red } else { $PSStyle.Foreground.Green }
$reset = $PSStyle.Reset
LogGroup " - $color`Summary$reset" {
$total | Format-Table | Out-String
if ($total.Failed -gt 0) {
Write-GitHubError "There are $($total.Failed) failed tests of $($total.Tests) tests"
$totalErrors += $total.Failed
}
if ($total.Inconclusive -gt 0) {
Write-GitHubError "There are $($total.Inconclusive) inconclusive tests of $($total.Tests) tests"
$totalErrors += $total.Inconclusive
}
if ($failedTests.Count -gt 0) {
Write-Host 'Failed Test Files'
$failedTests | ForEach-Object { Write-Host " - $_" }
}
if ($unexecutedTests.Count -gt 0) {
Write-Host 'Unexecuted Test Files'
$unexecutedTests | ForEach-Object { Write-Host " - $_" }
}
if ($missingResultFiles.Count -gt 0) {
Write-Host 'Missing Test Result Files'
$missingResultFiles | ForEach-Object { Write-Host " - $_" }
}
}
exit $totalErrors