compare-artifacts.ps1 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env pwsh
  2. <#
  3. .SYNOPSIS
  4. Used by Azure Pipelines to compare code coverage reports between master and current branch.
  5. .PARAMETER wd
  6. The working directory in which to find downloaded artifacts.
  7. #>
  8. param([string]$wd)
  9. if (Test-Path -Path "$wd/current-artifacts/Code Coverage Report_*/summary*/coverage.xml") {
  10. [xml]$BranchXML = Get-Content "$wd/current-artifacts/Code Coverage Report_*/summary*/coverage.xml"
  11. } else {
  12. Write-Host "No code coverage from this build. Is pytest configured to output code coverage? Exiting pipeline." -ForegroundColor Red
  13. exit 1
  14. }
  15. if (Test-Path -Path "$wd/previous-artifacts/Code Coverage Report_*/summary*/coverage.xml") {
  16. [xml]$MasterXML = Get-Content "$wd/previous-artifacts/Code Coverage Report_*/summary*/coverage.xml"
  17. } else {
  18. Write-Host "No code coverage from previous build. Exiting pipeline." -ForegroundColor Red
  19. exit 2
  20. }
  21. $masterlinerate = [math]::Round([decimal]$MasterXML.coverage.'line-rate' * 100, 2)
  22. $branchlinerate = [math]::Round([decimal]$BranchXML.coverage.'line-rate' * 100, 2)
  23. Write-Output "Master line coverage rate: $masterlinerate%"
  24. Write-Output "Branch line coverage rate: $branchlinerate%"
  25. if ($masterlinerate -eq 0) {
  26. $change = "Infinite"
  27. } else {
  28. $change = [math]::Abs($branchlinerate - $masterlinerate)
  29. }
  30. if ($branchlinerate -gt $masterlinerate) {
  31. Write-Host "Coverage increased by $change% 😀" -ForegroundColor Green
  32. exit 0
  33. } elseif ($branchlinerate -eq $masterlinerate) {
  34. Write-Host "Coverage has not changed." -ForegroundColor Green
  35. exit 0
  36. } else {
  37. Write-Host "Coverage decreased by $change% 😭" -ForegroundColor Red
  38. exit 4
  39. }