2013-06-19 06:46:31 +00:00
|
|
|
#!/bin/sh
|
2012-10-04 11:24:03 +00:00
|
|
|
|
Make sure that CDPATH isn't set, as it causes `cd` to behave unpredictably - notably, it can produce output, which breaks fullpath().
Also: Improved CLI help, updated URLs in read-me, cleaned up package.json:
I've tried to clarify the intent of `-x` in the CLI help, but I haven't touched the read-me in that respect.
I don't see any benefit to `-x`:
* Just using `#/bin/sh` as the shebang line in combination with `-s <shell>` gives you the same functionality,
* When it comes to invoking scripts from _within_ test scripts, nothing can do the work for you: you consciously have to mark the invocation with _something_ to indicate that it should be controlled from the outside; it won't get any easier than `$TEST_SHELL ...`
* Finally, using a shebang line such as `#!/usr/bin/env urchin -x` is problematic for two reasons:
* Some platforms can handle only *1* argument in a shebang line.
* In a _package-local_ installation, `#!/usr/bin/env` may not find the Urchin executable.
I'm also not sure how the following (from `readme.md`) fits in the picture:
> It might make sense if you do this.
export TEST_SHELL=zsh && urchin -x
export TEST_SHELL=bash && urchin -x
(As an aside: To achieve the same thing, you don't need `export`; `TEST_SHELL=zsh urchin -x` and `TEST_SHELL=bash urchin -x` is the better choice.)
How does this relate to use in a _shebang line_?
`urchin_help()` now uses a here-doc: easier to maintain, and should work in all Bourne-like shells.
`readmeFilename` removed from `package.json`:
> "The readmeFilename does not need to ever be in your actual package.json file" - npm/npm#3573
2014-12-03 14:48:49 +00:00
|
|
|
# Make sure that CDPATH isn't set, as it causes `cd` to behave unpredictably - notably, it can produce output,
|
|
|
|
# which breaks fullpath().
|
|
|
|
unset CDPATH
|
|
|
|
|
2012-10-11 18:50:03 +00:00
|
|
|
fullpath() {
|
|
|
|
(
|
2013-07-26 11:51:01 +00:00
|
|
|
cd -- "$1"
|
2012-10-11 18:50:03 +00:00
|
|
|
pwd
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2012-10-11 00:43:13 +00:00
|
|
|
indent() {
|
|
|
|
level="$1"
|
|
|
|
printf "%$((2 * ${level}))s"
|
|
|
|
}
|
|
|
|
|
2012-10-04 11:29:34 +00:00
|
|
|
recurse() {
|
|
|
|
potential_test="$1"
|
2012-10-11 00:43:13 +00:00
|
|
|
indent_level="$2"
|
2014-10-17 21:16:12 +00:00
|
|
|
shell_for_sh_tests="$3"
|
2012-10-10 18:40:49 +00:00
|
|
|
|
2012-10-10 18:43:41 +00:00
|
|
|
[ "$potential_test" = 'setup_dir' ] && return
|
|
|
|
[ "$potential_test" = 'teardown_dir' ] && return
|
|
|
|
[ "$potential_test" = 'setup' ] && return
|
|
|
|
[ "$potential_test" = 'teardown' ] && return
|
2012-10-10 18:40:49 +00:00
|
|
|
|
2014-10-16 20:37:51 +00:00
|
|
|
[ $indent_level -eq 0 ] && : > "$stdout_file"
|
2012-10-11 00:31:18 +00:00
|
|
|
|
2012-10-04 11:29:34 +00:00
|
|
|
if [ -d "$potential_test" ]
|
|
|
|
then
|
|
|
|
(
|
2012-10-11 00:43:13 +00:00
|
|
|
indent $indent_level
|
|
|
|
echo " ${potential_test}"
|
2013-07-26 11:51:01 +00:00
|
|
|
cd -- "$potential_test"
|
2013-06-19 06:34:07 +00:00
|
|
|
[ -f setup_dir ] && [ -x setup_dir ] && ./setup_dir >> "$stdout_file"
|
2014-11-05 17:38:22 +00:00
|
|
|
|
|
|
|
if [ -n "$ZSH_VERSION" ]; then
|
|
|
|
# avoid "no matches found: *" error when directories are empty
|
|
|
|
setopt NULL_GLOB
|
|
|
|
fi
|
|
|
|
|
2012-10-08 14:16:49 +00:00
|
|
|
for test in *
|
2012-10-11 00:31:18 +00:00
|
|
|
do
|
2013-06-19 06:34:07 +00:00
|
|
|
[ -f setup ] && [ -x setup ] && ./setup >> "$stdout_file"
|
2012-10-11 00:43:13 +00:00
|
|
|
|
|
|
|
# $2 instead of $indent_level so it doesn't clash
|
2014-10-17 21:16:12 +00:00
|
|
|
recurse "${test}" $(( $2 + 1 )) "$shell_for_sh_tests"
|
2016-01-25 14:41:21 +00:00
|
|
|
exit_code=$?
|
2012-10-11 00:43:13 +00:00
|
|
|
|
2016-01-25 14:41:21 +00:00
|
|
|
if $exit_on_fail && test $exit_code -ne 0; then
|
2016-01-25 14:35:54 +00:00
|
|
|
[ -f teardown ] && [ -x teardown ] && ./teardown >> "$stdout_file"
|
|
|
|
[ -f teardown_dir ] && [ -x teardown_dir ] && ./teardown_dir >> "$stdout_file"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2013-06-19 06:34:07 +00:00
|
|
|
[ -f teardown ] && [ -x teardown ] && ./teardown >> "$stdout_file"
|
2012-10-08 12:50:48 +00:00
|
|
|
done
|
2013-06-19 06:34:07 +00:00
|
|
|
[ -f teardown_dir ] && [ -x teardown_dir ] && ./teardown_dir >> "$stdout_file"
|
2012-10-11 05:17:20 +00:00
|
|
|
echo
|
2012-10-04 11:29:34 +00:00
|
|
|
)
|
2012-10-08 12:59:14 +00:00
|
|
|
elif [ -x "$potential_test" ]
|
2012-10-04 11:29:34 +00:00
|
|
|
then
|
2012-10-10 18:25:44 +00:00
|
|
|
|
2013-06-19 06:34:07 +00:00
|
|
|
[ -f setup ] && [ -x setup ] && ./setup >> "$stdout_file"
|
2012-10-10 19:47:21 +00:00
|
|
|
|
2012-10-10 18:25:44 +00:00
|
|
|
# Run the test
|
2014-10-17 21:16:12 +00: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 18:40:49 +00:00
|
|
|
exit_code="$?"
|
2012-10-10 19:47:21 +00:00
|
|
|
|
2013-06-19 06:34:07 +00:00
|
|
|
[ -f teardown ] && [ -x teardown ] && ./teardown >> "$stdout_file"
|
2012-10-10 18:25:44 +00:00
|
|
|
|
2016-01-27 00:32:51 +00:00
|
|
|
if $tap_format; then
|
|
|
|
sleep 0s
|
2012-10-04 11:29:34 +00:00
|
|
|
else
|
2016-01-27 00:32:51 +00:00
|
|
|
indent $indent_level
|
|
|
|
if [ $exit_code -eq 0 ]
|
|
|
|
then
|
|
|
|
# On success, print a green '✓'
|
|
|
|
printf '\033[32m✓ \033[0m'
|
|
|
|
printf '%s\n' "${potential_test}"
|
|
|
|
printf '%s\n' "${potential_test} passed" >> "$logfile"
|
|
|
|
else
|
|
|
|
# On fail, print a red '✗'
|
|
|
|
printf '\033[31m✗ \033[0m'
|
|
|
|
printf '%s\n' "${potential_test}"
|
|
|
|
printf '%s\n' "${potential_test} failed" >> "$logfile"
|
|
|
|
printf '\033[31m' # Print output captured from failed test in red.
|
|
|
|
cat "$stdout_file"
|
|
|
|
printf '\033[0m'
|
|
|
|
fi
|
2012-10-04 11:29:34 +00:00
|
|
|
fi
|
2016-01-25 14:35:54 +00:00
|
|
|
|
2016-01-25 14:41:21 +00:00
|
|
|
if $exit_on_fail && test 0 -ne $exit_code; then
|
|
|
|
return 1
|
2016-01-25 14:35:54 +00:00
|
|
|
fi
|
2012-10-04 11:29:34 +00:00
|
|
|
fi
|
2014-10-16 20:37:51 +00:00
|
|
|
[ $indent_level -eq 0 ] && rm "$stdout_file"
|
2012-10-04 11:29:34 +00:00
|
|
|
}
|
2012-10-04 16:43:49 +00:00
|
|
|
|
2014-10-17 21:16:12 +00: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 05:46:02 +00:00
|
|
|
|
2012-10-11 06:21:05 +00:00
|
|
|
urchin_help() {
|
2014-11-07 03:21:05 +00: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.
|
2016-01-25 14:20:40 +00:00
|
|
|
-e Stop running if any single test fails. This is helpful if you want
|
|
|
|
to use Urchin to run things other than tests, such as a set of
|
|
|
|
configuration scripts.
|
2014-11-07 03:21:05 +00:00
|
|
|
-f Force running even if the test directory's name does not
|
|
|
|
contain the word "test".
|
2016-01-27 00:32:51 +00:00
|
|
|
-t Format output in Test Anything Protocol (TAP)
|
2014-11-07 03:21:05 +00:00
|
|
|
-h This help.
|
|
|
|
|
|
|
|
Go to https://github.com/tlevine/urchin for documentation on writing tests.
|
|
|
|
|
|
|
|
EOF
|
2012-10-11 06:21:05 +00:00
|
|
|
}
|
2012-10-11 05:46:02 +00:00
|
|
|
|
2013-06-20 17:56:29 +00: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 06:21:05 +00:00
|
|
|
urchin_go() {
|
2014-10-16 20:37:51 +00:00
|
|
|
echo Running tests at $(date +%Y-%m-%dT%H:%M:%S) | tee "$logfile"
|
2013-06-28 09:48:43 +00:00
|
|
|
start=$(date +%s)
|
2012-10-11 05:10:43 +00:00
|
|
|
|
2014-10-17 21:16:12 +00: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 05:10:43 +00:00
|
|
|
|
2013-06-28 09:48:43 +00:00
|
|
|
finish=$(date +%s)
|
|
|
|
elapsed=$(($finish - $start))
|
2016-01-27 00:32:51 +00:00
|
|
|
if $tap_format; then
|
|
|
|
sleep 0s
|
|
|
|
else
|
|
|
|
echo "Done, took $elapsed $(plural second $elapsed)."
|
|
|
|
set -- $(grep -e 'passed$' "$logfile"|wc -l) $(grep -e 'failed$' "$logfile"|wc -l)
|
|
|
|
printf '%s\n' "$1 $(plural test "$1") passed."
|
|
|
|
[ $2 -gt 0 ] && printf '\033[31m' || printf '\033[32m' # If tests failed, print the message in red, otherwise in green.
|
|
|
|
printf '%s\n' "$2 $(plural test "$2") failed."
|
|
|
|
printf '\033[m'
|
|
|
|
fi
|
2014-03-17 13:43:13 +00:00
|
|
|
return "$2"
|
2012-10-11 06:21:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
urchin_molly_guard() {
|
2014-10-16 20:37:51 +00: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 05:46:02 +00:00
|
|
|
exit 1
|
2012-10-11 06:21:05 +00:00
|
|
|
}
|
|
|
|
|
2014-10-17 21:16:12 +00:00
|
|
|
shell_for_sh_tests=
|
2014-10-16 20:37:51 +00:00
|
|
|
force=false
|
2016-01-25 14:35:54 +00:00
|
|
|
exit_on_fail=false
|
2016-01-27 00:32:51 +00:00
|
|
|
tap_format=false
|
2012-10-11 06:21:05 +00:00
|
|
|
while [ $# -gt 0 ]
|
|
|
|
do
|
|
|
|
case "$1" in
|
2016-01-25 14:35:54 +00:00
|
|
|
-e) exit_on_fail=true;;
|
2014-10-16 20:37:51 +00:00
|
|
|
-f) force=true;;
|
2014-10-17 21:16:12 +00: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; }
|
|
|
|
;;
|
2016-01-27 00:32:51 +00:00
|
|
|
-t) tap_format=true;;
|
2013-06-18 21:21:31 +00:00
|
|
|
-h|--help) urchin_help
|
|
|
|
exit 0;;
|
2014-10-17 21:16:12 +00:00
|
|
|
-*) urchin_help >&2
|
2012-10-11 06:21:05 +00:00
|
|
|
exit 1;;
|
|
|
|
*) break;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
# Verify argument for main stuff
|
2014-10-16 20:37:51 +00:00
|
|
|
if [ "$#" != '1' ] || [ ! -d "$1" ]
|
2012-10-11 19:47:08 +00:00
|
|
|
then
|
2014-10-18 14:34:30 +00:00
|
|
|
[ -n "$1" ] && [ ! -d "$1" ] && echo "Not a directory: '$1'" >&2
|
2014-10-16 20:37:51 +00:00
|
|
|
echo "$USAGE" >&2
|
2014-10-17 21:16:12 +00:00
|
|
|
exit 2
|
2012-10-11 19:47:08 +00:00
|
|
|
fi
|
2012-10-11 06:21:05 +00:00
|
|
|
|
2014-10-16 20:37:51 +00:00
|
|
|
# Constants
|
|
|
|
logfile=$(fullpath "$1")/.urchin.log
|
|
|
|
stdout_file=$(fullpath "$1")/.urchin_stdout
|
|
|
|
|
2012-10-11 06:21:05 +00:00
|
|
|
# Run or present the Molly guard.
|
2014-10-16 20:37:51 +00:00
|
|
|
if basename "$(fullpath "$1")" | grep -Fi 'test' > /dev/null || $force
|
2012-10-11 06:21:05 +00:00
|
|
|
then
|
2014-10-17 21:16:12 +00:00
|
|
|
urchin_go "$1" "$shell_for_sh_tests"
|
2012-10-11 06:21:05 +00:00
|
|
|
else
|
|
|
|
urchin_molly_guard
|
2012-10-08 14:43:14 +00:00
|
|
|
fi
|