Last active
April 15, 2019 21:39
-
-
Save zushane/7031816 to your computer and use it in GitHub Desktop.
A git pre-commit hook to run phpunit tests, written in bash. Features pretty colours, and slightly individualized output.
This file contains hidden or 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 | |
# Locate our phpunit. | |
phpunit=`which phpunit` | |
# Any extra arguments to phpunit should go here. | |
phpunit_args="" | |
# Define a location to save the output. | |
outputlog="/tmp/phpunit_output_`date +%s`.log" | |
# Get name of the project (probably topmost directory name). | |
projectname=${PWD##*/} | |
echo | |
echo -e " + Starting unit tests for \033[1m\E[37;44m${projectname}\033[0m." | |
# execute unit tests. (Assume that phpunit.xml is in root of project). | |
output=`${phpunit} ${phpunit_args}` | |
returnCode=$? | |
# Save the output of phpunit for posterity. | |
echo "$output" > $outputlog | |
# if unit tests fail, output a summary and exit with failure code. | |
if [ $returnCode -ne 0 ]; then | |
# find the line with the summary. | |
while read -r line; do | |
if [[ $line =~ Failures: ]] ; then | |
summary=$line | |
break | |
fi | |
done <<< "$output" | |
# output the status. | |
echo -e " + Test suite \033[1m\E[47;41mfailed\033[0m: " | |
echo | |
echo -e "$summary\033[0m" | |
echo | |
echo " + The full output of phpunit has been saved in:" | |
echo -e " \033[1m${outputlog}\033[0m" | |
echo | |
# abort the commit. | |
echo -e " + \033[1m\E[47;41mABORTING COMMIT\033[0m" | |
echo | |
exit $returnCode | |
else | |
echo -e " + All tests \033[1m\E[47;42mpassed\033[0m. The full output of phpunit has been saved in:" | |
echo -e " \033[1m${outputlog}\033[0m" | |
echo " + Proceeding with commit. Have a nice day." | |
echo | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment