How to force run unit tests when running Git push?

#!/bin/bash
while read oldrev newrev refname
do
    # Only run this script for the master branch. You can remove this 
    # if block if you wish to run it for others as well.
    if [[ $refname = "refs/heads/master" ]] ; then
        # Anything echo'd will show up in the console for the person 
        # who's doing a push
        echo "Preparing to run phpunit for $newrev ... "
 
        # Since the repo is bare, we need to put the actual files someplace, 
        # so we use the temp dir we chose earlier
        git archive $newrev | tar -x -C /home/jani/tmp/example
 
        echo "Running phpunit for $newrev ... "
 
        # This part is the actual code which is used to run our tests
        # In my case, the phpunit testsuite resides in the tests directory, so go there
        cd /home/jani/tmp/example/tests       
 
        # And execute the testsuite, while ignoring any output
        phpunit > /dev/null
 
        # $? is a shell variable which stores the return code from what we just ran
        rc=$?
        if [[ $rc != 0 ]] ; then
            # A non-zero return code means an error occurred, so tell the user and exit
            echo "phpunit failed on rev $newrev - push deniend. Run tests locally and confirm they pass before pushing"
            exit $rc
        fi
    fi
done
 
# Everything went OK so we can exit with a zero
exit 0

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source