Skip to content

Instantly share code, notes, and snippets.

@francoisfaubert
Last active April 8, 2016 20:14
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 francoisfaubert/65586e22d47c690ad88e3bc923e80a1d to your computer and use it in GitHub Desktop.
Save francoisfaubert/65586e22d47c690ad88e3bc923e80a1d to your computer and use it in GitHub Desktop.
This bash script allows for automated TDD development of a Strata project. It is hard coded for Mac and requires Behat for BDD.
#!/usr/bin/env bash
#
# This bash script is hard-coded for bash and MacOS. It allows for TDD
# development with PHPUnit and Behat from the command line, assuming
# all the dependencies are present.
#
# It is intended to fit in a self calling script for portability even
# if it makes the script look unwieldy.
#
# Invoke the script using:
#
# $ bin/tdd watch
# Env variables
# -------------
MODIFIED_FILE=$@;
SELF=${BASH_SOURCE[0]}
PROJECT_ROOT=$( cd "$( dirname "$SELF" )/.." && pwd )
REL_PATH=`echo $MODIFIED_FILE | sed 's|'$PROJECT_ROOT'/||'`
# Confirm dependencies
# --------------------
command -v fswatch >/dev/null 2>&1 || {
command -v brew >/dev/null 2>&1 || {
echo ""
echo "fswatch is not installed on your machine and you don't have Homebrew installed."
echo "This script doesn't know what to do."\
echo ""
exit 1;
}
# Install
# -------
echo ""
echo "fswatch is not installed on your machine. I will attempt to install it for you."
echo ""
brew install fswatch
# confirm
# -------
command -v fswatch >/dev/null 2>&1 || {
echo ""
echo "fswatch still not installed on this machine. This is going to be a hard one to automate."
echo "Please go see someone IRL!"
echo ""
exit 1;
}
}
command -v terminal-notifier >/dev/null 2>&1 || {
command -v brew >/dev/null 2>&1 || {
echo ""
echo "terminal-notifier is not installed on your machine and you don't have Homebrew installed."
echo "This script doesn't know what to do."
echo ""
exit 1;
}
# Install
# -------
echo ""
echo "terminal-notifier is not installed on your machine. I will attempt to install it for you."
echo ""
brew install terminal-notifier
# confirm
# -------
command -v terminal-notifier >/dev/null 2>&1 || {
echo ""
echo "terminal-notifier still not installed on this machine. This is going to be a hard one to automate."
echo ""
exit 1;
}
}
if [[ ! -f "$PROJECT_ROOT/strata" ]]; then
echo "This script expects to be ran from a Strata project."
exit 1
fi
if [[ ! -f "$PROJECT_ROOT/vendor/bin/behat" ]]; then
echo "This script expects to have access to Behat from Composer."
exit 1
fi
# Check which parameters has been sent to the script. If it matches
# the expected "watch" entry parameter, launch the fswatch watcher.
if [[ "$MODIFIED_FILE" == "watch" ]]; then
echo ""
echo "[OK] Listening..."
fswatch "$PROJECT_ROOT" | xargs -n1 -I {} $SELF {}
exit 0
fi
# Only test when the file comes from one of the expected, known
# backend code locations.
if [[ $REL_PATH == test\/Behat* ]]; then
clear
echo ""
echo ">> $REL_PATH triggered Gherkin tests."
echo ""
echo ""
OUTPUT=$($PROJECT_ROOT/vendor/bin/behat &2>1)
MODE="gherkin"
elif [[ $REL_PATH == test* ]]; then
clear
echo ""
echo ">> $REL_PATH triggered unit tests."
echo ""
echo ""
OUTPUT=$($PROJECT_ROOT/strata test)
MODE="phpunit"
elif [[ $REL_PATH == src* ]]; then
clear
echo ""
echo ">> $REL_PATH triggered unit tests."
echo ""
echo ""
OUTPUT=$($PROJECT_ROOT/strata test)
MODE="phpunit"
fi
# Only bother sending to Growl when there was an output generated.
if [ "$OUTPUT" ]; then
printf "%s" "$OUTPUT"
echo ""
echo ""
echo "[OK] Listening..."
if [[ $MODE == "phpunit" ]]; then
if [[ $OUTPUT == *"FAILURES!"* ]]; then
echo "A unit test failed!" | terminal-notifier -contentImage "http://i.stack.imgur.com/OZAA2.png"
fi
if [[ $OUTPUT == *"Fatal error:"* ]]; then
echo "A unit test failed!" | terminal-notifier -contentImage "http://i.stack.imgur.com/OZAA2.png"
fi
if [[ $OUTPUT == *"OK ("* ]]; then
echo "Unit tests passed." | terminal-notifier -contentImage "http://i.stack.imgur.com/A1pLs.png"
fi
fi
if [[ $MODE == "gherkin" ]]; then
if [[ $OUTPUT == *"--- Failed scenarios"* ]]; then
echo "A Gherkin scenario failed!" | terminal-notifier -contentImage "http://i.stack.imgur.com/OZAA2.png"
else
echo "Gherkin scenarios passed." | terminal-notifier -contentImage "http://i.stack.imgur.com/A1pLs.png"
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment