Last active
March 26, 2018 09:37
-
-
Save milhomem/dfecf85b3f3fa75fc87f to your computer and use it in GitHub Desktop.
Script to show us difference of coverage when adding or changing code with PHPUnit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Reference: http://archive.gregk.me/2011/phpunit-command-line-code-coverage-summary/ | |
COVERAGE="" | |
METRICS="/tmp/phpunit-metrics.xml" | |
BEFORE="/tmp/before-wip" | |
AFTER="/tmp/after-wip" | |
if [ "$1" == "--clean" ] | |
then | |
rm -f $METRICS $BEFORE $AFTER | |
exit 0 | |
fi | |
if [ "$1" == "--coverage" ] | |
then | |
COVERAGE="--coverage-clover $METRICS" | |
shift | |
fi | |
OUTPUT=$BEFORE | |
if [ -f $BEFORE ]; | |
then | |
OUTPUT=$AFTER | |
fi | |
phpunit $COVERAGE $@ | |
# exit with PHPUnit's return code | |
if [ $? -ne 0 ] | |
then | |
exit $? | |
fi | |
if [ "$COVERAGE" != "" ] | |
then | |
echo "====================== Code Coverage Summary =====================" | |
FILTER_LINES="/<class/ { print \$2 } /<metrics method/ { print \$6,\$7 }" | |
JOIN_LINES="\$!N;s/\n/ /" | |
TRANSFORM="s/name=\"\([A-Za-z_]*\)\" statements=\"\([0-9]*\)\" coveredstatements=\"\([0-9]*\)\"/\1\t\2\t\3/" | |
RESULTS=$(cat "$METRICS" | awk "$FILTER_LINES" | sed "$JOIN_LINES" | sed "$TRANSFORM" | tr '\t' '|') | |
(for line in $RESULTS; do | |
CLASS=`echo "$line" | cut -d '|' -f1`; | |
STATEMENTS=`echo "$line" | cut -d '|' -f2`; | |
COVERED=`echo "$line" | cut -d '|' -f3`; | |
COVERAGE=0 | |
if [[ "$STATEMENTS" > "0" && "$COVERED" > "0" ]] | |
then | |
COVERAGE=$(echo | awk "{print $COVERED * 100 / $STATEMENTS}") | |
fi | |
echo "$CLASS $COVERAGE" | awk '{printf "%-60s %.2f%\n", $1, $2}' | |
done) | sort -nr --key=2 -o $OUTPUT | |
rm "$METRICS" | |
fi | |
if [[ -f $BEFORE && -f $AFTER ]]; | |
then | |
DIFFERENCES=$(diff $BEFORE $AFTER) | |
if [ -z "$DIFFERENCES" ]; | |
then | |
echo "We have a tie!" | |
fi | |
echo $DIFFERENCES | |
rm -f $AFTER | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment