Skip to content

Instantly share code, notes, and snippets.

@mallocator
Last active December 4, 2020 13:46
Show Gist options
  • Save mallocator/34332f7a6a68d15a419c to your computer and use it in GitHub Desktop.
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.
#!/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
@dyong0
Copy link

dyong0 commented Feb 25, 2016

Hi.
I want to use just single pre-commit hook. Could I deploy to others modified version of yours?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment