Last active
December 4, 2020 13:46
-
-
Save mallocator/34332f7a6a68d15a419c to your computer and use it in GitHub Desktop.
A pre-commit hook for git that will run maven clean test and output any failed tests as well as a summary if there was an error.
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 | |
# save the file as <git_directory>/.git/hooks/pre-commit.d/mvn_test and chmod +x | |
echo "Running mvn clean test for errors" | |
# retrieving current working directory | |
CWD=`pwd` | |
MAIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
# go to main project dir | |
cd $MAIN_DIR/../../../ | |
# running maven clean test | |
MVN_RESULT=$(mvn clean test 2>&1) | |
if [ $? -ne 0 ]; then | |
echo | |
echo "${MVN_RESULT}" | ((tee /dev/fd/5 | grep -A 10 -B 2 "Reactor Summary:" >/dev/fd/4) 5>&1 | sed -n -e '/^Failed tests:/,/Tests run:.*$/ p' ) 4>&1 | |
echo | |
echo "Error while testing the code" | |
# go back to current working dir | |
cd $CWD | |
exit 1 | |
fi | |
# go back to current working dir | |
cd $CWD |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi.
I want to use just single pre-commit hook. Could I deploy to others modified version of yours?