Skip to content

Commit e5f981b

Browse files
authored
Use third argument to allow users to only echo percentage
Add third, optional, parameter that allows users to only echo the percentage. Use case: we try to save the coverage in our GitLab pipeline via artifacts, and this makes it easier than to "regex my way" to grab the coverage percentage.
1 parent d872edc commit e5f981b

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

coverage-check.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
exit(1);
1111
}
1212

13+
$onlyEchoPercentage = false;
14+
15+
if (isset($argv[3]) && $argv[3] === 'only-percentage') {
16+
$onlyEchoPercentage = true;
17+
}
18+
1319
$inputFile = $argv[1];
1420
$percentage = min(100, max(0, (float)$argv[2]));
1521

@@ -35,9 +41,19 @@
3541
// See calculation: https://confluence.atlassian.com/pages/viewpage.action?pageId=79986990
3642
$TPC = ($coveredstatements + $coveredmethods + $coveredElements) / ($statements + $methods + $elements) * 100;
3743

38-
if ($TPC < $percentage) {
44+
if ($TPC < $percentage && ! $onlyEchoPercentage) {
3945
echo 'Total code coverage is ' . sprintf('%0.2f', $TPC) . '%, which is below the accepted ' . $percentage . '%' . PHP_EOL;
4046
exit(1);
4147
}
4248

49+
if ($TPC < $percentage && $onlyEchoPercentage) {
50+
echo sprintf('%0.2f', $TPC) . PHP_EOL;
51+
exit(1);
52+
}
53+
54+
if ($onlyEchoPercentage) {
55+
echo sprintf('%0.2f', $TPC) . PHP_EOL;
56+
exit(0);
57+
}
58+
4359
echo 'Total code coverage is ' . sprintf('%0.2f', $TPC) . '% - OK!' . PHP_EOL;

0 commit comments

Comments
 (0)