Skip to content

Commit 1c059bb

Browse files
committed
Incorporates clover.xml path guessing from Rick's #44.
1 parent cf4c959 commit 1c059bb

1 file changed

Lines changed: 48 additions & 33 deletions

File tree

coverage-ensure

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function usage()
88
$script = basename(__FILE__);
99
echo <<<EOT
1010
11-
${script} <clover.xml> <coverage_percent>
11+
${script} [coverage_percent] [clover.xml]
1212
Uses the provided Clover XML code coverage report value to calculate
1313
the percentage of code covered by phpunit unit tests. Compares that
1414
percentage to the provided integer value, and if coverage is lower
@@ -20,10 +20,15 @@ ${script} <clover.xml> <coverage_percent>
2020
Usage:
2121
bin/${script}
2222
23-
clover.xml - Provide a filesystem path to an XML file produced
24-
by PHPunit's code coverage.
25-
coverage_percent - An integer value between 0 and 100 represented the
26-
minimum required percentage of coverage required.
23+
coverage_percent - Optional integer value between 0 and 100 represented
24+
the minimum required percentage of coverage required.
25+
If not provided, defaults to 100.
26+
27+
clover.xml - Optional filesystem path to an XML file produced
28+
by PHPunit's code coverage. If absent, the script
29+
will attempt to obtain the correct path using the
30+
project's Config/phpunit.xml file and will exit
31+
with an error if this fails.
2732
2833
EOT;
2934

@@ -33,35 +38,44 @@ if (isset($argv[1]) && $argv[1] == '-h') {
3338
usage();
3439
}
3540

36-
if (!isset($argv[1])) {
37-
throw new InvalidArgumentException(
38-
'First argument missing (clover.xml file path).'
39-
);
40-
}
41-
42-
if (!isset($argv[2])) {
43-
throw new InvalidArgumentException(
44-
'Second argument missing (integer minimum coverage percent).'
45-
);
46-
}
47-
4841
// Set up variables.
49-
$inputFile = $argv[1];
50-
$percentage = min(100, max(0, (int) $argv[2]));
51-
52-
if (!is_readable($inputFile)) {
53-
throw new InvalidArgumentException(
54-
"Invalid input file provided: {$inputFile}"
55-
);
42+
$percentage = (isset($argv[1]) ? (int)$argv[1] : 100);
43+
$percentage = max(0, min(100, $percentage));
44+
45+
// Try to auto-detect the clover file path.
46+
if (isset($argv[2])) {
47+
$inputFile = $argv[2];
48+
} else {
49+
$baseDir = getcwd();
50+
$phpunitConfigFile = "{$baseDir}/Config/phpunit.xml";
51+
if (!is_readable($phpunitConfigFile)) {
52+
echo "!! Could not guess path to clover.xml file. Aborting." . PHP_EOL;
53+
exit(2);
54+
}
55+
56+
$xml = new SimpleXMLElement(file_get_contents($phpunitConfigFile));
57+
$snippet = $xml->xpath('//log[@type="coverage-clover"]');
58+
if (!count($snippet)) {
59+
echo "!! Config/phpunit.xml file does not specify a coverage-clover log. Aborting." . PHP_EOL;
60+
exit(3);
61+
}
62+
63+
$snippet = array_pop($snippet);
64+
if (!isset($snippet['target'])) {
65+
echo '!! Config/phpunit.xml: <log type="coverage-clover"> does not specify a `target` attribute. Aborting.' . PHP_EOL;
66+
exit(4);
67+
}
68+
69+
$snippet = (string)$snippet['target'];
70+
$inputFile = "{$baseDir}/Config/{$snippet}";
5671
}
5772

58-
if (!$percentage) {
59-
throw new InvalidArgumentException(
60-
"An integer checked percentage must be given as second parameter. Value provided: {$percentage}"
61-
);
73+
// Parse the clover report file.
74+
if (!is_readable(realpath($inputFile))) {
75+
echo "!! Invalid input file provided: `{$inputFile}`. Aborting." . PHP_EOL;
76+
exit(5);
6277
}
63-
64-
$xml = new SimpleXMLElement(file_get_contents($inputFile));
78+
$xml = new SimpleXMLElement(file_get_contents(realpath($inputFile)));
6579
$metrics = $xml->xpath('//metrics');
6680
$totalElements = 0;
6781
$checkedElements = 0;
@@ -73,11 +87,12 @@ foreach ($metrics as $metric) {
7387

7488
$coverage = ($checkedElements / $totalElements) * 100;
7589

76-
echo 'Code coverage is ' . number_format($coverage, 1) . '%';
90+
// Spit out the results.
91+
echo 'Code coverage is ' . number_format($coverage, 1) . '% - ';
7792

7893
if ($coverage < $percentage) {
79-
echo " - Minimum accepted is {$percentage}%" . PHP_EOL;
94+
echo "Minimum accepted is {$percentage}%" . PHP_EOL;
8095
exit(1);
8196
}
8297

83-
echo ' - OK!' . PHP_EOL;
98+
echo 'OK!' . PHP_EOL;

0 commit comments

Comments
 (0)