urchin/urchin

777 lines
23 KiB
Plaintext
Raw Normal View History

#!/bin/sh
2012-10-04 11:24:03 +00:00
2016-02-29 04:11:37 +00:00
# ----------------------------------------------------------------------
2016-02-29 04:09:46 +00:00
# Copyright (c) 2013, 2014, 2015, 2016 Thomas Levine
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2016-02-29 04:11:37 +00:00
# ----------------------------------------------------------------------
2016-02-08 16:09:00 +00:00
2016-02-29 04:09:46 +00:00
2016-02-29 04:11:37 +00:00
# ----------------------------------------------------------------------
2016-02-29 04:09:46 +00:00
# Copyright (c) 2014, Michael Klement
# Copyright (c) 2012, ScraperWiki Limited
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
2016-02-29 04:11:37 +00:00
# ----------------------------------------------------------------------
2016-02-28 01:18:55 +00:00
set -e
2016-02-08 16:09:00 +00:00
2016-04-10 08:45:11 +00:00
# Delimiters
2016-04-10 17:42:05 +00:00
LF="$(printf '\n')"
HT="$(printf '\t')"
DELIMITERS="[${LF}${HT}]"
2016-04-10 08:45:11 +00:00
2016-04-08 22:24:55 +00:00
# Urchin version number
2016-04-08 22:29:50 +00:00
VERSION=0.0.0-master
2016-04-08 22:24:55 +00:00
2016-03-06 14:19:04 +00:00
# Kill subprocesses on interrupt.
2016-03-06 14:23:04 +00:00
trap "kill -$$; exit" HUP INT TERM
2016-03-06 14:19:04 +00:00
2016-04-04 21:21:24 +00:00
DEFAULT_SHELLS='
sh
bash
dash
ksh
posh
pdksh
mksh
yash
zsh
'
2016-03-02 20:19:33 +00:00
if [ -n "${ZSH_VERSION}" ]; then
2016-02-29 02:32:58 +00:00
# avoid "no matches found: *" error when directories are empty
setopt NULL_GLOB
emulate sh
fi
2016-04-08 22:24:55 +00:00
# -------------------- Usage --------------------
USAGE="usage: $0 [options]... [test file or directory]..."
urchin_help() {
cat <<EOF
${USAGE}
By default, Urchin checks for the following shells and runs every
particular test file once per shell.
$(echo "${DEFAULT_SHELLS}" | sed 's/ /\n /g')
On each run,
1. The TEST_SHELL environment variable is set to the particular shell.
2. If the test file lacks a shebang line or has a shebang line of
"#!/bin/sh", the test script is also executed in that shell.
The following flags affect how this multiple-shell testing is handled.
-s, --shell <shell> Tell Urchin to use a different list of shells.
(You can pass this flag multiple times.)
-n, --disable-cycling Disable the cycling of shells; Urchin will
execute test files ordinarily, implicitly using
sh for files that lack shebang lines. It will
set the TEST_SHELL variable to "/bin/sh" if and
only if TEST_SHELL is empty or undefined.
The following flags affect how Urchin processes tests.
-b, --run-in-series Run tests in series. The default is to run tests
in parallel where possible.
-e, --exit-on-fail Stop running if any single test fails.
This can be useful if you are running something
other than test files with Urchin.
-T, --timeout <seconds> Kill a test if it runs for longer than the
specified duration. The default is no timeout.
-f, --force Force running even if the test directory's name
does not contain the word "test".
These options affect how results are formatted. Options -q, and -v
2016-04-08 22:44:29 +00:00
have no effect when combined with formats other than "urchin".
-vv, -vvv, and -vvvv do have effect when combined with formats "urchin"
or "tap".
2016-04-08 22:24:55 +00:00
-p, --pretty Print results in color and with fancy symbols.
2016-04-08 22:44:29 +00:00
-F, --format <name> XXX
2016-04-08 22:24:55 +00:00
And these options affect how much is printed.
-q, --quiet Print nothing to stdout;
the only output is the exit code.
(default verbosity) Print names of failed tests and counts
of passed, failed, and skipped tests.
-v Print stdout from failing tests.
-vv Print names of passed tests.
-vvv, --verbose Print stdout from all tests.
-vvvv, --debug Run with set -x.
The remaining flags provide information about urchin.
-h, --help Display this help.
--version Display the version number.
Urchin recognizes certain environment variables.
TEST_SHELL This is sometimes over-ridden; see -s.
2016-04-10 16:40:36 +00:00
RUN_IN_SERIES Set this to have the same effect as
2016-04-08 22:24:55 +00:00
-b/--run-in-series. This is helpful if you are
calling urchin inside an urchin test suite.
Go to https://thomaslevine.com/!/urchin/ for documentation on writing tests.
EOF
}
2016-04-10 15:34:06 +00:00
# -------------------- Dependency checks --------------------
if command -v md5 1> /dev/null 2> /dev/null; then
urchin_md5=md5
elif command -v md5sum 1> /dev/null 2> /dev/null; then
urchin_md5=md5sum
else
echo Could not find MD5 hash command >&2
exit 1
fi
if epoch_date 2>&1 > /dev/null; then
epoch=epoch_date
elif epoch_pax 2>&1 > /dev/null; then
epoch=epoch_pax
else
echo I could not find a seconds counter. >&2
exit 1
fi
2016-04-08 22:24:55 +00:00
# -------------------- Portable wrappers --------------------
2016-04-07 02:42:29 +00:00
mktemp_dir() {
# Support HP-UX mktemp that has wrong exit codes and
# can't make directories.
2016-04-07 03:51:01 +00:00
tmp=$(mktemp /tmp/urchin.XXXXXXXX)
2016-04-07 02:44:22 +00:00
if test -f "${tmp}"; then
rm "${tmp}"
2016-04-07 02:42:29 +00:00
fi
2016-04-07 02:44:22 +00:00
mkdir "${tmp}"
2016-04-07 02:48:58 +00:00
echo "${tmp}"
2016-04-07 02:42:29 +00:00
}
mktemp_file() {
2016-04-07 03:51:01 +00:00
tmp=$(mktemp /tmp/urchin.XXXXXXXX)
2016-04-07 02:42:29 +00:00
if ! test -f "${tmp}"; then
> "${tmp}"
fi
2016-04-07 02:48:58 +00:00
echo "${tmp}"
2016-04-07 02:42:29 +00:00
}
2016-04-08 22:25:58 +00:00
# -------------------- Temporary directory --------------------
urchin_tmp=$(mktemp_dir)
> "${urchin_tmp}/log"
urchin_exit() {
rm -Rf "${urchin_tmp}"
exit "$@"
}
2016-04-08 22:24:55 +00:00
# -------------------- Utilities --------------------
2016-04-08 22:25:58 +00:00
epoch_date() {
date +%s
}
epoch_pax() {
# Based on http://stackoverflow.com/a/7262588/407226
tmp="$(mktemp_file)"
echo "ibase=8;$({ pax -wx cpio "${tmp}"; echo; } | cut -c 48-59)" | bc
rm "${tmp}"
}
2016-04-08 22:24:55 +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.
2016-04-10 16:40:36 +00:00
if [ "${2}" = 1 ]; then
echo "${1}"
2016-04-08 22:24:55 +00:00
else
2016-04-10 16:40:36 +00:00
echo "${3-${1}s}"
2016-04-08 22:24:55 +00:00
fi
}
has_shebang_line() {
head -n 1 "${1}" | grep -v '^#!/bin/sh$' | grep -q '^#!'
}
indent() {
level="${1}"
if test "${level}" -gt 0; then
printf "%$((2 * ${level}))s"
fi
}
2016-04-07 02:42:29 +00:00
2016-02-28 13:12:55 +00:00
stdout_file() {
2016-03-02 20:19:33 +00:00
the_test="${1}"
the_shell="${2}"
2016-02-28 20:31:17 +00:00
2016-03-02 20:19:33 +00:00
x="${urchin_tmp}/stdout$(fullpath "$the_test")"
mkdir -p "${x}"
2016-04-07 00:06:02 +00:00
case "${urchin_md5}" in
md5sum) y=$(echo "${the_shell}" | md5sum | cut -d\ -f1) ;;
2016-04-07 03:55:43 +00:00
md5) y=$(echo "${the_shell}" | md5 | sed 's/.* //') ;;
2016-04-07 00:06:02 +00:00
*) echo md5 command is not configured >&2; urchin_exit 1;;
esac
echo "${x}/${y}"
2016-02-28 13:12:55 +00:00
}
2016-02-28 09:12:57 +00:00
# Expand relative paths
fullpath() {
2016-03-02 20:19:33 +00:00
readlink -f -- "${1}"
}
2016-02-28 09:12:57 +00:00
2016-04-10 15:48:30 +00:00
contains() {
case "$#" in
1) grep "${1}" > /dev/null ;;
2) echo "${1}" | contains "${2}" ;;
3) contains "${1}" "${2}" && contains "${1}" "${3}" ;;
*) container="${1}" && shift
contains "${container}" "${1}" && shift &&
contains "${container}" "${@}" ;;
esac
}
2016-04-10 16:40:36 +00:00
is_set() {
set | grep "^${1}=" > /dev/null
}
2016-02-28 11:22:51 +00:00
remove_trailing_slash() {
echo "$1" | sed s/\\/$//
}
2016-04-10 16:02:23 +00:00
# Find the root directory of the present test suite
2016-02-28 11:18:39 +00:00
urchin_root() {
2016-02-28 11:22:51 +00:00
# Call recursively but remember the original argument.
2016-04-10 16:03:57 +00:00
orig="${2:-$1}"
2016-04-10 16:02:23 +00:00
current="${1%/}"
2016-02-28 11:18:39 +00:00
2016-04-10 16:03:57 +00:00
abscurrent="$(fullpath "${current}")"
2016-03-02 20:19:33 +00:00
if test "${abscurrent}" = / ||
2016-04-10 15:50:18 +00:00
basename "${abscurrent}" | contains '^\.' ; then
2016-02-28 22:49:57 +00:00
# Stop traversing upwards at / and at hidden directories.
2016-03-02 20:19:33 +00:00
if test -d "${orig}"; then
echo "${orig}"
2016-02-28 11:22:51 +00:00
else
2016-03-02 20:19:33 +00:00
dirname -- "${orig}"
2016-02-28 11:22:51 +00:00
fi
2016-03-02 20:19:33 +00:00
elif ! test -e "${current}"; then
echo "${current}: No such file or directory">&2
2016-02-28 11:22:51 +00:00
return 1
2016-03-02 20:19:33 +00:00
elif test -f "${current}"; then
urchin_root "$(dirname -- "${current}")" "${orig}"
elif test -f "${current}"/.urchin_root; then
2016-04-10 16:02:23 +00:00
echo "${current}"
2016-02-28 11:22:51 +00:00
else
2016-03-02 20:19:33 +00:00
urchin_root "${current}"/.. "${orig}"
2016-02-28 11:22:51 +00:00
fi
2016-02-28 11:18:39 +00:00
}
2016-04-10 18:42:58 +00:00
# -------------------- Metafunctions --------------------
meta_verbosity() {
echo "if test \${${1}} -gt ${2}; then ${3}=true; fi"
}
2016-04-10 18:23:24 +00:00
# -------------------- Printing output --------------------
2016-04-10 18:42:58 +00:00
# Format functions may read a log file from stdin.
2016-04-10 18:23:24 +00:00
format_tap() {
2016-04-10 18:42:58 +00:00
v="${1}"
tmp_dir="${2}"
2016-04-10 18:23:24 +00:00
2016-04-10 18:42:58 +00:00
$(verbosity v 2 print_not_ok_stdout)
$(verbosity v 3 print_ok_stdout)
2016-04-10 18:23:24 +00:00
2016-04-10 18:42:58 +00:00
while IFS="${HT}" read remote the_shell path result file_elapsed; do
# Number of files that have run, including this one
n=$(( ${n:-0} + 1))
2016-04-10 18:23:24 +00:00
2016-04-10 18:42:58 +00:00
if test -z "${the_shell}"; then
the_shell='File is not executable.'
fi
case "${result}" in
ok) "ok $n - ${path} (${the_shell}${remote})" ;;
not_ok) "not_ok $n - ${path} (${the_shell}${remote})" ;;
skip) "ok $n - ${path} (${the_shell}${remote}) # SKIP" ;;
esac
if { test "${result}" = not_ok && "${print_not_ok_stdout}"; } ||
{ test "${result}" = ok && "${print_ok_stdout}"; }; then
echo '# ------------ Begin output ------------'
sed 's/^/# /' "$(stdout_file "${path}" "${the_shell}")"
echo '# ------------ End output ------------'
fi
echo "# Previous test took ${file_elapsed} seconds."
2016-04-10 18:23:24 +00:00
2016-04-10 18:42:58 +00:00
# Number of files that have been ok, not ok, and skipped
eval "old_count=${result}s"
eval "${result}s=$(($old_count+1))"
done
}
2016-04-10 18:23:24 +00:00
format_urchin() {
2016-04-10 18:42:58 +00:00
v="${1}"
tmp_dir="${2}"
verbosity="${3}"
print_in_color="${4}"
$(verbosity v 1 print_margins)
$(verbosity v 1 print_not_ok)
$(verbosity v 2 print_not_ok_stdout)
$(verbosity v 2 print_ok)
$(verbosity v 3 print_ok_stdout)
2016-04-10 18:23:24 +00:00
2016-04-10 18:42:58 +00:00
if $print_in_color; then
success_mark=$(printf "\033[32m✓ \033[0m")
fail_mark=$(printf "\033[31m✗ \033[0m")
else
success_mark=.\
fail_mark=F\
fi
2016-04-10 18:23:24 +00:00
header() {
if test "${prevdir}" != "${currentdir}"; then
echo
fi
if test "${prevpath}" != "${path}"; then
printf "$(dirname -- "${path}")/\n> $(basename -- "${path}")\n"
fi
}
2016-04-10 18:42:58 +00:00
while IFS="${HT}" read remote the_shell path result file_elapsed; do
abspath=${urchin_tmp}/${path}
currentdir="$(dirname -- "${path}")"
prevdir="${currentdir}"
case "${result}" in
ok)
if "${print_ok}"; then
header
2016-04-10 18:23:24 +00:00
printf "${success_mark} "
2016-04-10 18:42:58 +00:00
echo "${the_shell} ("${file_elapsed}" $(plural second $file_elapsed))"
2016-04-10 18:23:24 +00:00
fi
2016-04-10 18:42:58 +00:00
;;
not_ok)
if "${print_not_ok}"; then
header
2016-04-10 18:23:24 +00:00
printf "${fail_mark} "
2016-04-10 18:42:58 +00:00
echo "${the_shell} ("${file_elapsed}" $(plural second $file_elapsed))"
2016-04-10 18:23:24 +00:00
fi
2016-04-10 18:42:58 +00:00
;;
skip)
if "${print_ok}"; then
header
if test -z "${the_shell}"; then
echo ' (Specified shell is not executable.)'
else
echo " ${the_shell} ("${file_elapsed}" $(plural second $file_elapsed))"
fi
2016-04-10 18:23:24 +00:00
fi
2016-04-10 18:42:58 +00:00
;;
esac
if { test "${result}" = not_ok && "${print_not_ok_stdout}"; } ||
{ test "${result}" = ok && "${print_ok_stdout}"; }; then
sed 's/^/ | /' "$(stdout_file "${path}" "${the_shell}")"
fi
prevpath="${path}"
done
2016-04-10 18:23:24 +00:00
}
2016-04-08 22:24:55 +00:00
# -------------------- Main stuff --------------------
2012-10-04 11:29:34 +00:00
recurse() {
2016-03-02 20:19:33 +00:00
requested_path="${1}"
potential_test="$(fullpath "${2}")"
cycle_shell="${3}"
TEST_SHELL="${4}"
2016-02-28 10:27:49 +00:00
2016-04-10 16:49:14 +00:00
root="$(urchin_root "${1}")"
2016-02-28 10:00:53 +00:00
for ignore in setup_dir teardown_dir setup teardown; do
2016-03-02 20:19:33 +00:00
if test "$(basename "${potential_test}")" = "${ignore}"; then
2016-02-28 10:00:53 +00:00
return
fi
done
2012-10-10 18:40:49 +00:00
2016-04-10 17:53:11 +00:00
# Return if we should not run this file
if contains "${potential_test}" "^${requested_path}" ||
contains "${requested_path}" "^${potential_test}" ; then
2016-03-06 10:22:15 +00:00
if test "$(dirname "${potential_test}")" = \
"$(dirname "${requested_path}")" &&
test "${potential_test}" != "${requested_path}"; then
return 0
fi
else
2016-02-28 12:14:16 +00:00
return 0
2016-03-06 10:22:15 +00:00
fi
2016-02-28 12:02:57 +00:00
2016-04-10 17:53:11 +00:00
if contains "${potential_test}" "${HT}"; then
echo 'Test file names may not contain tabs (HT).' >&2
2016-04-10 09:05:08 +00:00
urchin_exit 11
fi
2016-02-28 12:02:57 +00:00
2016-03-02 22:02:21 +00:00
if [ -x "${potential_test}" ]; then
if [ -d "${potential_test}" ]; then
(
cd -- "${potential_test}"
2016-04-10 15:50:18 +00:00
if test -f .urchin_dir && grep series .urchin_dir > /dev/null; then
2016-03-06 14:44:39 +00:00
run_in_series_dir=true
else
run_in_series_dir=false
fi
2016-04-01 19:52:34 +00:00
if test -f setup_dir; then
2016-04-04 01:22:05 +00:00
. ./setup_dir
2016-04-01 19:52:34 +00:00
fi
2014-11-05 17:38:22 +00:00
2016-02-28 01:18:55 +00:00
for test in *; do
2016-03-02 20:19:33 +00:00
if test "${test}" = '*' && ! test -e "${test}"; then
2016-02-28 12:35:27 +00:00
# The directory is empty.
break
fi
2012-10-11 00:43:13 +00:00
recurse "${requested_path}" "${test}" "${cycle_shell}" \
"${TEST_SHELL}" &
2016-02-28 16:05:03 +00:00
2016-03-06 14:44:39 +00:00
if "${run_in_series}" || "${run_in_series_dir}"; then
2016-03-02 20:19:33 +00:00
if wait "${!}"; then exit_code=0; else exit_code="${?}"; fi
if "${exit_on_not_ok}" && test "${exit_code}" -ne 0; then
2016-04-01 19:52:34 +00:00
if test -f teardown_dir; then
. ./teardown_dir
fi
2016-02-29 01:03:17 +00:00
return 1
2016-02-28 16:05:03 +00:00
fi
fi
2012-10-08 12:50:48 +00:00
done
2016-02-28 16:12:13 +00:00
wait
2016-04-01 19:52:34 +00:00
if test -f teardown_dir; then
. ./teardown_dir
fi
2016-02-28 10:00:53 +00:00
)
2016-03-02 22:02:21 +00:00
elif [ -f "${potential_test}" ]; then
2016-03-02 20:19:33 +00:00
cd -- "$(dirname -- "${potential_test}")"
2016-02-28 14:01:17 +00:00
# Determine the environment variable to define for test scripts
# that reflects the specified or implied shell to use for shell-code tests.
2016-02-28 16:32:06 +00:00
while read the_test_shell; do
(
2016-04-01 19:52:34 +00:00
if test -f setup; then
. ./setup
fi
2016-02-28 16:32:06 +00:00
# Run the test
2016-04-07 02:52:40 +00:00
start=$("${epoch}")
2016-02-28 16:32:06 +00:00
set +e
2016-02-28 16:53:49 +00:00
{
2016-03-02 20:19:33 +00:00
if "${cycle_shell}"; then
if has_shebang_line "${potential_test}"; then
TEST_SHELL="${the_test_shell}" $TIMEOUT "${potential_test}"
else
2016-03-02 20:19:33 +00:00
TEST_SHELL="${the_test_shell}" $TIMEOUT \
"${the_test_shell}" "${potential_test}"
fi
2016-02-28 16:53:49 +00:00
else
2016-02-29 02:13:40 +00:00
# Shell cycling is disabled with -n; use the present value of
# TEST_SHELL or default to /bin/sh
2016-03-02 20:19:33 +00:00
if [ -n "${TEST_SHELL}" ]; then
$TIMEOUT "${potential_test}"
2016-02-29 02:13:40 +00:00
else
2016-03-02 20:19:33 +00:00
TEST_SHELL=/bin/sh $TIMEOUT "${potential_test}"
2016-02-29 02:13:40 +00:00
fi
2016-02-28 16:53:49 +00:00
fi
2016-03-02 20:19:33 +00:00
} > "$(stdout_file "${potential_test}" "${the_test_shell}")" 2>&1
exit_code="${?}"
2016-02-28 16:32:06 +00:00
set -e
2016-04-07 02:52:40 +00:00
finish=$("${epoch}")
2016-02-28 16:32:06 +00:00
2016-04-01 19:52:34 +00:00
if test -f teardown; then
2016-04-04 01:22:05 +00:00
. ./teardown
2016-04-01 19:52:34 +00:00
fi
2016-02-28 16:32:06 +00:00
2016-03-02 20:19:33 +00:00
if [ "${exit_code}" -eq 0 ]; then
2016-02-28 16:32:06 +00:00
result=ok
2016-03-02 20:19:33 +00:00
elif [ "${exit_code}" -eq 3 ]; then
2016-02-28 16:32:06 +00:00
result=skip
else
result=not_ok
fi
2016-03-02 20:29:33 +00:00
elapsed=$(($finish - $start))
2016-04-10 16:49:14 +00:00
rel="${potential_test##"${root}/"}"
2016-04-10 17:53:11 +00:00
printf "\t${the_test_shell}\t${rel}\t${result}\t${elapsed}\n" \
2016-03-02 20:19:33 +00:00
>> "${urchin_tmp}"/log
exit "${exit_code}"
2016-02-28 16:32:06 +00:00
) &
2016-03-02 20:19:33 +00:00
if "${run_in_series}"; then
if wait "${!}"; then exit_code=0; else exit_code="${?}"; fi
if "${exit_on_not_ok}" && test "${exit_code}" -ne 0; then
2016-04-01 19:52:34 +00:00
if test -f teardown_dir; then
. ./teardown_dir
fi
2016-02-28 16:32:06 +00:00
return 1
fi
fi
2016-03-02 20:19:33 +00:00
done < "${shell_list}"
2016-02-28 16:32:06 +00:00
wait
2012-10-04 11:29:34 +00:00
else
2016-04-04 21:37:47 +00:00
echo "${potential_test}: Neither file nor directory!?" >&2
fi
2016-03-02 22:02:21 +00:00
else
# Shell is ''
2016-04-10 17:53:11 +00:00
printf "\t\t${potential_test}\tskip\t0\n" >> "${urchin_tmp}"/log
2016-02-28 09:08:20 +00:00
fi
}
2016-04-08 22:44:29 +00:00
# This should not require the root variable; fix the paths before they
# get to the log file.
2016-02-28 09:08:20 +00:00
report_outcome() {
2016-04-10 16:49:14 +00:00
root="$(fullpath "${1}")"
2016-04-10 06:04:12 +00:00
format="${2}"
2016-03-02 20:19:33 +00:00
log_file="${3}"
start="${4}"
finish="${5}"
2016-02-28 12:41:39 +00:00
2016-03-02 20:29:33 +00:00
elapsed=$(($finish - $start))
2016-02-28 10:57:27 +00:00
for number in n oks skips not_oks; do
2016-03-02 20:19:33 +00:00
eval "${number}=0"
2016-02-28 10:57:27 +00:00
done
2016-02-28 11:14:23 +00:00
# Use a temporary file rather than a pipe because a pipe starts a sub-shell
# and thus makes the above variables local.
2016-04-07 02:42:29 +00:00
sorted_log_file=$(mktemp_file)
2016-04-04 19:14:48 +00:00
cat "${log_file}" | LC_COLLATE=C sort > "${sorted_log_file}"
2016-02-29 00:41:03 +00:00
2016-03-02 20:39:31 +00:00
rm "${sorted_log_file}"
2016-02-28 09:17:16 +00:00
2016-04-10 06:04:12 +00:00
if test "${format}" = tap; then
2016-03-02 20:39:31 +00:00
echo "# Full test suite took ${elapsed} $(plural second ${elapsed})."
echo 1.."${n}"
2016-04-10 06:04:12 +00:00
elif test "${format}" = urchin; then
if "${print_margins}"; then
echo
echo "Done, took ${elapsed} $(plural second ${elapsed})."
2016-04-10 18:42:58 +00:00
echo "${oks} $(plural test "${oks}") passed."
echo "${skips} $(plural test "${skips}") skipped."
echo "${not_oks} $(plural test "${not_oks}") failed."
2016-03-06 09:44:10 +00:00
fi
2016-02-28 09:17:16 +00:00
fi
2016-03-02 20:39:31 +00:00
test "${not_oks}" -eq '0'
2012-10-04 11:29:34 +00:00
}
2012-10-04 16:43:49 +00:00
2016-02-28 14:32:33 +00:00
2016-03-02 21:18:40 +00:00
main() {
cycle_shell=true
shell_list="${urchin_tmp}"/shell_list
2016-03-06 14:23:04 +00:00
test_arg_list="${urchin_tmp}"/test_list
2016-04-07 02:54:30 +00:00
> "${test_arg_list}"
2016-03-02 21:18:40 +00:00
run_in_series=false
force=false
exit_on_not_ok=false
2016-04-08 22:44:29 +00:00
format=urchin
2016-03-06 09:44:10 +00:00
print_in_color=false
2016-03-06 10:39:40 +00:00
2016-03-06 10:45:40 +00:00
print_margins=true
2016-03-06 10:39:40 +00:00
print_ok=false
print_not_ok=true
2016-03-06 09:37:08 +00:00
print_ok_stdout=false
print_not_ok_stdout=false
2016-03-02 21:18:40 +00:00
while [ "${#}" -gt 0 ]
do
case "${1}" in
-b|--run-in-series) run_in_series=true;;
-e|--exit-on-fail) exit_on_not_ok=true;;
-f|--force) force=true;;
-s|--shell)
shift
shell_for_sh_tests="${1}"
2016-04-10 06:15:55 +00:00
command -v "${shell_for_sh_tests}" > /dev/null || {
2016-03-02 21:18:40 +00:00
echo "Cannot find specified shell: '${shell_for_sh_tests}'" >&2
urchin_help >&2
urchin_exit 11
}
2016-04-10 17:42:05 +00:00
if contains "${potential_test}" "${HT}"; then
echo 'Shell paths may not contain tab characters (HT).' >&2
2016-04-10 09:05:08 +00:00
urchin_exit 11
fi
2016-04-10 15:48:30 +00:00
if contains "${shell_for_sh_tests}" "[${IFS}]"; then
2016-04-10 09:05:08 +00:00
echo "Warning: It is best if field-separator characters
(usually spaces) are absent from shell paths so that
you don't need to quote the TEST_SHELL variable." >&2
2016-03-02 21:18:40 +00:00
fi
2016-02-28 14:01:17 +00:00
2016-03-02 21:18:40 +00:00
echo "${shell_for_sh_tests}" >> "${shell_list}"
;;
-n|--disable-cycling) cycle_shell=false;;
2016-04-08 22:44:29 +00:00
-F|--format) shift ; format="${1}";;
2016-04-04 01:16:51 +00:00
-T|--timeout)
shift
urchin_timeout="${1}"
2016-04-10 15:48:30 +00:00
if ! contains "${urchin_timeout}" '[0-9][0-9.]*\(s\|m\|h\|d\|\)' ; then
2016-04-04 01:16:51 +00:00
echo Bad timeout argument: "${urchin_timeout}" >&2
urchin_exit 1
fi ;;
2016-03-31 19:56:12 +00:00
-p|--pretty) print_in_color=true;;
2016-03-06 10:42:34 +00:00
2016-03-06 10:45:40 +00:00
-q|--quiet) print_not_ok=false
print_margins=false;;
2016-03-06 13:54:10 +00:00
-v) print_not_ok_stdout=true;;
-vv) print_not_ok_stdout=true
print_ok=true;;
-vvv|--verbose)print_not_ok_stdout=true
print_ok=true;
2016-03-06 10:42:34 +00:00
print_ok_stdout=true;;
2016-04-01 19:52:34 +00:00
-vvvv|--debug) print_not_ok_stdout=true
print_ok=true;
print_ok_stdout=true
2016-04-07 02:47:55 +00:00
set -x;;
2016-03-06 10:42:34 +00:00
2016-03-02 21:18:40 +00:00
-h|--help) urchin_help
urchin_exit 0;;
--version) echo "${VERSION}"
urchin_exit;;
-*) urchin_help >&2
urchin_exit 11;;
2016-04-10 17:42:05 +00:00
*) if contains "${1}" "${HT}"; then
echo 'Test file names may not contain tab characters (HT).' >&2
2016-04-10 16:12:40 +00:00
urchin_exit 11
elif [ ! -e "${1}" ]; then
echo "No such file or directory: '${1}'" >&2
echo "${USAGE}" >&2
urchin_exit 11
elif ! {
# Molly guard
root="$(urchin_root "${1}")"
basename "$(fullpath "${root}")" |
grep -i 'test' > /dev/null || "${force}"
}; then
echo 'The root directory of the tests that you are running urchin on
does not contain the word "test", so I am not running,
in case that was an accident. Use the -f flag if you really
want to run urchin on that directory.' >&2
urchin_exit 12
fi
2016-03-06 14:23:04 +00:00
echo "${1}" >> "${test_arg_list}" ;;
2016-03-02 21:18:40 +00:00
esac
shift
done
2016-04-07 03:17:08 +00:00
if "${RUN_IN_SERIES}" 2> /dev/null; then
run_in_series=true
2016-04-07 03:13:42 +00:00
fi
2016-02-28 16:53:49 +00:00
2016-03-02 21:18:40 +00:00
# -------------------- VALIDATE INPUT -------------------- #
2016-04-10 16:40:36 +00:00
if ! "${cycle_shell}" && ! is_set shell_list; then
2016-03-02 21:18:40 +00:00
echo "The -n/--disable-cycling and -s/--shell options clash with each other." >&2
urchin_exit 11
2016-02-28 16:53:49 +00:00
fi
2016-02-28 14:14:55 +00:00
2016-03-02 21:18:40 +00:00
# If -s was not passed, use the available default shells.
2016-04-10 16:40:36 +00:00
if test -z "${shell_list}"; then
2016-03-02 21:18:40 +00:00
if $cycle_shell; then
for shell in $DEFAULT_SHELLS; do
2016-04-10 16:40:36 +00:00
if command -v "${shell}" 1> /dev/null 2> /dev/null; then
2016-04-10 17:42:05 +00:00
shell_list="${shell}${HT}${shell_list}"
2016-03-02 21:18:40 +00:00
fi
done
fi
2016-02-28 12:02:57 +00:00
fi
2012-10-11 06:21:05 +00:00
2016-04-07 02:47:55 +00:00
if $print_margins; then
2016-04-10 06:04:12 +00:00
if test "${format}" = tap; then
printf \#\ >> "${urchin_tmp}"/head
fi
if test "${format}" = urchin || test "${format}" = tap; then
2016-04-10 06:05:28 +00:00
echo Running tests at $(date +%Y-%m-%dT%H:%M:%S) >> "${urchin_tmp}"/head
2016-04-10 06:04:12 +00:00
fi
2016-04-08 22:44:29 +00:00
if test "${format}" = tap; then
printf '# ' >> "${urchin_tmp}"/head
2016-04-07 05:19:47 +00:00
fi
2016-04-08 22:44:29 +00:00
if test "${format}" = urchin || test "${format}" = tap; then
printf 'Cycling with the following shells: ' >> "${urchin_tmp}"/head
fi
cat "${shell_list}" | tr '\n' \ >> "${urchin_tmp}"/head
echo >> "${urchin_tmp}"/head
2016-04-01 19:52:34 +00:00
fi
2016-04-10 16:40:36 +00:00
if is_set urchin_timeout; then
2016-03-02 21:18:40 +00:00
# Choose the timeout command
2016-04-04 01:04:04 +00:00
if timeout -t 0 true 2> /dev/null; then
2016-03-02 21:18:40 +00:00
TIMEOUT="timeout -t ${urchin_timeout}"
2016-04-04 01:04:04 +00:00
elif timeout 0 true 2> /dev/null; then
2016-03-02 21:18:40 +00:00
TIMEOUT="timeout ${urchin_timeout}"
else
2016-03-21 20:28:59 +00:00
echo I couldn\'t figure out how to use your version of timeout >&2
2016-03-02 21:18:40 +00:00
urchin_exit 1
fi
fi
2016-02-28 16:12:13 +00:00
2016-03-02 21:18:40 +00:00
if "${exit_on_not_ok}" && ! "${run_in_series}"; then
echo 'You must also pass -b/--series in order to use -e/--exit-on-fail.' >&2
urchin_exit 11
fi
# -------------------- REALLY RUN -------------------- #
2016-04-07 02:52:40 +00:00
start=$("${epoch}")
2016-02-28 10:39:32 +00:00
2016-02-28 12:14:16 +00:00
# 1 test file or folder to run
# 2 urchin root
2016-02-28 14:01:17 +00:00
# 3 Should we cycle shells?
2016-02-28 12:14:16 +00:00
# 4 TEST_SHELL
2016-03-06 14:19:04 +00:00
while read seed; do
recurse "$(fullpath "${seed}")" "${root}" "${cycle_shell}" \
"${TEST_SHELL}" || break
2016-03-06 14:23:04 +00:00
done < "${test_arg_list}"
2016-04-07 02:52:40 +00:00
finish=$("${epoch}")
2016-02-28 10:18:06 +00:00
2016-04-04 21:29:51 +00:00
if test $(cat "${urchin_tmp}"/log | wc -l) -eq 0; then
2016-04-04 21:37:47 +00:00
echo 'No tests found' >&2
urchin_exit 1
2016-04-04 21:29:51 +00:00
fi
2016-04-10 06:05:28 +00:00
cat "${urchin_tmp}"/head
2016-04-08 22:44:29 +00:00
report_outcome "${root}" "${format}" "${urchin_tmp}"/log "${start}" \
2016-03-02 20:39:31 +00:00
"${finish}"
2016-04-10 06:05:28 +00:00
# cat "${urchin_tmp}"/foot
2016-03-02 20:39:31 +00:00
urchin_exit "${?}"
2016-03-02 21:18:40 +00:00
}
2016-04-10 16:40:36 +00:00
is_set TESTING_URCHIN_INTERNALS || main "$@"