Created
August 20, 2012 22:36
-
-
Save andrewgross/3408708 to your computer and use it in GitHub Desktop.
Bash Pre Commit Hook
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 | |
# A pre-commit hook that will stash any changes so that we are running our tests | |
# against what we are committing, not what was on in memory. | |
# Because I was having trouble getting trap to play nicely with any signal except | |
# EXIT we just carry around our status and exit with it at the end | |
status=0 | |
function git_stash() { | |
echo "Stashing changes before running tests..." | |
git stash --include-untracked --keep-index | |
if [ "$?" != "0" ] | |
then | |
echo "Failed!..." | |
exit 1 | |
else | |
echo | |
echo "Ok!..." | |
fi | |
} | |
function git_unstash() { | |
echo "Restoring stashed changes..." | |
git stash pop --quiet --index | |
exit ${status} | |
} | |
function knife_test() { | |
local grep_code=0 | |
echo "Testing Cookbook Syntax with Knife..." | |
# Use egrep to limit to just error lines (stderr not respected by knife) | |
# Should just include lines with FATAL and | |
knife cookbook test --all | egrep [A-Z][A-Z]; grep_code=$? | |
if [ "${grep_code}" == "0" ] | |
then | |
echo "Failed!..." | |
status=1 | |
exit | |
else | |
echo | |
echo "Ok!..." | |
fi | |
return | |
} | |
function foodcritic_test() { | |
echo "Using Foodcritic to lint our cookbooks..." | |
# Stricter Etsy rules included w/ autofail | |
# foodcritic -t any -f any -I foodcritic-rules/rules.rb cookbooks/ | |
foodcritic -f any cookbooks/ | |
if [ "$?" != "0" ] | |
then | |
echo "Failed!..." | |
status=1 | |
exit | |
else | |
echo "Ok!..." | |
fi | |
return | |
} | |
function rspec_test() { | |
echo "Running Rspec on yipit cookbooks..." | |
rspec cookbooks/* | |
if [ "$?" != "0" ] | |
then | |
echo "Failed!..." | |
status=1 | |
exit | |
fi | |
return | |
} | |
trap git_unstash EXIT | |
# Stash any uncommitted changes so we are testing the right things | |
git_stash | |
# Check for syntax errors | |
knife_test | |
# Lint our custom cookbooks | |
foodcritic_test | |
# Run our Unit Tests with Rspec | |
rspec_test | |
echo | |
exit ${status} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment