Skip to content

Instantly share code, notes, and snippets.

@jamesfernandes
Last active August 16, 2018 22:45
Show Gist options
  • Save jamesfernandes/13d947c4c78f8571751428aedf3cf16c to your computer and use it in GitHub Desktop.
Save jamesfernandes/13d947c4c78f8571751428aedf3cf16c to your computer and use it in GitHub Desktop.
Ruby on Rails testing shortcut script
#!/bin/bash
# TODO: implement "-v" verbose arg
# TESTOPTS="-v"
# -v Verbose; log message to standard output.
# TODO: implement base command switching for `rails test` when Rails gem > v5
function _printUsage()
{
cat <<EOF
Usage: testr <[options]>
Options:
-a Run all files per this pathspec: test/**/*_test.rb
-f Specify explicit file path or pathspec.
-s Specify the seed to run. Useful when debugging failed CI build.
-h Display this help message.
Examples:
testr -a
% bundle exec rake TEST=test/**/*_test.rb
testr test/dir/file_test.rb
testr -f test/dir/file_test.rb
% bundle exec rake TEST=test/dir/file_test.rb
testr -s 12345
% bundle exec rake TEST=test/**/*_test.rb TESTOPTS="--seed=12345"
testr -f test/dir/foo_*_test.rb -s 54321
% bundle exec rake TEST=test/dir/foo_*_test.rb TESTOPTS="--seed=54321"
EOF
exit # always quit after displaying help message
}
while getopts "avf:s:" opt; do
case $opt in
a ) PATHSPEC="test/**/*_test.rb" ;;
f ) PATHSPEC="$OPTARG" ;;
s ) SEED="$OPTARG" ;;
\?) _printUsage ;;
esac
done
# shortcut requires no params, but only assign if PATHSPEC not previously set (ie. by "-a")
if [ $# -eq 1 ] && [ -z "$PATHSPEC" ]; then
PATHSPEC=$1
fi
# if SEED is set, apply TESTOPTS
if [ -n "$SEED" ]; then
OPTS=TESTOPTS=\"--seed=$SEED\"
# default to all test files if seed set but no pathspec
if [ -z "$PATHSPEC" ]; then
PATHSPEC="test/**/*_test.rb"
fi
fi
if [ -z "$PATHSPEC" ]; then
_printUsage
fi
bundle exec rake TEST=$PATHSPEC $OPTS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment