Improve test framework: do not use $BASHPID

Also do not allow -j and --verbose to be given together
This commit is contained in:
Andrew Bettison 2012-07-18 14:44:28 +09:30
parent 3c09a1ec7d
commit c8a538337d

View File

@ -150,6 +150,9 @@ runTests() {
shift shift
done done
_tfw_shopt_restore _tfw_shopt_restore
if $_tfw_verbose && [ $njobs -ne 1 ]; then
_tfw_fatal "--verbose is incompatible with --jobs=$njobs"
fi
# Create an empty results directory. # Create an empty results directory.
_tfw_results_dir="$_tfw_tmpdir/results" _tfw_results_dir="$_tfw_tmpdir/results"
mkdir "$_tfw_results_dir" || return $? mkdir "$_tfw_results_dir" || return $?
@ -183,23 +186,25 @@ runTests() {
$_tfw_stop_on_failure && [ $_tfw_failcount -ne 0 ] && break $_tfw_stop_on_failure && [ $_tfw_failcount -ne 0 ] && break
# Start the next test in a child process. # Start the next test in a child process.
_tfw_echo_intro $testPosition $testNumber $testName _tfw_echo_intro $testPosition $testNumber $testName
[ $njobs -ne 1 ] && echo if $_tfw_verbose || [ $njobs -ne 1 ]; then
echo
fi
echo "$testPosition $testNumber $testName" >"$_tfw_results_dir/$testName"
( (
_tfw_unique=$BASHPID _tfw_test_name="$testName"
echo "$testPosition $testNumber $testName" >"$_tfw_results_dir/$_tfw_unique" _tfw_unique=$testNumber
_tfw_tmp=/tmp/_tfw-$_tfw_unique _tfw_tmp=/tmp/_tfw-$_tfw_suite_name-$_tfw_test_name
trap '_tfw_status=$?; rm -rf "$_tfw_tmp"; exit $_tfw_status' EXIT SIGHUP SIGINT SIGTERM trap '_tfw_status=$?; rm -rf "$_tfw_tmp"; exit $_tfw_status' EXIT SIGHUP SIGINT SIGTERM
local start_time=$(_tfw_timestamp) local start_time=$(_tfw_timestamp)
local finish_time=unknown local finish_time=unknown
( (
_tfw_test_name="$testName"
trap '_tfw_status=$?; _tfw_teardown; exit $_tfw_status' EXIT SIGHUP SIGINT SIGTERM trap '_tfw_status=$?; _tfw_teardown; exit $_tfw_status' EXIT SIGHUP SIGINT SIGTERM
_tfw_result=ERROR _tfw_result=ERROR
mkdir $_tfw_tmp || exit 255 mkdir $_tfw_tmp || exit 255
_tfw_setup _tfw_setup
_tfw_result=FAIL _tfw_result=FAIL
_tfw_phase=testcase _tfw_phase=testcase
echo "# call test_$_tfw_test_name()" tfw_log "# CALL test_$_tfw_test_name()"
$_tfw_trace && set -x $_tfw_trace && set -x
test_$_tfw_test_name test_$_tfw_test_name
_tfw_result=PASS _tfw_result=PASS
@ -218,7 +223,7 @@ runTests() {
1) result=FAIL;; 1) result=FAIL;;
0) result=PASS;; 0) result=PASS;;
esac esac
echo "$testPosition $testNumber $testName $result" >"$_tfw_results_dir/$_tfw_unique" echo "$testPosition $testNumber $testName $result" >"$_tfw_results_dir/$testName"
{ {
echo "Name: $testName" echo "Name: $testName"
echo "Result: $result" echo "Result: $result"
@ -239,6 +244,7 @@ runTests() {
exit 0 exit 0
) & ) &
_tfw_running_pids+=($!) _tfw_running_pids+=($!)
ln -s "$_tfw_results_dir/$testName" "$_tfw_results_dir/pid-$!"
done done
# Wait for all child processes to finish. # Wait for all child processes to finish.
while [ ${#_tfw_running_pids[*]} -ne 0 ]; do while [ ${#_tfw_running_pids[*]} -ne 0 ]; do
@ -271,8 +277,8 @@ _tfw_harvest_processes() {
for pid in ${_tfw_running_pids[*]}; do for pid in ${_tfw_running_pids[*]}; do
if kill -0 $pid 2>/dev/null; then if kill -0 $pid 2>/dev/null; then
surviving_pids+=($pid) surviving_pids+=($pid)
elif [ -s "$_tfw_results_dir/$pid" ]; then elif [ -s "$_tfw_results_dir/pid-$pid" ]; then
set -- $(<"$_tfw_results_dir/$pid") set -- $(<"$_tfw_results_dir/pid-$pid")
local testPosition="$1" local testPosition="$1"
local testNumber="$2" local testNumber="$2"
local testName="$3" local testName="$3"
@ -293,11 +299,11 @@ _tfw_harvest_processes() {
;; ;;
esac esac
local lines local lines
if [ $njobs -eq 1 ]; then if ! $_tfw_verbose && [ $njobs -eq 1 ]; then
echo -n " " echo -n " "
_tfw_echo_result "$result" _tfw_echo_result "$result"
echo echo
elif lines=$($_tfw_tput lines); then elif ! $_tfw_verbose && lines=$($_tfw_tput lines); then
local travel=$(($_tfw_test_number_watermark - $testPosition + 1)) local travel=$(($_tfw_test_number_watermark - $testPosition + 1))
if [ $travel -gt 0 -a $travel -lt $lines ] && $_tfw_tput cuu $travel ; then if [ $travel -gt 0 -a $travel -lt $lines ] && $_tfw_tput cuu $travel ; then
_tfw_echo_intro $testPosition $testNumber $testName _tfw_echo_intro $testPosition $testNumber $testName
@ -673,9 +679,12 @@ _tfw_setup() {
_tfw_stdout=5 _tfw_stdout=5
_tfw_stderr=5 _tfw_stderr=5
if $_tfw_verbose; then if $_tfw_verbose; then
# These tail processes will die when the test case's subshell exits. # Find the PID of the current subshell process. Cannot use $BASHPID
tail --pid=$BASHPID --follow $_tfw_tmp/log.stdout >&$_tfw_stdout 2>/dev/null & # because MacOS only has Bash-3.2, and $BASHPID was introduced in Bash-4.
tail --pid=$BASHPID --follow $_tfw_tmp/log.stderr >&$_tfw_stderr 2>/dev/null & local mypid=$($BASH -c 'echo $PPID')
# These tail processes will die when the current subshell exits.
tail --pid=$mypid --follow $_tfw_tmp/log.stdout >&$_tfw_stdout 2>/dev/null &
tail --pid=$mypid --follow $_tfw_tmp/log.stderr >&$_tfw_stderr 2>/dev/null &
fi fi
export TFWUNIQUE=$_tfw_unique export TFWUNIQUE=$_tfw_unique
export TFWVAR=$_tfw_tmp/var export TFWVAR=$_tfw_tmp/var