Created
November 21, 2017 21:12
-
-
Save bmonkman/0488304240dcdf1683155c3e8de9288f to your computer and use it in GitHub Desktop.
Go TDD
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
#!/usr/bin/env bash | |
PATTERN="${1}" | |
if [ "${PATTERN}" == "" ]; then | |
PATTERN="*" | |
fi | |
DIR=`pwd` | |
LAST_HASH="" | |
LAST_FILE_LIST="" | |
function runIfChanged { | |
FILE_LIST=`find . -type f -name "${PATTERN}" -not -path "*/.git/*" -not -path "*/go-*" -ls` | |
NEW_HASH=`echo "$FILE_LIST" | md5` | |
if [ "${NEW_HASH}" != "${LAST_HASH}" ]; then | |
echo -e " \033[32mRunning tests...\033[0m" | |
RESULT=$(go test $VERBOSEFLAG ./... 2>&1) | |
clear | |
echo -e "\033[32m$(date)\n$(date) Watching files under \033[33m${DIR}\033[32m matching pattern \033[33m${PATTERN}\033[0m" | |
CHANGED_FILES=$(diff <(echo "$FILE_LIST") <(echo "$LAST_FILE_LIST") | grep ">" | awk -v OFS="\t" {'print $12'}) | |
CHANGED_COUNT=$(echo "$CHANGED_FILES" | wc -l) | |
if [[ "$CHANGED_COUNT" -gt 1 ]]; then | |
echo -e "\033[32m Changed: \033[33m$CHANGED_COUNT\033[32m files\033[0m" | |
else | |
echo -e "\033[32m Changed: \033[33m$CHANGED_FILES\033[0m" | |
fi | |
RESULT=$(echo "$RESULT" | sed 's/FAIL/\\033[31mFAIL\\033[0m/g') | |
echo -e "$RESULT" | |
fi | |
LAST_HASH="${NEW_HASH}" | |
LAST_FILE_LIST="${FILE_LIST}" | |
} | |
while true; do | |
echo -e "\033[0;0H\033[32m$(date)\033[0m\033[2;0H" | |
runIfChanged | |
read -n 1 -t 1 -s input | |
case "$input" in | |
v) | |
[ "$VERBOSEFLAG" == "-v" ] && VERBOSEFLAG="" || VERBOSEFLAG="-v" | |
LAST_HASH="" | |
;; | |
q) | |
exit | |
;; | |
esac | |
input="" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment