MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4param(5 [parameter(Mandatory)]6 [string]$Name,7 [parameter(Mandatory)]8 [string]$TestResultsFolder9)10 11Import-Module "$PSScriptRoot/../../../../build.psm1"12 13if (-not $env:GITHUB_STEP_SUMMARY) {14 Write-Error "GITHUB_STEP_SUMMARY is not set. Ensure this workflow is running in a GitHub Actions environment."15 exit 116}17 18$testCaseCount = 019$testErrorCount = 020$testFailureCount = 021$testNotRunCount = 022$testInconclusiveCount = 023$testIgnoredCount = 024$testSkippedCount = 025$testInvalidCount = 026 27# Process test results and generate annotations for failures28Get-ChildItem -Path "${TestResultsFolder}/*.xml" -Recurse | ForEach-Object {29 $results = [xml] (get-content $_.FullName)30 31 $testCaseCount += [int]$results.'test-results'.total32 $testErrorCount += [int]$results.'test-results'.errors33 $testFailureCount += [int]$results.'test-results'.failures34 $testNotRunCount += [int]$results.'test-results'.'not-run'35 $testInconclusiveCount += [int]$results.'test-results'.inconclusive36 $testIgnoredCount += [int]$results.'test-results'.ignored37 $testSkippedCount += [int]$results.'test-results'.skipped38 $testInvalidCount += [int]$results.'test-results'.invalid39 40 # Generate GitHub Actions annotations for test failures41 # Select failed test cases42 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.description51 $testName = $testfail.name52 $message = $testfail.failure.message53 $stack_trace = $testfail.failure.'stack-trace'54 55 # Parse stack trace to get file and line info56 $fileInfo = Get-PesterFailureFileInfo -StackTraceString $stack_trace57 58 if ($fileInfo.File) {59 # Convert absolute path to relative path for GitHub Actions60 $filePath = $fileInfo.File61 62 # GitHub Actions expects paths relative to the workspace root63 if ($env:GITHUB_WORKSPACE) {64 $workspacePath = $env:GITHUB_WORKSPACE65 if ($filePath.StartsWith($workspacePath)) {66 $filePath = $filePath.Substring($workspacePath.Length).TrimStart('/', '\')67 # Normalize to forward slashes for consistency68 $filePath = $filePath -replace '\\', '/'69 }70 }71 72 # Create annotation title73 $annotationTitle = "Test Failure: $description / $testName"74 75 # Build the annotation message76 $annotationMessage = $message -replace "`n", "%0A" -replace "`r"77 78 # Build and output the workflow command79 $workflowCommand = "::error file=$filePath"80 if ($fileInfo.Line) {81 $workflowCommand += ",line=$($fileInfo.Line)"82 }83 $workflowCommand += ",title=$annotationTitle::$annotationMessage"84 85 Write-Host $workflowCommand86 87 # Output a link to the test run88 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 }94}95 96@"97 98# Summary of $Name99 100- Total Tests: $testCaseCount101- Total Errors: $testErrorCount102- Total Failures: $testFailureCount103- Total Not Run: $testNotRunCount104- Total Inconclusive: $testInconclusiveCount105- Total Ignored: $testIgnoredCount106- Total Skipped: $testSkippedCount107- Total Invalid: $testInvalidCount108 109"@ | Out-File -FilePath $ENV:GITHUB_STEP_SUMMARY -Append110 111Write-Log "Summary written to $ENV:GITHUB_STEP_SUMMARY"112 113Write-LogGroupStart -Title 'Test Results'114Get-Content $ENV:GITHUB_STEP_SUMMARY115Write-LogGroupEnd -Title 'Test Results'116 117if ($testErrorCount -gt 0 -or $testFailureCount -gt 0) {118 Write-Error "There were $testErrorCount/$testFailureCount errors/failures in the test results."119 exit 1120}121if ($testCaseCount -eq 0) {122 Write-Error "No test cases were run."123 exit 1124}125 