Skip to content

Instantly share code, notes, and snippets.

@rdallman
Created October 7, 2013 03:58
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 rdallman/6862350 to your computer and use it in GitHub Desktop.
Save rdallman/6862350 to your computer and use it in GitHub Desktop.
pre-commit hook for golang projects that assures all tests pass and all files are gofmt'd
#!/bin/sh
status=0
IFS=$'\n'
for file in $(git diff --cached --name-only | grep -e '\.go$'); do
badfile="$(git --no-pager show :"$file" | gofmt -l)"
if test -n "$badfile" ; then
echo "git pre-commit check failed: file needs gofmt: $file"
status=1
fi
done
badtest="$(go test | grep "FAIL:" | awk '{print $3}')"
if test -n "$badtest" ; then
for bad in $badtest; do
echo "git pre-commit check failed: go test failed: $bad"
done
status=1
fi
exit $status
@davidfetter
Copy link

How do you get go test to send its FAIL outputs to STDOUT? I'm only catching them on STDERR.

@flowchartsman
Copy link

flowchartsman commented Aug 18, 2017

a redirect should suffice
"$(go test 2>&1 | grep "FAIL:" | awk '{print $3}')"

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