From aeb8f51141974fd4e617b10956d401a02639e16a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niko=20Heikkila=CC=88?= Date: Sun, 13 Oct 2019 18:27:37 +0300 Subject: [PATCH] Refactor and optimize code - Guard against empty data (division by zero) - Refactor logic to functions and constants - Make 'only-percentage' option more canonical by prepending with two dashes - Update tests --- README.md | 10 +++--- composer.lock | 19 ++++++++++ coverage-check.php | 87 ++++++++++++++++++++++++++++++---------------- test/empty.xml | 20 +++++++++++ test/run | 7 ++++ 5 files changed, 109 insertions(+), 34 deletions(-) create mode 100644 composer.lock create mode 100644 test/empty.xml diff --git a/README.md b/README.md index ab8564f..360d60b 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,9 @@ This script can be used in your continuous deployment environment or for example # Installation The script can be installed using composer. Add this repository as a dependency to the composer.json file. + ```bash composer require --dev rregeer/phpunit-coverage-check -composer install ``` # Usage @@ -17,17 +17,19 @@ The script has requires 2 parameters that are mandatory to return the code cover 1. The location of the clover xml file, that's generated by phpunit. 2. The coverage threshold that is acceptable. Min = 1, Max = 100 -Generate the clover xml by using phpunit and run the coverage check script: +Generate the `clover.xml` file by using phpunit and run the coverage check script: Run the script: + ```bash vendor/bin/phpunit --coverage-clover clover.xml vendor/bin/coverage-check clover.xml 80 -vendor/bin/coverage-check clover.xml 80 only-percentage +vendor/bin/coverage-check clover.xml 80 --only-percentage ``` -With the `only-percentage` enabled, the CLI command will only return the resulting coverage percentage. +With the `--only-percentage` enabled, the CLI command will only return the resulting coverage percentage. It's also possible to add the coverage report generation to the phpunit.xml.dist add to following line to the xml file: + ```xml diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..3de2b89 --- /dev/null +++ b/composer.lock @@ -0,0 +1,19 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "a9497a69ebd8915d88ea719f570f9441", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=7.0.0" + }, + "platform-dev": [] +} diff --git a/coverage-check.php b/coverage-check.php index c3f0b40..5720ad3 100755 --- a/coverage-check.php +++ b/coverage-check.php @@ -1,26 +1,46 @@ xpath(XPATH_METRICS); } -$onlyEchoPercentage = false; +function printStatus(string $msg, int $exitCode = STATUS_OK) +{ + echo $msg.PHP_EOL; + exit($exitCode); +} -if (isset($argv[3]) && $argv[3] === 'only-percentage') { - $onlyEchoPercentage = true; +if (! isset($argv[1]) || ! file_exists($argv[1])) { + printStatus("Invalid input file {$argv[1]} provided.", STATUS_ERROR); } -$inputFile = $argv[1]; -$percentage = min(100, max(0, (float)$argv[2])); +if (! isset($argv[2])) { + printStatus( + 'An integer checked percentage must be given as second parameter.', + STATUS_ERROR + ); +} + +$onlyEchoPercentage = isset($argv[3]) && $argv[3] === '--only-percentage'; -$xml = new SimpleXMLElement(file_get_contents($inputFile)); -$metrics = $xml->xpath('//metrics'); +$inputFile = $argv[1]; +$percentage = min(100, max(0, (float) $argv[2])); $elements = 0; $coveredElements = 0; @@ -29,31 +49,38 @@ $methods = 0; $coveredmethods = 0; -foreach ($metrics as $metric) { - $elements += (int)$metric['elements']; - $coveredElements += (int)$metric['coveredelements']; - $statements += (int)$metric['statements']; - $coveredstatements += (int)$metric['coveredstatements']; - $methods += (int)$metric['methods']; - $coveredmethods += (int)$metric['coveredmethods']; +foreach (loadMetrics($inputFile) as $metric) { + $elements += (int) $metric['elements']; + $coveredElements += (int) $metric['coveredelements']; + $statements += (int) $metric['statements']; + $coveredstatements += (int) $metric['coveredstatements']; + $methods += (int) $metric['methods']; + $coveredmethods += (int) $metric['coveredmethods']; } // See calculation: https://confluence.atlassian.com/pages/viewpage.action?pageId=79986990 -$TPC = ($coveredstatements + $coveredmethods + $coveredElements) / ($statements + $methods + $elements) * 100; +$coveredMetrics = $coveredstatements + $coveredmethods + $coveredElements; +$totalMetrics = $statements + $methods + $elements; + +if ($totalMetrics === 0) { + printStatus('Insufficient data for calculation. Please add more code.', STATUS_ERROR); +} + +$totalPercentageCoverage = $coveredMetrics / $totalMetrics * 100; -if ($TPC < $percentage && ! $onlyEchoPercentage) { - echo 'Total code coverage is ' . sprintf('%0.2f', $TPC) . '%, which is below the accepted ' . $percentage . '%' . PHP_EOL; - exit(1); +if ($totalPercentageCoverage < $percentage && ! $onlyEchoPercentage) { + printStatus( + 'Total code coverage is '.formatCoverage($totalPercentageCoverage).' which is below the accepted '.$percentage.'%', + STATUS_ERROR + ); } -if ($TPC < $percentage && $onlyEchoPercentage) { - echo sprintf('%0.2f', $TPC) . PHP_EOL; - exit(1); +if ($totalPercentageCoverage < $percentage && $onlyEchoPercentage) { + printStatus(formatCoverage($totalPercentageCoverage), STATUS_ERROR); } if ($onlyEchoPercentage) { - echo sprintf('%0.2f', $TPC) . PHP_EOL; - exit(0); + printStatus(formatCoverage($totalPercentageCoverage)); } -echo 'Total code coverage is ' . sprintf('%0.2f', $TPC) . '% - OK!' . PHP_EOL; +printStatus('Total code coverage is '.formatCoverage($totalPercentageCoverage).' – OK!'); diff --git a/test/empty.xml b/test/empty.xml new file mode 100644 index 0000000..5a83a03 --- /dev/null +++ b/test/empty.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/test/run b/test/run index d0b271e..d4b5399 100755 --- a/test/run +++ b/test/run @@ -8,5 +8,12 @@ bin/coverage-check test/clover.xml 0 bin/coverage-check test/clover.xml 90 # Expect fail +! bin/coverage-check test/empty.xml 10 ! bin/coverage-check test/clover.xml 95 ! bin/coverage-check test/clover.xml 100 + +# Only percentage +expected="90.32 %" +actual=$(bin/coverage-check test/clover.xml 90 --only-percentage) + +[[ "$expected" == "$actual" ]] || (echo "ERROR: Expected coverage $expected, got $actual" && exit 1)