Skip to content

Commit b490e52

Browse files
CopilotTravisEz13
andcommitted
Move annotation generation to process-pester-results.ps1
- Moved GitHub Actions annotation logic from Show-PSPesterError to process-pester-results.ps1 - process-pester-results.ps1 is the central place where test results are processed in CI - Removed redundant GITHUB_WORKFLOW check (already in workflow context) - Show-PSPesterError now only displays error info (backward compatible) - Get-PesterFailureFileInfo remains in build.psm1 for reuse Co-authored-by: TravisEz13 <[email protected]>
1 parent 3138c65 commit b490e52

File tree

2 files changed

+56
-41
lines changed

2 files changed

+56
-41
lines changed

.github/actions/test/process-pester-results/process-pester-results.ps1

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ $testIgnoredCount = 0
2424
$testSkippedCount = 0
2525
$testInvalidCount = 0
2626

27+
# Process test results and generate annotations for failures
2728
Get-ChildItem -Path "${TestResultsFolder}/*.xml" -Recurse | ForEach-Object {
2829
$results = [xml] (get-content $_.FullName)
2930

@@ -35,6 +36,61 @@ Get-ChildItem -Path "${TestResultsFolder}/*.xml" -Recurse | ForEach-Object {
3536
$testIgnoredCount += [int]$results.'test-results'.ignored
3637
$testSkippedCount += [int]$results.'test-results'.skipped
3738
$testInvalidCount += [int]$results.'test-results'.invalid
39+
40+
# Generate GitHub Actions annotations for test failures
41+
# Select failed test cases
42+
if ("System.Xml.XmlDocumentXPathExtensions" -as [Type]) {
43+
$failures = [System.Xml.XmlDocumentXPathExtensions]::SelectNodes($results.'test-results', './/test-case[@result = "Failure"]')
44+
}
45+
else {
46+
$failures = $results.SelectNodes('.//test-case[@result = "Failure"]')
47+
}
48+
49+
foreach ($testfail in $failures) {
50+
$description = $testfail.description
51+
$testName = $testfail.name
52+
$message = $testfail.failure.message
53+
$stack_trace = $testfail.failure.'stack-trace'
54+
55+
# Parse stack trace to get file and line info
56+
$fileInfo = Get-PesterFailureFileInfo -StackTraceString $stack_trace
57+
58+
if ($fileInfo.File) {
59+
# Convert absolute path to relative path for GitHub Actions
60+
$filePath = $fileInfo.File
61+
62+
# GitHub Actions expects paths relative to the workspace root
63+
if ($env:GITHUB_WORKSPACE) {
64+
$workspacePath = $env:GITHUB_WORKSPACE
65+
if ($filePath.StartsWith($workspacePath)) {
66+
$filePath = $filePath.Substring($workspacePath.Length).TrimStart('/', '\')
67+
# Normalize to forward slashes for consistency
68+
$filePath = $filePath -replace '\\', '/'
69+
}
70+
}
71+
72+
# Create annotation title
73+
$annotationTitle = "Test Failure: $description / $testName"
74+
75+
# Build the annotation message
76+
$annotationMessage = $message -replace "`n", "%0A" -replace "`r"
77+
78+
# Build and output the workflow command
79+
$workflowCommand = "::error file=$filePath"
80+
if ($fileInfo.Line) {
81+
$workflowCommand += ",line=$($fileInfo.Line)"
82+
}
83+
$workflowCommand += ",title=$annotationTitle::$annotationMessage"
84+
85+
Write-Host $workflowCommand
86+
87+
# Output a link to the test run
88+
if ($env:GITHUB_SERVER_URL -and $env:GITHUB_REPOSITORY -and $env:GITHUB_RUN_ID) {
89+
$logUrl = "$($env:GITHUB_SERVER_URL)/$($env:GITHUB_REPOSITORY)/actions/runs/$($env:GITHUB_RUN_ID)"
90+
Write-Host "Test logs: $logUrl"
91+
}
92+
}
93+
}
3894
}
3995

4096
@"

build.psm1

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,47 +1862,6 @@ $stack_trace
18621862
18631863
"@
18641864

1865-
# If we're in a GitHub workflow, add an annotation with file location
1866-
if ($env:GITHUB_WORKFLOW) {
1867-
$fileInfo = Get-PesterFailureFileInfo -StackTraceString $stack_trace
1868-
1869-
if ($fileInfo.File) {
1870-
# Convert absolute path to relative path for GitHub Actions
1871-
$filePath = $fileInfo.File
1872-
1873-
# GitHub Actions expects paths relative to the workspace root
1874-
if ($env:GITHUB_WORKSPACE) {
1875-
$workspacePath = $env:GITHUB_WORKSPACE
1876-
if ($filePath.StartsWith($workspacePath)) {
1877-
$filePath = $filePath.Substring($workspacePath.Length).TrimStart('/', '\')
1878-
# Normalize to forward slashes for consistency
1879-
$filePath = $filePath -replace '\\', '/'
1880-
}
1881-
}
1882-
1883-
# Create annotation title
1884-
$annotationTitle = "Test Failure: $description / $name"
1885-
1886-
# Build the annotation message
1887-
$annotationMessage = $message -replace "`n", "%0A" -replace "`r"
1888-
1889-
# Build the workflow command
1890-
$workflowCommand = "::error file=$filePath"
1891-
if ($fileInfo.Line) {
1892-
$workflowCommand += ",line=$($fileInfo.Line)"
1893-
}
1894-
$workflowCommand += ",title=$annotationTitle::$annotationMessage"
1895-
1896-
Write-Host $workflowCommand
1897-
1898-
# If available, also log a link to the test run
1899-
if ($env:GITHUB_SERVER_URL -and $env:GITHUB_REPOSITORY -and $env:GITHUB_RUN_ID) {
1900-
$logUrl = "$($env:GITHUB_SERVER_URL)/$($env:GITHUB_REPOSITORY)/actions/runs/$($env:GITHUB_RUN_ID)"
1901-
Write-Host "Test logs: $logUrl"
1902-
}
1903-
}
1904-
}
1905-
19061865
}
19071866

19081867
function Get-PesterFailureFileInfo

0 commit comments

Comments
 (0)