Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 139 lines (110 sloc) 3.044 kB
#!/bin/bash
#
# yesod test runner, simplistically based on rails' test/unit
#
# searches for "test files" located at ./test/*_test.sh
#
# a test file can contain the following special function definitions:
#
# setup: run before _every_ test
# teardown: run after _every_ test
# test_*: any functions prefixed with this are taken to be your
# actual tests
#
# setup/teardown are optional and if no test_ functions are found, no
# tests are run.
#
# compiles the application into a source distribution, unpacks and
# enters the source distribution, then runs any test_* functions "as
# tests", meaning:
#
# stdout is silenced (unless --verbose) and stderr is collected in /tmp
# (use --tmp <dir> to override). after all tests finish, the failures
# and the collected stderr is reported.
#
###
usage() { echo 'runtests [ --verbose ] [ --tmp <dir> ]'; exit 1; }
# runs the function named by $1 as a test. failures are tracked to be
# reported on after all tests have been run
run_test() { # {{{
local test_function="$*" stdout
$verbose && stdout='/dev/stdout' || stdout='/dev/null'
type -p setup && setup
if $test_function >"$stdout" 2>"$tmp/$test_function.errors"; then
echo -n '.'
rm -f "$tmp/$test_function.errors"
else
echo -n 'F'
FAILURES+=( "$test_function" )
fi
type -p teardown && teardown
}
# }}}
# unset any existing definitions and source new ones from the file named
# by $1
source_test_file() { # {{{
local test_file="$1" test_
[[ -f "$test_file" ]] || return 1
unset -f setup teardown
while read -r test_; do
unset -f test_ # unset any already-defined test_ functions
done < <(set | sed '/^\(test_[^ ]*\) ()/!d; s//\1/g')
source "$test_file" || return 1
# read any newly defined test_ functions into a TESTS array
read -r -d '\n' -a TESTS < <(set | sed '/^\(test_[^ ]*\) ()/!d; s//\1/g')
}
# }}}
# outputs a report of tests and failures
report() { # {{{
local test_
echo
echo
echo "Tests: ${#TESTS[@]}, Failures: ${#FAILURES[@]}."
echo
[[ ${#FAILURES[@]} -eq 0 ]] && return 0
for test_ in ${FAILURES[@]}; do
echo "Failure: $test_"
echo 'details:'
echo
if [[ -f "$tmp/$test_.errors" ]]; then
cat "$tmp/$test_.errors"
rm "$tmp/$test_.errors"
else
echo '<no stderr captured>'
fi
echo
done
return ${#FAILURES[@]}
}
# }}}
tmp='/tmp'
verbose=false
while [[ -n "$1" ]]; do
case "$1" in
-t|--tmp) shift; tmp="$1" ;;
-v|--verbose) verbose=true ;;
*) usage ;;
esac
done
CABAL=${CABAL:-cabal}
echo 'Compiling application...'
(
set -e
$CABAL clean
$CABAL install
$CABAL sdist
) &>/dev/null || { echo 'Failed.' >&2; exit 1; }
read -r pkg < <(find dist/ -type f -name '*.tar.gz' | sort -rV)
dir="$(basename "$pkg" .tar.gz)"
tar -xzf "$pkg"
echo 'Started'
for file in test/*_test.sh; do
source_test_file "$file"
cd "$dir"
for test_ in "${TESTS[@]}"; do
run_test $test_
done
cd - >/dev/null
done
rm -rf "$dir"
report
Jump to Line
Something went wrong with that request. Please try again.