Skip to content

Commit 52e6e64

Browse files
Refactor: Enhance expected test suite validation and improve error handling for missing result files
1 parent 9ad597e commit 52e6e64

1 file changed

Lines changed: 87 additions & 68 deletions

File tree

scripts/main.ps1

Lines changed: 87 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,6 @@ $PSStyle.OutputRendering = 'Ansi'
77
$repo = $env:GITHUB_REPOSITORY
88
$runId = $env:GITHUB_RUN_ID
99

10-
LogGroup 'List test suites' {
11-
$sourceCodeTestSuites = $env:PSMODULE_GET_PESTERTESTRESULTS_INPUT_SourceCodeTestSuites | ConvertFrom-Json
12-
$psModuleTestSuites = $env:PSMODULE_GET_PESTERTESTRESULTS_INPUT_PSModuleTestSuites | ConvertFrom-Json
13-
$moduleTestSuites = $env:PSMODULE_GET_PESTERTESTRESULTS_INPUT_ModuleTestSuites | ConvertFrom-Json
14-
15-
[pscustomobject]@{
16-
SourceCodeTestSuites = $sourceCodeTestSuites
17-
PSModuleTestSuites = $psModuleTestSuites
18-
ModuleTestSuites = $moduleTestSuites
19-
} | Format-List | Out-String
20-
}
21-
2210
$testResultsFolder = New-Item -Path . -ItemType Directory -Name 'TestResults' -Force
2311
gh run download $runId --repo $repo --pattern *-TestResults --dir TestResults
2412
$files = Get-ChildItem -Path $testResultsFolder -Recurse -File -Filter *.json | Sort-Object Name
@@ -27,74 +15,114 @@ LogGroup 'List files' {
2715
$files.Name | Out-String
2816
}
2917

30-
# Validate that all expected artifact files are present.
31-
$expectedFiles = @()
18+
LogGroup 'Expected test suites' {
19+
$sourceCodeTestSuites = $env:PSMODULE_GET_PESTERTESTRESULTS_INPUT_SourceCodeTestSuites | ConvertFrom-Json
20+
$psModuleTestSuites = $env:PSMODULE_GET_PESTERTESTRESULTS_INPUT_PSModuleTestSuites | ConvertFrom-Json
21+
$moduleTestSuites = $env:PSMODULE_GET_PESTERTESTRESULTS_INPUT_ModuleTestSuites | ConvertFrom-Json
22+
23+
# Build an array of expected test suite objects
24+
$expectedTestSuites = @()
3225

33-
# Expected files from SourceCodeTestSuites: files prefixed with 'SourceCode-'
26+
# SourceCodeTestSuites: expected file names start with "SourceCode-"
3427
foreach ($suite in $sourceCodeTestSuites) {
35-
$expectedFiles += "SourceCode-$($suite.OSName)-TestResult-Report.json"
28+
$expectedTestSuites += [pscustomobject]@{
29+
FileName = "SourceCode-$($suite.OSName)-TestResult-Report.json"
30+
Category = 'SourceCode'
31+
OSName = $suite.OSName
32+
TestName = $null
33+
}
3634
}
35+
36+
# PSModuleTestSuites: expected file names start with "Module-"
3737
foreach ($suite in $psModuleTestSuites) {
38-
$expectedFiles += "Module-$($suite.OSName)-TestResult-Report.json"
38+
$expectedTestSuites += [pscustomobject]@{
39+
FileName = "Module-$($suite.OSName)-TestResult-Report.json"
40+
Category = 'PSModuleTest'
41+
OSName = $suite.OSName
42+
TestName = $null
43+
}
3944
}
45+
46+
# ModuleTestSuites: expected file names use the TestName as prefix
4047
foreach ($suite in $moduleTestSuites) {
41-
$expectedFiles += "$($suite.TestName)-$($suite.OSName)-TestResult-Report.json"
48+
$expectedTestSuites += [pscustomobject]@{
49+
FileName = "$($suite.TestName)-$($suite.OSName)-TestResult-Report.json"
50+
Category = 'ModuleTest'
51+
OSName = $suite.OSName
52+
TestName = $suite.TestName
53+
}
4254
}
4355

44-
# Remove any duplicates if present.
45-
$expectedFiles = $expectedFiles | Select-Object -Unique
56+
# Remove duplicates if any
57+
$expectedTestSuites = $expectedTestSuites | Select-Object -Unique
4658

47-
# Check for missing files.
48-
$missingFiles = $expectedFiles | Where-Object { $files.Name -notcontains $_ }
49-
if ($missingFiles.Count -gt 0) {
50-
Write-GitHubError "Missing expected test result files: $($missingFiles -join ', ')"
51-
exit 1
59+
$expectedTestSuites | Format-Table | Out-String
5260
}
5361

5462
$testResults = [System.Collections.Generic.List[psobject]]::new()
5563
$failedTests = [System.Collections.Generic.List[psobject]]::new()
5664
$unexecutedTests = [System.Collections.Generic.List[psobject]]::new()
5765
$totalErrors = 0
5866

59-
foreach ($file in $files) {
60-
$fileName = $file.BaseName
61-
$content = Get-Content -Path $file
62-
$object = $content | ConvertFrom-Json
63-
$testResults.Add($object)
64-
65-
$result = [pscustomobject]@{
66-
Tests = [int]([math]::Round(($object | Measure-Object -Sum -Property TotalCount).Sum))
67-
Passed = [int]([math]::Round(($object | Measure-Object -Sum -Property PassedCount).Sum))
68-
Failed = [int]([math]::Round(($object | Measure-Object -Sum -Property FailedCount).Sum))
69-
NotRun = [int]([math]::Round(($object | Measure-Object -Sum -Property NotRunCount).Sum))
70-
Inconclusive = [int]([math]::Round(($object | Measure-Object -Sum -Property InconclusiveCount).Sum))
71-
Skipped = [int]([math]::Round(($object | Measure-Object -Sum -Property SkippedCount).Sum))
67+
foreach ($expected in $expectedTestSuites) {
68+
$filePath = Join-Path $testResultsFolder.FullName $expected.FileName
69+
if (Test-Path $filePath) {
70+
$content = Get-Content -Path $filePath
71+
$object = $content | ConvertFrom-Json
72+
$result = [pscustomobject]@{
73+
Tests = [int]([math]::Round(($object | Measure-Object -Sum -Property TotalCount).Sum))
74+
Passed = [int]([math]::Round(($object | Measure-Object -Sum -Property PassedCount).Sum))
75+
Failed = [int]([math]::Round(($object | Measure-Object -Sum -Property FailedCount).Sum))
76+
NotRun = [int]([math]::Round(($object | Measure-Object -Sum -Property NotRunCount).Sum))
77+
Inconclusive = [int]([math]::Round(($object | Measure-Object -Sum -Property InconclusiveCount).Sum))
78+
Skipped = [int]([math]::Round(($object | Measure-Object -Sum -Property SkippedCount).Sum))
79+
ResultFilePresent = $true
80+
}
81+
} else {
82+
$result = [pscustomobject]@{
83+
Tests = $null
84+
Passed = $null
85+
Failed = $null
86+
NotRun = $null
87+
Inconclusive = $null
88+
Skipped = $null
89+
ResultFilePresent = $false
90+
}
91+
Write-GitHubError "Missing expected test result file: $($expected.FileName)"
92+
$totalErrors++
7293
}
7394

74-
$failed = (
75-
$result.Failed -gt 0 -or
76-
$result.NotRun -gt 0 -or
77-
$result.Inconclusive -gt 0 -or
78-
$object.Result -eq 'Failed' -or $object.Executed -eq $false
79-
)
80-
$color = $failed ? $PSStyle.Foreground.Red : $PSStyle.Foreground.Green
95+
# Determine if there’s any failure: missing file or non-successful test counts
96+
$isFailure = ($result.ResultFilePresent -eq $false) -or
97+
($result.Failed -gt 0) -or
98+
($result.NotRun -gt 0) -or
99+
($result.Inconclusive -gt 0)
100+
$color = $isFailure ? $PSStyle.Foreground.Red : $PSStyle.Foreground.Green
81101
$reset = $PSStyle.Reset
82-
$logGroupName = $fileName.Replace('-TestResult-Report', '')
102+
$logGroupName = $expected.FileName -replace '-TestResult-Report', ''
103+
83104
LogGroup " - $color$logGroupName$reset" {
84-
$object | Format-List | Out-String
85-
if ($object.Executed -eq $false) {
86-
$unexecutedTests.Add($file)
87-
Write-GitHubError "Test was not executed as reported in file: $($file.Name)"
88-
$totalErrors++
89-
} else {
90-
if ($object.Result -eq 'Failed') {
91-
$failedTests.Add($file)
92-
Write-GitHubError "Test result explicitly marked as Failed in file: $($file.Name)"
105+
if ($result.ResultFilePresent) {
106+
# Output detailed results from the file.
107+
$object | Format-List | Out-String
108+
if ($object.Executed -eq $false) {
109+
$unexecutedTests.Add($expected.FileName)
110+
Write-GitHubError "Test was not executed as reported in file: $($expected.FileName)"
111+
$totalErrors++
112+
} elseif ($object.Result -eq 'Failed') {
113+
$failedTests.Add($expected.FileName)
114+
Write-GitHubError "Test result explicitly marked as Failed in file: $($expected.FileName)"
93115
$totalErrors++
94116
}
95117
$result | Format-Table | Out-String
118+
} else {
119+
Write-Host "Test result file not found for: $($expected.FileName)"
96120
}
97121
}
122+
123+
if ($result.ResultFilePresent) {
124+
$testResults.Add($object)
125+
}
98126
}
99127

100128
Write-Output ('' * 50)
@@ -107,39 +135,30 @@ $total = [pscustomobject]@{
107135
Skipped = [int]([math]::Round(($testResults | Measure-Object -Sum -Property SkippedCount).Sum))
108136
}
109137

110-
$failed = (
111-
$total.Failed -gt 0 -or
112-
$total.NotRun -gt 0 -or
113-
$total.Inconclusive -gt 0 -or
114-
$totalErrors -gt 0
115-
)
116-
$color = $failed ? $PSStyle.Foreground.Red : $PSStyle.Foreground.Green
138+
$overallFailure = ($total.Failed -gt 0) -or ($total.NotRun -gt 0) -or ($total.Inconclusive -gt 0) -or ($totalErrors -gt 0)
139+
$color = $overallFailure ? $PSStyle.Foreground.Red : $PSStyle.Foreground.Green
117140
$reset = $PSStyle.Reset
118141
LogGroup " - $color`Summary$reset" {
119142
$total | Format-Table | Out-String
120143
if ($total.Failed -gt 0) {
121144
Write-GitHubError "There are $($total.Failed) failed tests of $($total.Tests) tests"
122145
$totalErrors += $total.Failed
123146
}
124-
125147
if ($total.NotRun -gt 0) {
126148
Write-GitHubError "There are $($total.NotRun) tests not run of $($total.Tests) tests"
127149
$totalErrors += $total.NotRun
128150
}
129-
130151
if ($total.Inconclusive -gt 0) {
131152
Write-GitHubError "There are $($total.Inconclusive) inconclusive tests of $($total.Tests) tests"
132153
$totalErrors += $total.Inconclusive
133154
}
134-
135155
if ($failedTests.Count -gt 0) {
136156
Write-Host 'Failed Test Files'
137-
$failedTests.Name | ForEach-Object { Write-Host " - $_" }
157+
$failedTests | ForEach-Object { Write-Host " - $_" }
138158
}
139-
140159
if ($unexecutedTests.Count -gt 0) {
141160
Write-Host 'Unexecuted Test Files'
142-
$unexecutedTests.Name | ForEach-Object { Write-Host " - $_" }
161+
$unexecutedTests | ForEach-Object { Write-Host " - $_" }
143162
}
144163
}
145164

0 commit comments

Comments
 (0)