urchin/urchin

749 lines
21 KiB
Plaintext
Raw Permalink 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')"
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.)
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.
2016-04-10 19:17:02 +00:00
Exit codes have the following meanings
0 All tests were ok
1 At least one test was not ok.
2 No tests were found.
10 Dependencies are missing (locally, not on remotes).
11 Flags were not valid.
12 File names contain unsupported delimiters (HT or LF).
2016-04-10 19:39:11 +00:00
13 An test shell specified with -s/--shell is not available.
2016-04-10 19:17:02 +00:00
2016-04-08 22:24:55 +00:00
Go to https://thomaslevine.com/!/urchin/ for documentation on writing tests.
EOF
}
# -------------------- 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-11 04:48:26 +00:00
tmp=$(mktemp)
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
}
2016-04-10 19:17:02 +00:00
md5 () {
case "${urchin_md5}" in
md5sum) echo "${1}" | md5sum | sed 's/ .*//' ;;
md5) echo "${1}" | md5 | sed 's/.* //' ;;
esac
}
2016-04-08 22:24:55 +00:00
# -------------------- Utilities --------------------
2016-04-11 04:13:05 +00:00
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 10
fi
2016-04-08 22:25:58 +00:00
epoch_date() {
date +%s
}
epoch_pax() {
# Based on http://stackoverflow.com/a/7262588/407226
2016-04-11 04:48:26 +00:00
tmp="$(mktemp)"
2016-04-08 22:25:58 +00:00
echo "ibase=8;$({ pax -wx cpio "${tmp}"; echo; } | cut -c 48-59)" | bc
rm "${tmp}"
}
2016-04-11 04:13:05 +00:00
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 10
fi
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 09:12:57 +00:00
# Expand relative paths
fullpath() {
2016-04-10 21:45:37 +00:00
if test -e "${1}"; then
readlink -f -- "${1}" | sed 's/\/*$//'
else
echo "Could not find file or directory: ${1}" >&2
return 1
fi
}
2016-02-28 09:12:57 +00:00
2016-04-10 21:45:37 +00:00
# If $1 is an ascestor of $2, echo the path of $2 relative $1.
# If either of $1 or $2 does not exist, return with code 1
# If $1 is not an ancestor of $2, return with code 2
2016-04-10 21:18:08 +00:00
# Otherwise, return with code 2.
2016-04-10 21:17:19 +00:00
localpath() {
2016-04-10 21:45:37 +00:00
test -e "${1}" && test -e "${2}" || return 1 # A file is missing.
parent="$(fullpath "${1}")"
child="$(fullpath "${2}")"
if echo "${child}" | grep "^${parent}/" > /dev/null; then
2016-04-10 21:27:04 +00:00
# Child is really a child.
2016-04-10 21:45:37 +00:00
echo "${child#"${parent}/"}"
2016-04-10 21:27:04 +00:00
return 0
2016-04-10 21:17:19 +00:00
else
2016-04-10 21:27:04 +00:00
# Child is not really a child.
return 2
2016-04-10 21:17:19 +00:00
fi
}
2016-04-10 21:45:37 +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-04-10 19:51:18 +00:00
# File where a test's stdout and stderr is saved
stdout_file() {
2016-04-11 05:44:58 +00:00
urchin_tmp="${1}"
2016-04-10 19:51:18 +00:00
the_test="${2}"
the_shell="${3}"
2016-04-11 05:44:58 +00:00
x="${urchin_tmp}/stdout$(localpath "$the_test")"
2016-04-10 19:51:18 +00:00
mkdir -p "${x}"
echo "${x}/$(md5 "${the_shell}")"
}
2016-04-10 21:05:34 +00:00
# Print a line of a log file
log() {
for arg in "$@"; do
printf "${arg}\t"
done
printf '\n'
}
2016-04-10 19:51:18 +00:00
# Root directory of the present test suite
# USAGE: test_suite_root <directory>
test_suite_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
2016-04-10 19:51:18 +00:00
test_suite_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-04-10 19:51:18 +00:00
test_suite_root "${current}"/.. "${orig}"
2016-02-28 11:22:51 +00:00
fi
2016-02-28 11:18:39 +00:00
}
2016-04-10 22:45:39 +00:00
# -------------------- Printing output --------------------
# Format functions may read a log file from stdin.
2016-04-10 18:42:58 +00:00
meta_verbosity() {
2016-04-10 20:36:05 +00:00
echo "if test \${${1}} -ge ${2}; then ${3}=true; fi"
2016-04-10 18:42:58 +00:00
}
2016-04-10 18:23:24 +00:00
format_tap() {
2016-04-10 18:42:58 +00:00
v="${1}"
2016-04-11 05:44:58 +00:00
urchin_tmp="${2}"
2016-04-10 19:08:19 +00:00
elapsed="${3}"
2016-04-10 18:23:24 +00:00
2016-04-10 22:45:39 +00:00
$(meta_verbosity v 2 print_not_ok_stdout)
$(meta_verbosity v 3 print_ok_stdout)
2016-04-10 18:23:24 +00:00
2016-04-10 18:56:32 +00:00
print_stdout() {
echo '# ------------ Begin output ------------'
2016-04-11 05:44:58 +00:00
sed 's/^/# /' "$(stdout_file "${urchin_tmp}" "${path}" "${the_shell}")"
2016-04-10 18:56:32 +00:00
echo '# ------------ End output ------------'
}
2016-04-10 21:54:08 +00:00
while IFS="${HT}" read -r remote the_shell path result file_elapsed; do
2016-04-10 18:42:58 +00:00
# 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
case "${result}" in
2016-04-10 19:44:17 +00:00
ok) echo "ok $n - ${path} (${the_shell}${on})"
2016-04-10 18:56:32 +00:00
if "${print_ok_stdout}"; then print_stdout; fi ;;
2016-04-10 19:44:17 +00:00
not_ok) echo "not_ok $n - ${path} (${the_shell}${on})"
2016-04-10 18:56:32 +00:00
if "${print_not_ok_stdout}"; then print_stdout; fi ;;
2016-04-10 19:44:17 +00:00
skip) "ok $n - ${path} (${the_shell}${on}) # SKIP" ;;
2016-04-10 18:42:58 +00:00
esac
echo "# Previous test took ${file_elapsed} seconds."
2016-04-10 18:23:24 +00:00
2016-04-10 18:42:58 +00:00
done
2016-04-10 19:08:19 +00:00
echo "# Full test suite took ${elapsed} $(plural second ${elapsed})."
echo 1.."${n}"
2016-04-10 18:42:58 +00:00
}
2016-04-10 18:23:24 +00:00
format_urchin() {
2016-04-10 18:42:58 +00:00
v="${1}"
2016-04-11 05:44:58 +00:00
urchin_tmp="${2}"
2016-04-10 18:42:58 +00:00
verbosity="${3}"
print_in_color="${4}"
2016-04-10 22:45:39 +00:00
$(meta_verbosity v 1 print_margins)
$(meta_verbosity v 1 print_not_ok)
$(meta_verbosity v 2 print_not_ok_stdout)
$(meta_verbosity v 2 print_ok)
$(meta_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:56:32 +00:00
print_stdout() {
2016-04-11 05:44:58 +00:00
sed 's/^/ | /' "$(stdout_file "${urchin_tmp}" "${path}" "${the_shell}")"
2016-04-10 18:56:32 +00:00
}
2016-04-10 21:54:08 +00:00
while IFS="${HT}" read -r remote the_shell path result file_elapsed; do
2016-04-11 05:44:58 +00:00
abspath=${urchin_tmp}/${path}
2016-04-10 19:44:17 +00:00
currentdir="$(dirname -- "${path}")"
prevdir="${currentdir}"
# Format the message
2016-04-10 19:39:11 +00:00
if test -z "${remote}"; then
on=" on ${remote}"
else
on=
fi
2016-04-10 19:44:17 +00:00
if test result = skip; then
parantheses="(skipped)"
2016-04-10 18:51:52 +00:00
else
2016-04-10 19:44:17 +00:00
parantheses="(${file_elapsed} $(plural second "${file_elapsed}"))"
2016-04-10 18:51:52 +00:00
fi
2016-04-10 19:44:17 +00:00
message="${the_shell}${on} (${file_elapsed} ${unit})"
2016-04-10 19:08:19 +00:00
# Keep track of how many files have been ok, not ok, and skipped.
eval "${result}s=$((${result}s+1))"
# Print the result.
2016-04-10 18:42:58 +00:00
case "${result}" in
2016-04-10 18:51:52 +00:00
ok) if "${print_ok}"; then
header && echo "${success_mark} ${message}"
fi ;;
not_ok) if "${print_not_ok}"; then
header && echo "${fail_mark} ${message}"
2016-04-10 18:56:32 +00:00
if "${print_not_ok_stdout}"; then print_stdout; fi
2016-04-10 18:51:52 +00:00
fi ;;
skip) if "${print_ok}"; then
header && echo "${skip_mark} ${message}"
2016-04-10 18:56:32 +00:00
if "${print_ok_stdout}"; then print_stdout; fi
2016-04-10 18:51:52 +00:00
fi ;;
2016-04-10 18:42:58 +00:00
esac
prevpath="${path}"
done
2016-04-10 18:43:58 +00:00
if "${print_margins}"; then
echo
echo "Done, took ${elapsed} $(plural second ${elapsed})."
echo "${oks} $(plural test "${oks}") passed."
echo "${skips} $(plural test "${skips}") skipped."
echo "${not_oks} $(plural test "${not_oks}") failed."
fi
2016-04-10 18:23:24 +00:00
}
2016-04-08 22:24:55 +00:00
# -------------------- Main stuff --------------------
2016-04-10 20:34:36 +00:00
# Return codes have the following meaning
# 0) All tests succeded
# 1) At least one test failed.
# *) Something else went wrong; Urchin should exit with the same code.
2012-10-04 11:29:34 +00:00
recurse() {
2016-04-11 18:42:33 +00:00
echo "$3 $4 $5"
2016-04-10 21:54:08 +00:00
abs_root="${1}"
abs_requested="${2}"
abs_current="${3}"
2016-02-28 10:27:49 +00:00
2016-04-10 22:44:22 +00:00
run_in_series_root="${4}"
2016-04-11 05:05:39 +00:00
exit_on_not_ok="${5}"
2016-04-10 22:44:22 +00:00
2016-04-10 21:54:08 +00:00
rel_requested="$(localpath ${abs_root} ${abs_requested})"
rel_current="$(localpath ${abs_root} ${abs_current})"
2016-04-10 16:49:14 +00:00
2016-02-28 10:00:53 +00:00
for ignore in setup_dir teardown_dir setup teardown; do
2016-04-10 21:54:08 +00:00
if test "$(basename "${abs_current}")" = "${ignore}"; then
2016-03-06 10:22:15 +00:00
return 0
fi
2016-04-10 21:54:08 +00:00
done
2016-02-28 12:02:57 +00:00
2016-04-10 21:55:15 +00:00
if contains "${abs_current}" "${HT}"; then
2016-04-10 21:54:08 +00:00
echo "${shell_list}" | while read -r sh; do
no_tab="$(echo "${rel_current}" | tr '\t' ' ')"
log "${remote}" "${sh}" "${no_tab}" tab '' >> "${urchin_tmp}"/log
2016-04-10 21:05:34 +00:00
done
2016-04-10 21:55:15 +00:00
elif [ -x "${abs_current}" ]; then
2016-04-10 22:45:39 +00:00
meta_finalize='
2016-04-10 22:44:22 +00:00
if test -f .urchin_dir && grep series ./.urchin_dir > /dev/null ||
"${run_in_series_root}"; then
2016-04-11 16:20:20 +00:00
set +e
wait ${!}
return_code=$?
set -e
if "${exit_on_not_ok}" && test "${return_code}" -ne 0; then
eval "$(meta_dot_if_exists teardown_dir)"
2016-04-10 22:44:22 +00:00
return $return_code
fi
fi
'
2016-04-11 16:20:20 +00:00
meta_dot_if_exists() {
2016-04-10 22:48:55 +00:00
echo "
if test -f ${1}; then
. ./${1}
fi
"
}
2016-04-10 22:44:22 +00:00
2016-04-10 21:55:15 +00:00
if [ -d "${abs_current}" ]; then
(
2016-04-10 21:55:15 +00:00
cd -- "${abs_current}"
2016-03-06 14:44:39 +00:00
2016-04-11 16:20:20 +00:00
eval "$(meta_dot_if_exists setup_dir)"
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
2016-04-11 16:27:38 +00:00
recurse "${abs_root}" "${abs_requested}" "${abs_current}/${test}" \
2016-04-11 18:42:33 +00:00
"${run_in_series_root}" "${exit_on_not_ok}"
#&
2016-04-11 16:27:38 +00:00
# eval "${meta_finalize}"
2016-02-28 16:05:03 +00:00
2012-10-08 12:50:48 +00:00
done
2016-02-28 16:12:13 +00:00
wait
2016-04-11 16:20:20 +00:00
eval "$(meta_dot_if_exists teardown_dir)"
2016-02-28 10:00:53 +00:00
)
2016-04-10 21:55:15 +00:00
elif [ -f "${abs_current}" ]; then
cd -- "$(dirname -- "${abs_current}")"
echo "${shell_list}" | while read -r sh; do
2016-02-28 16:32:06 +00:00
(
2016-04-11 16:20:20 +00:00
eval "$(meta_dot_if_exists setup)"
2016-02-28 16:32:06 +00:00
2016-04-11 05:44:58 +00:00
out_file="$(stdout_file "${urchin_tmp}" "${rel_current}" "${sh}")"
2016-04-10 22:07:15 +00:00
2016-04-10 20:03:07 +00:00
# Run with a shell?
2016-04-10 21:55:15 +00:00
if has_shebang_line "${abs_current}"; then
set -- "${abs_current}"
2016-04-10 20:03:07 +00:00
else
2016-04-10 21:55:15 +00:00
set -- "${sh}" "${abs_current}"
2016-04-10 20:03:07 +00:00
fi
2016-02-28 16:32:06 +00:00
# Run the test
2016-04-10 22:07:15 +00:00
cmd='TEST_SHELL="${sh}" $TIMEOUT "$@" > "${out_file}" 2>&1'
2016-04-07 02:52:40 +00:00
start=$("${epoch}")
2016-04-10 22:07:15 +00:00
exit_code="$(catch "${cmd}")"
2016-04-07 02:52:40 +00:00
finish=$("${epoch}")
2016-04-10 22:07:15 +00:00
elapsed=$(($finish - $start))
2016-02-28 16:32:06 +00:00
2016-04-11 16:20:20 +00:00
eval "$(meta_dot_if_exists teardown)"
2016-02-28 16:32:06 +00:00
2016-04-10 19:20:04 +00:00
case "${exit_code}" in
0) result=ok ;;
3) result=skip ;;
*) result=not_ok ;;
esac
2016-02-28 16:32:06 +00:00
2016-04-10 22:07:15 +00:00
log "${remote}" "${rel_curent}" "${result}" "${elapsed}" \
2016-03-02 20:19:33 +00:00
>> "${urchin_tmp}"/log
exit "${exit_code}"
2016-04-11 18:42:33 +00:00
)
#&
2016-04-11 16:27:38 +00:00
# eval "${meta_finalize}"
done
2016-04-11 18:42:33 +00:00
#wait
fi
2016-04-11 04:48:26 +00:00
elif test -n "${rel_current}"; then
2016-04-10 19:44:17 +00:00
# Skip because the file is not executable.
2016-04-10 21:54:08 +00:00
echo "${shell_list}" | while read -r sh; do
printf "\t${sh}\t${rel_current}\tskip\t0\n" >> "${urchin_tmp}"/log
2016-04-10 19:44:17 +00:00
done
2016-02-28 09:08:20 +00:00
fi
2016-04-11 04:48:26 +00:00
echo ${rel_current} 3 >> /tmp/bbb
2016-02-28 09:08:20 +00:00
}
2016-03-02 21:18:40 +00:00
main() {
2016-04-10 20:27:56 +00:00
# Defaults
2016-04-08 22:44:29 +00:00
format=urchin
2016-04-10 20:27:56 +00:00
if "${RUN_IN_SERIES}" 2> /dev/null; then
run_in_series=true
2016-04-11 05:05:39 +00:00
else
run_in_series=false
2016-04-10 20:27:56 +00:00
fi
2016-04-11 05:05:39 +00:00
exit_on_not_ok=false
2016-03-06 10:39:40 +00:00
2016-04-10 20:27:56 +00:00
# Shift if possible; error otherwise.
flag_arg() {
flag="${1}"
if shift; then
echo "${1}"
else
echo Missing argument for "${flag}" >&2
exit 11
fi
}
2016-04-10 09:05:08 +00:00
2016-04-10 20:27:56 +00:00
# Receive input
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;;
-F|--format) format="$(flag_arg)" ;;
-p|--pretty) print_in_color=true;;
-q|--quiet) verbosity=0 ;;
-v) verbosity=2 ;;
-vv) verbosity=3 ;;
-vvv|--verbose) verbosity=4 ;;
-vvvv|--debug) verbosity=5 ;;
--version) echo "${VERSION}" && exit;;
-h|--help) urchin_help && exit 0;;
-s|--shell) sh="$(flag_arg)"
if ! command -v "${sh}" > /dev/null; then
echo "Cannot find specified shell: '${sh}'" >&2
urchin_help >&2
exit 13
elif contains "${potential_test}" "${HT}" "${LF}"; then
echo 'Shell paths may contain all characters other than' >&2
echo 'horizontal tab (\t) and line feed (\n).' >&2
exit 11
elif contains "${sh}" "[${IFS}]"; then
echo "Warning: It is best if field-separator characters
2016-04-10 09:05:08 +00:00
(usually spaces) are absent from shell paths so that
you don't need to quote the TEST_SHELL variable." >&2
2016-04-10 20:27:56 +00:00
fi
shell_list="${sh}${LF}${shell_list}" ;;
2016-04-10 19:32:01 +00:00
2016-04-10 20:27:56 +00:00
-T|--timeout) urchin_timeout="$(flag_arg)"
if ! contains "${urchin_timeout}" \
'[0-9][0-9.]*\(s\|m\|h\|d\|\)' ; then
echo Bad timeout argument: "${urchin_timeout}" >&2
exit 11
fi ;;
-*) urchin_help >&2 && exit 11;;
*) if contains "${1}" "${HT}" "${LF}"; then
echo 'Test file names may contain all characters other than' >&2
echo 'horizontal tab (\t) and line feed (\n).' >&2
exit 11
elif [ ! -e "${1}" ]; then
echo "No such file or directory: '${1}'" >&2
echo "${USAGE}" >&2
exit 11
elif ! {
# Molly guard
root="$(test_suite_root "${1}")"
basename "$(fullpath "${root}")" |
2016-04-11 04:48:26 +00:00
grep -i 'test' > /dev/null
2016-04-10 20:27:56 +00:00
}; then
2016-04-11 04:48:26 +00:00
molly=true
2016-04-10 20:27:56 +00:00
fi
test_seeds="${1}${LF}${test_seeds}" ;;
esac
shift
2016-03-02 21:18:40 +00:00
done
2016-02-28 16:53:49 +00:00
2016-04-11 16:27:38 +00:00
if ! is_set test_seeds; then
echo Missing test location >&2
exit 2
fi
2016-04-11 04:48:26 +00:00
if is_set molly && ! is_set force; then
echo 'The root directory of the tests that you are running urchin on
doesnot 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
exit 12
fi
2016-03-02 21:18:40 +00:00
# If -s was not passed, use the available default shells.
if ! is_set "${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-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-04-10 20:27:56 +00:00
exit 10
2016-03-02 21:18:40 +00:00
fi
fi
2016-02-28 16:12:13 +00:00
2016-04-11 04:13:05 +00:00
if is_set exit_on_not_ok && ! is_set run_in_series; then
2016-03-02 21:18:40 +00:00
echo 'You must also pass -b/--series in order to use -e/--exit-on-fail.' >&2
2016-04-10 20:27:56 +00:00
exit 11
2016-03-02 21:18:40 +00:00
fi
# -------------------- REALLY RUN -------------------- #
2016-04-10 20:05:34 +00:00
2016-04-10 20:27:56 +00:00
# Temporary files
urchin_tmp=$(mktemp_dir)
2016-04-10 20:05:34 +00:00
# Write header information
echo Running tests at $(date +%Y-%m-%dT%H:%M:%S) >> "${urchin_tmp}"/head
printf 'Cycling with the following shells: ' >> "${urchin_tmp}"/head
echo "${shell_list}" | tr "${HT}" \ >> "${urchin_tmp}"/head
echo >> "${urchin_tmp}"/head
2016-04-07 02:52:40 +00:00
start=$("${epoch}")
2016-04-11 04:31:15 +00:00
while read -r seed; do
2016-04-11 16:20:20 +00:00
root="$(fullpath "$(test_suite_root "${seed}")")"
2016-04-11 05:05:39 +00:00
abs="$(fullpath "${seed}")"
2016-04-11 05:44:58 +00:00
set +e
2016-04-11 16:20:20 +00:00
recurse "${root}" "${abs}" "${root}" \
2016-04-11 05:44:58 +00:00
"${run_in_series}" "${exit_on_not_ok}"
return_code=$?
set -e
echo $return_code
2016-04-11 04:31:15 +00:00
if test "${return_code}" -eq 0; then break; fi
done<<EOF
${test_seeds}
EOF
finish=$("${epoch}")
2016-04-11 04:13:05 +00:00
2016-04-11 04:31:15 +00:00
if test "${return_code}" -le 1; then
if test -f "${urchin_tmp}"/log ; then
2016-04-11 05:05:39 +00:00
cat "${urchin_tmp}"/head
2016-04-11 04:48:26 +00:00
# "${urchin_tmp}"/log
# elapsed=$(($finish - $start))
# # Use a temporary file rather than a pipe because a pipe starts a sub-shell
# # and thus makes the above variables local.
# sorted_log_file=$(mktemp)
# cat "${log_file}" | LC_COLLATE=C sort > "${sorted_log_file}"
# rm "${sorted_log_file}"
# test "${not_oks}" -eq '0'
2016-04-11 04:31:15 +00:00
else
echo 'No tests found' >&2
return_code=2
fi
fi
2016-04-10 20:27:56 +00:00
rm -Rf "${urchin_tmp}"
2016-04-11 04:13:05 +00:00
exit "${return_code}"
2016-03-02 21:18:40 +00:00
}
2016-04-11 16:20:20 +00:00
echo "kill -$$"
2016-04-11 04:04:30 +00:00
is_set NO_MAIN || main "$@"