Browse Source

Fix pipeline code coverage comparison (#261)

Vince Salvino 5 years ago
parent
commit
b5b5a60cba
4 changed files with 73 additions and 60 deletions
  1. 1 11
      azure-pipelines.yml
  2. 0 48
      ci/compare-artifacts.ps1
  3. 64 0
      ci/compare-codecov.ps1
  4. 8 1
      docs/contributing/index.rst

+ 1 - 11
azure-pipelines.yml

@@ -109,17 +109,7 @@ stages:
         project: '$(System.TeamProjectId)'
         pipeline: '$(System.DefinitionId)'
 
-    - task: DownloadPipelineArtifact@2
-      displayName: 'Download code coverage from latest master build'
-      inputs:
-        source: 'specific'
-        path: '$(Agent.WorkFolder)/previous-artifacts'
-        project: '$(System.TeamProjectId)'
-        pipeline: '$(System.DefinitionId)'
-        runVersion: 'latestFromBranch'
-        runBranch: 'refs/heads/master'
-
-    - pwsh: ./ci/compare-artifacts.ps1 -wd $Env:WorkDir
+    - pwsh: ./ci/compare-codecov.ps1 -wd $Env:WorkDir
       displayName: 'CR-QC: Compare code coverage'
       env:
         WorkDir: $(Agent.WorkFolder)

+ 0 - 48
ci/compare-artifacts.ps1

@@ -1,48 +0,0 @@
-#!/usr/bin/env pwsh
-
-<#
-.SYNOPSIS
-Used by Azure Pipelines to compare code coverage reports between master and current branch.
-
-.PARAMETER wd
-The working directory in which to find downloaded artifacts.
-#>
-
-param([string]$wd)
-
-if (Test-Path -Path "$wd/current-artifacts/Code Coverage Report_*/summary*/coverage.xml") {
-    [xml]$BranchXML = Get-Content "$wd/current-artifacts/Code Coverage Report_*/summary*/coverage.xml"
-} else {
-    Write-Host "No code coverage from this build. Is pytest configured to output code coverage? Exiting pipeline." -ForegroundColor Red
-    exit 1
-}
-
-if (Test-Path -Path "$wd/previous-artifacts/Code Coverage Report_*/summary*/coverage.xml") {
-    [xml]$MasterXML = Get-Content "$wd/previous-artifacts/Code Coverage Report_*/summary*/coverage.xml"
-} else {
-    Write-Host "No code coverage from previous build. Exiting pipeline." -ForegroundColor Red
-    exit 2
-}
-
-$masterlinerate = [math]::Round([decimal]$MasterXML.coverage.'line-rate' * 100, 2)
-$branchlinerate = [math]::Round([decimal]$BranchXML.coverage.'line-rate' * 100, 2)
-
-Write-Output "Master line coverage rate:  $masterlinerate%"
-Write-Output "Branch line coverage rate:  $branchlinerate%"
-
-if ($masterlinerate -eq 0) {
-    $change = "Infinite"
-} else {
-    $change = [math]::Abs($branchlinerate - $masterlinerate)
-}
-
-if ($branchlinerate -gt $masterlinerate) {
-    Write-Host "Coverage increased by $change% 😀" -ForegroundColor Green
-    exit 0
-} elseif ($branchlinerate -eq $masterlinerate) {
-    Write-Host "Coverage has not changed." -ForegroundColor Green
-    exit 0
-} else {
-    Write-Host "Coverage decreased by $change% 😭" -ForegroundColor Red
-    exit 4
-}

+ 64 - 0
ci/compare-codecov.ps1

@@ -0,0 +1,64 @@
+#!/usr/bin/env pwsh
+
+<#
+.SYNOPSIS
+Compares code coverage percent of local coverage.xml file to master branch (Azure Pipeline API).
+
+.PARAMETER wd
+The working directory in which to search for current coverage.xml.
+#>
+
+param(
+    [string] $wd = (Get-Item (Split-Path $PSCommandPath -Parent)).Parent,
+    [string] $org = "coderedcorp",
+    [string] $project = "coderedcms"
+)
+
+# Hide "UI" and progress bars.
+$ProgressPreference = "SilentlyContinue"
+
+# Get latest coverage from master.
+$ApiBase = "https://dev.azure.com/$org/$project"
+$masterBuildJson = (Invoke-WebRequest "$ApiBase/_apis/build/builds?branchName=refs/heads/master&api-version=5.1").Content | ConvertFrom-Json
+$masterLatestId = $masterBuildJson.value[0].id
+$masterCoverageJson = (Invoke-WebRequest "$ApiBase/_apis/test/codecoverage?buildId=$masterLatestId&api-version=5.1-preview.1").Content | ConvertFrom-Json
+foreach ($cov in $masterCoverageJson.coverageData.coverageStats) {
+    if ($cov.label -eq "Lines") {
+        $masterlinerate = [math]::Round(($cov.covered / $cov.total) * 100, 2)
+    }
+}
+
+# Get current code coverage from coverage.xml file.
+$coveragePath = Get-ChildItem -Recurse -Filter "coverage.xml" $wd
+if (Test-Path -Path $coveragePath) {
+    [xml]$BranchXML = Get-Content $coveragePath
+}
+else {
+    Write-Host "No code coverage from this build. Is pytest configured to output code coverage? Exiting." -ForegroundColor Red
+    exit 1
+}
+$branchlinerate = [math]::Round([decimal]$BranchXML.coverage.'line-rate' * 100, 2)
+
+Write-Output ""
+Write-Output "Master line coverage rate:  $masterlinerate%"
+Write-Output "Branch line coverage rate:  $branchlinerate%"
+
+if ($masterlinerate -eq 0) {
+    $change = "Infinite"
+}
+else {
+    $change = [math]::Abs($branchlinerate - $masterlinerate)
+}
+
+if ($branchlinerate -gt $masterlinerate) {
+    Write-Host "Coverage increased by $change% 😀" -ForegroundColor Green
+    exit 0
+}
+elseif ($branchlinerate -eq $masterlinerate) {
+    Write-Host "Coverage has not changed." -ForegroundColor Green
+    exit 0
+}
+else {
+    Write-Host "Coverage decreased by $change% 😭" -ForegroundColor Red
+    exit 4
+}

+ 8 - 1
docs/contributing/index.rst

@@ -130,7 +130,14 @@ code coverage percentage in the console:
     $ ./ci/run-tests.ps1
 
 Detailed test coverage reports are now available by opening ``htmlcov/index.html``
-in your browser (which is ignored by version control)
+in your browser (which is ignored by version control).
+
+To compare your current code coverage against the code coverage of the master
+branch (based on latest Azure Pipeline build from master) run:
+
+.. code-block:: console
+
+    $ ./ci/compare-codecov.ps1
 
 
 Adding New Tests