Skip to content

Instantly share code, notes, and snippets.

@jonmbake
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonmbake/7b40d085b1b9f367e7a1 to your computer and use it in GitHub Desktop.
Save jonmbake/7b40d085b1b9f367e7a1 to your computer and use it in GitHub Desktop.
Rails/Angular Pre-Commit Hook
#!/usr/bin/env bash
#
# Rails/Angular pre commit hook.
#
# This should live in your rails config folder and a symbolic link should be created from .git/hooks/ to this file. You
# should also set the FRONT_END_PATH/BACK_END_PATH env. variable so the script knows where to pull in the front-end code from.
#
# This pre-commit hook does the following:
# 1. Runs rspec (and fails the commit if any tests fail)
# 2. Runs grunts default task on the Front-End project (which should minify and run karam tests). Fails the commit if any tests fail.
# 3. Copies the Front-End assets into public directory
# 4. Starts the Selium server and runs protractor tests. Fails the commit if any tests fail.
# 5. Finally, git add any ./public/* files
#kill subshells on exit
trap 'kill -n 2 0' SIGINT SIGTERM
#kill everything if exit code is not 1, make sure to not kill parent shell if exit code is zero.
trap 'if [ $? -ne 0 ] ; then kill -n 2 0 ; else kill -n 2 -- -$$ ; fi' EXIT
echo "Running Front-End tests and copying assets"
echo "----"
# if FRONT_END_PATH env var not set bail
[ -z "$FRONT_END_PATH" ] && echo "FRONT_END_PATH env variable must be set" && exit 1
# Run respec tests
rspec
RESULT=$?
[ $RESULT -ne 0 ] && echo "Rspec tests failed; exiting" && exit 1
# Run grunt build - this will run unit tests
cd $FRONT_END_PATH && grunt
RESULT=$?
[ $RESULT -ne 0 ] && echo "Front end unit tests failed; commit cancelled." && exit 1
#copy newly built assets to public
echo "Copying newly built assets to /public"
cd $BACK_END_PATH && cp -r $FRONT_END_PATH/dist/* ./public/
#start selenium server in subshell
echo "Starting e2e testing - starting servers"
cd $BACK_END_PATH
RAILS_ENV=test rake db:reset
#start rails server in test
(rails s -e=test) &
($FRONT_END_PATH/node_modules/protractor/bin/webdriver-manager start --standalone) &
# wait for these bg process to start
sleep 2
#run grunts e2e test
echo "Running e2e tests"
$FRONT_END_PATH/node_modules/protractor/bin/protractor $FRONT_END_PATH/test/protractor-rails.conf.js
# run test
RESULT=$?
[ $RESULT -ne 0 ] && echo "E2E tests failed; commit cancelled." && exit 1
# add files and exit
git add -f ./public/*
echo "All Front-End tests completed successfully. Adding front-end code to commit."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment