|
| 1 | +#!/usr/bin/env pwsh |
| 2 | + |
| 3 | +#Requires -Version 7.0 |
| 4 | +<# |
| 5 | +.SYNOPSIS |
| 6 | +Converts cargo test JSON output to JUnit XML format using cargo2junit. |
| 7 | +
|
| 8 | +.DESCRIPTION |
| 9 | +This script converts the JSON output files from cargo test (captured by Test-Packages.ps1 in CI mode) |
| 10 | +to JUnit XML format suitable for publishing to Azure DevOps test results using the cargo2junit tool. |
| 11 | +
|
| 12 | +.PARAMETER TestResultsDirectory |
| 13 | +The directory containing JSON test result files. Defaults to test-results in the repo root. |
| 14 | +
|
| 15 | +.PARAMETER OutputDirectory |
| 16 | +The directory where JUnit XML files should be written. Defaults to test-results/junit in the repo root. |
| 17 | +
|
| 18 | +.EXAMPLE |
| 19 | +./eng/scripts/Convert-TestResultsToJUnit.ps1 |
| 20 | +
|
| 21 | +.EXAMPLE |
| 22 | +./eng/scripts/Convert-TestResultsToJUnit.ps1 -TestResultsDirectory ./test-results -OutputDirectory ./junit-results |
| 23 | +#> |
| 24 | + |
| 25 | +param( |
| 26 | + [string]$TestResultsDirectory, |
| 27 | + [string]$OutputDirectory |
| 28 | +) |
| 29 | + |
| 30 | +$ErrorActionPreference = 'Stop' |
| 31 | +Set-StrictMode -Version 2.0 |
| 32 | + |
| 33 | +# Get repo root |
| 34 | +$RepoRoot = Resolve-Path (Join-Path $PSScriptRoot .. ..) |
| 35 | + |
| 36 | +# Set default directories |
| 37 | +if (!$TestResultsDirectory) { |
| 38 | + $TestResultsDirectory = Join-Path $RepoRoot "test-results" |
| 39 | +} |
| 40 | + |
| 41 | +if (!$OutputDirectory) { |
| 42 | + $OutputDirectory = Join-Path $RepoRoot "test-results" "junit" |
| 43 | +} |
| 44 | + |
| 45 | +Write-Host "Converting test results from JSON to JUnit XML using cargo2junit" |
| 46 | +Write-Host " Input directory: $TestResultsDirectory" |
| 47 | +Write-Host " Output directory: $OutputDirectory" |
| 48 | + |
| 49 | +# Check if test results directory exists |
| 50 | +if (!(Test-Path $TestResultsDirectory)) { |
| 51 | + Write-Warning "Test results directory not found: $TestResultsDirectory" |
| 52 | + Write-Host "No test results to convert." |
| 53 | + exit 0 |
| 54 | +} |
| 55 | + |
| 56 | +# Create output directory if it doesn't exist |
| 57 | +if (!(Test-Path $OutputDirectory)) { |
| 58 | + New-Item -ItemType Directory -Path $OutputDirectory | Out-Null |
| 59 | + Write-Host "Created output directory: $OutputDirectory" |
| 60 | +} |
| 61 | + |
| 62 | +# Check if cargo2junit is installed |
| 63 | +$cargo2junitPath = Get-Command cargo2junit -ErrorAction SilentlyContinue |
| 64 | +if (!$cargo2junitPath) { |
| 65 | + Write-Host "cargo2junit not found. Installing..." |
| 66 | + cargo install cargo2junit |
| 67 | + if ($LASTEXITCODE -ne 0) { |
| 68 | + Write-Error "Failed to install cargo2junit" |
| 69 | + exit 1 |
| 70 | + } |
| 71 | + Write-Host "cargo2junit installed successfully" |
| 72 | +} |
| 73 | + |
| 74 | +# Get all JSON files in the test results directory |
| 75 | +$jsonFiles = @(Get-ChildItem -Path $TestResultsDirectory -Filter "*.json" -File) |
| 76 | + |
| 77 | +if ($jsonFiles.Count -eq 0) { |
| 78 | + Write-Warning "No JSON files found in $TestResultsDirectory" |
| 79 | + Write-Host "No test results to convert." |
| 80 | + exit 0 |
| 81 | +} |
| 82 | + |
| 83 | +Write-Host "`nConverting $($jsonFiles.Count) JSON file(s) to JUnit XML..." |
| 84 | + |
| 85 | +$convertedCount = 0 |
| 86 | +$failedCount = 0 |
| 87 | + |
| 88 | +foreach ($jsonFile in $jsonFiles) { |
| 89 | + $baseName = [System.IO.Path]::GetFileNameWithoutExtension($jsonFile.Name) |
| 90 | + $junitFile = Join-Path $OutputDirectory "$baseName.xml" |
| 91 | + |
| 92 | + Write-Host " Converting: $($jsonFile.Name) -> $([System.IO.Path]::GetFileName($junitFile))" |
| 93 | + |
| 94 | + try { |
| 95 | + # Convert JSON to JUnit XML using cargo2junit |
| 96 | + Get-Content $jsonFile.FullName | cargo2junit > $junitFile |
| 97 | + |
| 98 | + if ($LASTEXITCODE -ne 0) { |
| 99 | + Write-Warning " cargo2junit returned exit code $LASTEXITCODE for $($jsonFile.Name)" |
| 100 | + $failedCount++ |
| 101 | + } |
| 102 | + else { |
| 103 | + $convertedCount++ |
| 104 | + } |
| 105 | + } |
| 106 | + catch { |
| 107 | + Write-Warning " Failed to convert $($jsonFile.Name): $_" |
| 108 | + $failedCount++ |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +Write-Host "`nConversion complete:" |
| 113 | +Write-Host " Successfully converted: $convertedCount" |
| 114 | +if ($failedCount -gt 0) { |
| 115 | + Write-Host " Failed to convert: $failedCount" -ForegroundColor Yellow |
| 116 | +} |
| 117 | + |
| 118 | +Write-Host "`nJUnit XML files are available in: $OutputDirectory" |
| 119 | + |
| 120 | +# Exit with error if any conversions failed |
| 121 | +if ($failedCount -gt 0) { |
| 122 | + exit 1 |
| 123 | +} |
| 124 | + |
| 125 | +exit 0 |
0 commit comments