Skip to content

Commit e567b78

Browse files
ci: add coverage threshold check to fail below 97% (#186)
Add a step to the test workflow that: - Reads coveredstatements and statements from clover.xml - Calculates coverage percentage - Fails the build if coverage drops below 97% --------- Co-authored-by: Claude <[email protected]>
1 parent b49e735 commit e567b78

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ jobs:
4949
- name: Run tests
5050
run: vendor/bin/phpunit --testdox --display-all-issues --coverage-clover build/logs/clover.xml
5151

52+
- name: Check coverage threshold
53+
run: php scripts/check-coverage-threshold.php build/logs/clover.xml
54+
5255
- name: Code Coverage Annotation
5356
uses: ggilder/codecoverage@47c83daaf1d5a3190cad56363baf459c59114170 # v1
5457
with:
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
declare(strict_types=1);
5+
6+
$cloverPath = $argv[1] ?? 'build/logs/clover.xml';
7+
$threshold = 97.0;
8+
9+
if (!file_exists($cloverPath)) {
10+
fwrite(STDERR, "Error: Clover XML file not found at: $cloverPath\n");
11+
exit(1);
12+
}
13+
14+
$xml = simplexml_load_file($cloverPath);
15+
if ($xml === false) {
16+
fwrite(STDERR, "Error: Failed to parse clover XML file\n");
17+
exit(1);
18+
}
19+
20+
$metrics = $xml->project->metrics;
21+
if ($metrics === null) {
22+
fwrite(STDERR, "Error: No project metrics found in clover XML\n");
23+
exit(1);
24+
}
25+
26+
$statements = (int) $metrics['statements'];
27+
$coveredStatements = (int) $metrics['coveredstatements'];
28+
29+
if ($statements === 0) {
30+
fwrite(STDERR, "Error: No statements found in coverage report\n");
31+
exit(1);
32+
}
33+
34+
$coverage = ($coveredStatements / $statements) * 100;
35+
36+
printf("Coverage: %.3f%% (%d/%d statements)\n", $coverage, $coveredStatements, $statements);
37+
38+
if ($coverage < $threshold) {
39+
fwrite(STDERR, sprintf(
40+
"::error::Coverage %.3f%% is below the required threshold of %.1f%%\n",
41+
$coverage,
42+
$threshold
43+
));
44+
exit(1);
45+
}
46+
47+
echo "Coverage meets the required threshold of {$threshold}%\n";

0 commit comments

Comments
 (0)