2013-06-19 02:46:31 -04:00
|
|
|
#!/bin/sh
|
2012-10-04 07:24:03 -04:00
|
|
|
|
2012-10-11 14:50:03 -04:00
|
|
|
fullpath() {
|
|
|
|
(
|
2013-07-26 07:51:01 -04:00
|
|
|
cd -- "$1"
|
2012-10-11 14:50:03 -04:00
|
|
|
pwd
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2012-10-10 20:43:13 -04:00
|
|
|
indent() {
|
|
|
|
level="$1"
|
|
|
|
printf "%$((2 * ${level}))s"
|
|
|
|
}
|
|
|
|
|
2012-10-04 07:29:34 -04:00
|
|
|
recurse() {
|
|
|
|
potential_test="$1"
|
2012-10-10 20:43:13 -04:00
|
|
|
indent_level="$2"
|
2014-10-17 17:16:12 -04:00
|
|
|
shell_for_sh_tests="$3"
|
2012-10-10 14:40:49 -04:00
|
|
|
|
2012-10-10 14:43:41 -04:00
|
|
|
[ "$potential_test" = 'setup_dir' ] && return
|
|
|
|
[ "$potential_test" = 'teardown_dir' ] && return
|
|
|
|
[ "$potential_test" = 'setup' ] && return
|
|
|
|
[ "$potential_test" = 'teardown' ] && return
|
2012-10-10 14:40:49 -04:00
|
|
|
|
2014-10-16 16:37:51 -04:00
|
|
|
[ $indent_level -eq 0 ] && : > "$stdout_file"
|
2012-10-10 20:31:18 -04:00
|
|
|
|
2012-10-04 07:29:34 -04:00
|
|
|
if [ -d "$potential_test" ]
|
|
|
|
then
|
|
|
|
(
|
2012-10-10 20:43:13 -04:00
|
|
|
indent $indent_level
|
|
|
|
echo " ${potential_test}"
|
2013-07-26 07:51:01 -04:00
|
|
|
cd -- "$potential_test"
|
2013-06-19 02:34:07 -04:00
|
|
|
[ -f setup_dir ] && [ -x setup_dir ] && ./setup_dir >> "$stdout_file"
|
2014-11-05 12:38:22 -05:00
|
|
|
|
|
|
|
if [ -n "$ZSH_VERSION" ]; then
|
|
|
|
# avoid "no matches found: *" error when directories are empty
|
|
|
|
setopt NULL_GLOB
|
|
|
|
fi
|
|
|
|
|
2012-10-08 10:16:49 -04:00
|
|
|
for test in *
|
2012-10-10 20:31:18 -04:00
|
|
|
do
|
2013-06-19 02:34:07 -04:00
|
|
|
[ -f setup ] && [ -x setup ] && ./setup >> "$stdout_file"
|
2012-10-10 20:43:13 -04:00
|
|
|
|
|
|
|
# $2 instead of $indent_level so it doesn't clash
|
2014-10-17 17:16:12 -04:00
|
|
|
recurse "${test}" $(( $2 + 1 )) "$shell_for_sh_tests"
|
2012-10-10 20:43:13 -04:00
|
|
|
|
2013-06-19 02:34:07 -04:00
|
|
|
[ -f teardown ] && [ -x teardown ] && ./teardown >> "$stdout_file"
|
2012-10-08 08:50:48 -04:00
|
|
|
done
|
2013-06-19 02:34:07 -04:00
|
|
|
[ -f teardown_dir ] && [ -x teardown_dir ] && ./teardown_dir >> "$stdout_file"
|
2012-10-11 01:17:20 -04:00
|
|
|
echo
|
2012-10-04 07:29:34 -04:00
|
|
|
)
|
2012-10-08 08:59:14 -04:00
|
|
|
elif [ -x "$potential_test" ]
|
2012-10-04 07:29:34 -04:00
|
|
|
then
|
2012-10-10 14:25:44 -04:00
|
|
|
|
2013-06-19 02:34:07 -04:00
|
|
|
[ -f setup ] && [ -x setup ] && ./setup >> "$stdout_file"
|
2012-10-10 15:47:21 -04:00
|
|
|
|
2012-10-10 14:25:44 -04:00
|
|
|
# Run the test
|
2014-10-17 17:16:12 -04:00
|
|
|
if [ -n "$shell_for_sh_tests" ] && has_sh_or_no_shebang_line ./"$potential_test"
|
|
|
|
then
|
|
|
|
TEST_SHELL="$TEST_SHELL" "$shell_for_sh_tests" ./"$potential_test" > "$stdout_file" 2>&1
|
|
|
|
else
|
|
|
|
TEST_SHELL="$TEST_SHELL" ./"$potential_test" > "$stdout_file" 2>&1
|
|
|
|
fi
|
2012-10-10 14:40:49 -04:00
|
|
|
exit_code="$?"
|
2012-10-10 15:47:21 -04:00
|
|
|
|
2014-10-17 17:16:12 -04:00
|
|
|
|
2013-06-19 02:34:07 -04:00
|
|
|
[ -f teardown ] && [ -x teardown ] && ./teardown >> "$stdout_file"
|
2012-10-10 14:25:44 -04:00
|
|
|
|
2012-10-10 20:43:13 -04:00
|
|
|
indent $indent_level
|
2014-10-16 16:37:51 -04:00
|
|
|
if [ $exit_code -eq 0 ]
|
2012-10-04 07:29:34 -04:00
|
|
|
then
|
2014-10-16 16:37:51 -04:00
|
|
|
# On success, print a green '✓'
|
2013-06-18 17:11:04 -04:00
|
|
|
printf '\033[32m✓ \033[0m'
|
2013-06-26 03:15:07 -04:00
|
|
|
printf '%s\n' "${potential_test}"
|
|
|
|
printf '%s\n' "${potential_test} passed" >> "$logfile"
|
2012-10-04 07:29:34 -04:00
|
|
|
else
|
2012-10-10 14:25:44 -04:00
|
|
|
# On fail, print a red '✗'
|
2013-06-18 17:11:04 -04:00
|
|
|
printf '\033[31m✗ \033[0m'
|
2013-06-26 03:15:07 -04:00
|
|
|
printf '%s\n' "${potential_test}"
|
|
|
|
printf '%s\n' "${potential_test} failed" >> "$logfile"
|
2014-10-16 16:37:51 -04:00
|
|
|
printf '\033[31m' # Print output captured from failed test in red.
|
2013-06-19 02:34:07 -04:00
|
|
|
cat "$stdout_file"
|
2014-10-16 16:37:51 -04:00
|
|
|
printf '\033[0m'
|
2012-10-04 07:29:34 -04:00
|
|
|
fi
|
|
|
|
fi
|
2014-10-16 16:37:51 -04:00
|
|
|
[ $indent_level -eq 0 ] && rm "$stdout_file"
|
2012-10-04 07:29:34 -04:00
|
|
|
}
|
2012-10-04 12:43:49 -04:00
|
|
|
|
2014-10-17 17:16:12 -04:00
|
|
|
has_sh_or_no_shebang_line() {
|
|
|
|
head -n 1 "$1" | grep -vqE '^#!' && return 0 # no shebang line at all
|
|
|
|
head -n 1 "$1" | grep -qE '^#![[:blank:]]*/bin/sh($|[[:blank:]])' && return 0 # shebang line is '#!/bin/sh' or legal variations thereof
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
USAGE="usage: $0 [<options>] <test directory>"
|
2012-10-11 01:46:02 -04:00
|
|
|
|
2012-10-11 02:21:05 -04:00
|
|
|
urchin_help() {
|
2014-11-06 22:21:05 -05:00
|
|
|
cat <<EOF
|
|
|
|
|
|
|
|
$USAGE
|
|
|
|
|
|
|
|
-s <shell> Invoke test scripts that either have no shebang line at all or
|
|
|
|
have shebang line "#!/bin/sh" with the specified shell.
|
|
|
|
-f Force running even if the test directory's name does not
|
|
|
|
contain the word "test".
|
|
|
|
-x [Experimental; not meant for direct invocation, but for use in
|
|
|
|
the shebang line of test scripts]
|
|
|
|
Run with "\$TEST_SHELL", falling back on /bin/sh.
|
|
|
|
-h This help.
|
|
|
|
|
|
|
|
Go to https://github.com/tlevine/urchin for documentation on writing tests.
|
|
|
|
|
|
|
|
EOF
|
2012-10-11 02:21:05 -04:00
|
|
|
}
|
2012-10-11 01:46:02 -04:00
|
|
|
|
2013-06-20 13:56:29 -04:00
|
|
|
plural () {
|
|
|
|
# Make $1 a plural according to the number $2.
|
|
|
|
# If $3 is supplied, use that instead of "${1}s".
|
|
|
|
# Result is written to stdout.
|
|
|
|
if [ "$2" = 1 ]
|
|
|
|
then
|
|
|
|
printf '%s\n' "$1"
|
|
|
|
else
|
|
|
|
printf '%s\n' "${3-${1}s}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2012-10-11 02:21:05 -04:00
|
|
|
urchin_go() {
|
2014-10-16 16:37:51 -04:00
|
|
|
echo Running tests at $(date +%Y-%m-%dT%H:%M:%S) | tee "$logfile"
|
2013-06-28 05:48:43 -04:00
|
|
|
start=$(date +%s)
|
2012-10-11 01:10:43 -04:00
|
|
|
|
2014-10-17 17:16:12 -04:00
|
|
|
# Determine the environment variable to define for test scripts
|
|
|
|
# that reflects the specified or implied shell to use for shell-code tests.
|
|
|
|
# - Set it to the shell specified via -s, if any.
|
|
|
|
# - Otherwise, use its present value, if non-empty.
|
|
|
|
# - Otherwise, default to '/bin/sh'.
|
|
|
|
if [ -n "$2" ]
|
|
|
|
then
|
|
|
|
TEST_SHELL="$2"
|
|
|
|
elif [ -z "$TEST_SHELL" ]
|
|
|
|
then
|
|
|
|
TEST_SHELL='/bin/sh'
|
|
|
|
fi
|
|
|
|
|
|
|
|
recurse "$1" 0 "$2" # test folder -- indentation level -- [shell to invoke test scripts with]
|
2012-10-11 01:10:43 -04:00
|
|
|
|
2013-06-28 05:48:43 -04:00
|
|
|
finish=$(date +%s)
|
|
|
|
elapsed=$(($finish - $start))
|
2014-10-16 16:37:51 -04:00
|
|
|
echo "Done, took $elapsed $(plural second $elapsed)."
|
2013-06-20 13:56:29 -04:00
|
|
|
set -- $(grep -e 'passed$' "$logfile"|wc -l) $(grep -e 'failed$' "$logfile"|wc -l)
|
|
|
|
printf '%s\n' "$1 $(plural test "$1") passed."
|
2014-10-16 16:37:51 -04:00
|
|
|
[ $2 -gt 0 ] && printf '\033[31m' || printf '\033[32m' # If tests failed, print the message in red, otherwise in green.
|
2013-06-20 13:56:29 -04:00
|
|
|
printf '%s\n' "$2 $(plural test "$2") failed."
|
2014-10-16 16:37:51 -04:00
|
|
|
printf '\033[m'
|
2014-03-17 09:43:13 -04:00
|
|
|
return "$2"
|
2012-10-11 02:21:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
urchin_molly_guard() {
|
2014-10-16 16:37:51 -04:00
|
|
|
{
|
|
|
|
echo
|
|
|
|
echo 'The name of the directory on which you are running urchin'
|
|
|
|
echo 'does not contain the word "test", so I am not running,'
|
|
|
|
echo 'in case that was an accident. Use the -f flag if you really'
|
|
|
|
echo 'want to run urchin on that directory.'
|
|
|
|
echo
|
|
|
|
} >&2
|
2012-10-11 01:46:02 -04:00
|
|
|
exit 1
|
2012-10-11 02:21:05 -04:00
|
|
|
}
|
|
|
|
|
2014-10-17 17:16:12 -04:00
|
|
|
shell_for_sh_tests=
|
2014-10-16 16:37:51 -04:00
|
|
|
force=false
|
2012-10-11 02:21:05 -04:00
|
|
|
while [ $# -gt 0 ]
|
|
|
|
do
|
|
|
|
case "$1" in
|
2014-10-16 16:37:51 -04:00
|
|
|
-f) force=true;;
|
2014-10-17 17:16:12 -04:00
|
|
|
-s)
|
|
|
|
shift
|
|
|
|
shell_for_sh_tests=$1
|
|
|
|
which "$shell_for_sh_tests" >/dev/null || { echo "Cannot find specified shell: '$shell_for_sh_tests'" >&2; urchin_help >&2; exit 2; }
|
|
|
|
;;
|
2014-11-06 22:21:05 -05:00
|
|
|
-x) # `urchin -x <test-script>` is equivalent to `"$TEST_SHELL" <test-script>`
|
2014-11-05 12:46:19 -05:00
|
|
|
shift
|
2014-11-05 12:15:30 -05:00
|
|
|
urchinsh=${TEST_SHELL:-/bin/sh}
|
|
|
|
"$urchinsh" "$@"
|
|
|
|
exit $?;;
|
2013-06-18 17:21:31 -04:00
|
|
|
-h|--help) urchin_help
|
|
|
|
exit 0;;
|
2014-10-17 17:16:12 -04:00
|
|
|
-*) urchin_help >&2
|
2012-10-11 02:21:05 -04:00
|
|
|
exit 1;;
|
|
|
|
*) break;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
# Verify argument for main stuff
|
2014-10-16 16:37:51 -04:00
|
|
|
if [ "$#" != '1' ] || [ ! -d "$1" ]
|
2012-10-11 15:47:08 -04:00
|
|
|
then
|
2014-10-18 10:34:30 -04:00
|
|
|
[ -n "$1" ] && [ ! -d "$1" ] && echo "Not a directory: '$1'" >&2
|
2014-10-16 16:37:51 -04:00
|
|
|
echo "$USAGE" >&2
|
2014-10-17 17:16:12 -04:00
|
|
|
exit 2
|
2012-10-11 15:47:08 -04:00
|
|
|
fi
|
2012-10-11 02:21:05 -04:00
|
|
|
|
2014-10-16 16:37:51 -04:00
|
|
|
# Constants
|
|
|
|
logfile=$(fullpath "$1")/.urchin.log
|
|
|
|
stdout_file=$(fullpath "$1")/.urchin_stdout
|
|
|
|
|
2012-10-11 02:21:05 -04:00
|
|
|
# Run or present the Molly guard.
|
2014-10-16 16:37:51 -04:00
|
|
|
if basename "$(fullpath "$1")" | grep -Fi 'test' > /dev/null || $force
|
2012-10-11 02:21:05 -04:00
|
|
|
then
|
2014-10-17 17:16:12 -04:00
|
|
|
urchin_go "$1" "$shell_for_sh_tests"
|
2012-10-11 02:21:05 -04:00
|
|
|
else
|
|
|
|
urchin_molly_guard
|
2012-10-08 10:43:14 -04:00
|
|
|
fi
|