-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathTest-CodeCoverageThreshold.ps1
More file actions
68 lines (51 loc) · 2.19 KB
/
Test-CodeCoverageThreshold.ps1
File metadata and controls
68 lines (51 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Checks that code coverage meets a minimum threshold.
.DESCRIPTION
Parses a Go coverage profile using 'go tool cover -func' to extract the
total statement coverage percentage, then fails if it is below the
specified minimum.
.PARAMETER CoverageFile
Path to the Go coverage profile (typically cover.out).
.PARAMETER MinimumCoveragePercent
The minimum acceptable coverage percentage (0-100). The script exits
with a non-zero code when coverage is below this value.
.EXAMPLE
./Test-CodeCoverageThreshold.ps1 -CoverageFile cover.out -MinimumCoveragePercent 48
#>
param(
[Parameter(Mandatory = $true)]
[string]$CoverageFile,
[Parameter(Mandatory = $true)]
[ValidateRange(0, 100)]
[double]$MinimumCoveragePercent
)
$ErrorActionPreference = 'Stop'
if (-not (Test-Path $CoverageFile)) {
throw "Coverage file '$CoverageFile' not found"
}
Write-Host "Checking code coverage threshold (minimum: $MinimumCoveragePercent%)..."
# Use 'go tool cover -func' to get per-function coverage and the total line.
$output = & go tool cover "-func=$CoverageFile" 2>&1
if ($LASTEXITCODE -ne 0) {
throw "go tool cover -func failed: $output"
}
# Find the "total:" summary line, which looks like: "total: (statements) 48.3%"
$totalLine = $output | Where-Object { $_ -match '^\s*total:' } | Select-Object -Last 1
if (-not $totalLine) {
throw "Could not find 'total:' line in 'go tool cover -func' output"
}
# Match both integer (100%) and decimal (48.3%) percentages using invariant culture
if ($totalLine -match '(\d+(?:\.\d+)?)%') {
$coveragePercent = [double]::Parse($matches[1], [System.Globalization.CultureInfo]::InvariantCulture)
} else {
throw "Could not parse coverage percentage from: $totalLine"
}
Write-Host "Total statement coverage: $coveragePercent%"
if ($coveragePercent -lt $MinimumCoveragePercent) {
Write-Host "##vso[task.logissue type=error]Code coverage $coveragePercent% is below the minimum threshold of $MinimumCoveragePercent%."
Write-Host "##vso[task.complete result=Failed;]Coverage threshold not met."
exit 1
}
Write-Host "Coverage $coveragePercent% meets the minimum threshold of $MinimumCoveragePercent%. ✓"