-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoverage-ensure
executable file
·113 lines (91 loc) · 3.42 KB
/
coverage-ensure
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env php
<?php
// @ref: http://ocramius.github.io/blog/automated-code-coverage-check-for-github-pull-requests-with-travis/
/**
* Prints usage information and exits.
*
* @param string $script Typically set to basename($argv[0]) when the method is called.
* @param int $exitCode The numeric exit code to return. 0 = success. >0 = failure.
* @return void
*/
function usage($script = null, $exitCode = 0) {
$script = $script ?: basename(__FILE__);
$usage = <<<EOD
{$script}
Uses the provided Clover XML code coverage report value to calculate
the percentage of code covered by phpunit unit tests. Compares that
percentage to the provided integer value, and if coverage is lower
than specified the script returns a non-zero value to indicate failure.
Intended to be used with automated build systems like Travis to ensure
code coverage remains above a designated percentage.
Usage:
bin/${script} [coverage_percent] [clover.xml]
coverage_percent - Optional integer value between 0 and 100 represented
the minimum required percentage of coverage required.
If not provided, defaults to 100.
clover.xml - Optional filesystem path to an XML file produced
by PHPunit's code coverage. If absent, the script
will attempt to obtain the correct path using the
project's phpunit.xml[.dist] file and will exit
with an error if this fails.
EOD;
echo $usage;
exit($exitCode);
}
/**
* main() ===================================================================
*/
if (isset($argv[1]) && $argv[1] == '-h') {
usage();
}
// Set up variables.
$percentage = (isset($argv[1]) ? (int)$argv[1] : 100);
$percentage = max(0, min(100, $percentage));
// Try to auto-detect the clover file path.
if (isset($argv[2])) {
$inputFile = $argv[2];
} else {
$baseDir = getcwd();
$phpunitConfigFile = "{$baseDir}/phpunit.xml";
if (!is_readable($phpunitConfigFile)) {
$phpunitConfigFile = "{$baseDir}/phpunit.xml.dist";
if (!is_readable($phpunitConfigFile)) {
echo "!! Could not guess path to clover.xml file. Aborting." . PHP_EOL;
exit(2);
}
}
$xml = new SimpleXMLElement(file_get_contents($phpunitConfigFile));
$snippet = $xml->xpath('//log[@type="coverage-clover"]');
if (!count($snippet)) {
echo "!! phpunit.xml file does not specify a coverage-clover log. Aborting." . PHP_EOL;
exit(3);
}
$snippet = array_pop($snippet);
if (!isset($snippet['target'])) {
echo '!! phpunit.xml: <log type="coverage-clover"> does not specify a `target` attribute. Aborting.' . PHP_EOL;
exit(4);
}
$snippet = (string)$snippet['target'];
$inputFile = "{$baseDir}/{$snippet}";
}
// Parse the clover report file.
if (!is_readable(realpath($inputFile))) {
echo "!! Invalid input file provided: `{$inputFile}`. Aborting." . PHP_EOL;
exit(5);
}
$xml = new SimpleXMLElement(file_get_contents(realpath($inputFile)));
$metrics = $xml->xpath('//metrics');
$totalElements = 0;
$checkedElements = 0;
foreach ($metrics as $metric) {
$totalElements += (int) $metric['elements'];
$checkedElements += (int) $metric['coveredelements'];
}
$coverage = ($checkedElements / $totalElements) * 100;
// Spit out the results.
echo 'Code coverage is ' . number_format($coverage, 1) . '% - ';
if ($coverage < $percentage) {
echo "Minimum accepted is {$percentage}%" . PHP_EOL;
exit(1);
}
echo 'OK!' . PHP_EOL;