Improve test framework: log timestamps

This commit is contained in:
Andrew Bettison 2012-07-02 12:26:58 +09:30
parent fc8df7d5d4
commit f76ef9a7e6
2 changed files with 25 additions and 23 deletions

View File

@ -399,14 +399,14 @@ escape_grep_extended() {
# - sets the $executed variable to a description of the command that was # - sets the $executed variable to a description of the command that was
# executed # executed
execute() { execute() {
echo -n "# execute "; _tfw_shellarg "$@" tfw_log "# execute" $(_tfw_shellarg "$@")
_tfw_getopts execute "$@" _tfw_getopts execute "$@"
shift $_tfw_getopts_shift shift $_tfw_getopts_shift
_tfw_execute "$@" _tfw_execute "$@"
} }
executeOk() { executeOk() {
echo -n "# executeOk "; _tfw_shellarg "$@" tfw_log "# executeOk" $(_tfw_shellarg "$@")
_tfw_getopts executeok "$@" _tfw_getopts executeok "$@"
_tfw_opt_exit_status=0 _tfw_opt_exit_status=0
_tfw_dump_on_fail --stderr _tfw_dump_on_fail --stderr
@ -423,7 +423,7 @@ assert() {
_tfw_getopts assert "$@" _tfw_getopts assert "$@"
shift $_tfw_getopts_shift shift $_tfw_getopts_shift
_tfw_assert "$@" || _tfw_failexit _tfw_assert "$@" || _tfw_failexit
echo -n "# assert "; _tfw_shellarg "$@" tfw_log "# assert" $(_tfw_shellarg "$@")
return 0 return 0
} }
@ -433,7 +433,7 @@ assertExpr() {
local awkexpr=$(_tfw_expr_to_awkexpr "$@") local awkexpr=$(_tfw_expr_to_awkexpr "$@")
_tfw_message="${_tfw_message+$_tfw_message }($awkexpr)" _tfw_message="${_tfw_message+$_tfw_message }($awkexpr)"
_tfw_assert _tfw_eval_awkexpr "$awkexpr" || _tfw_failexit _tfw_assert _tfw_eval_awkexpr "$awkexpr" || _tfw_failexit
echo -n "# assertExpr "; _tfw_shellarg "$awkexpr" tfw_log "# assertExpr" $(_tfw_shellarg "$awkexpr")
return 0 return 0
} }
@ -464,8 +464,9 @@ fatal() {
# will also do this, but tfw_log will work even in a context that stdout (fd 1) # will also do this, but tfw_log will work even in a context that stdout (fd 1)
# is redirected. # is redirected.
tfw_log() { tfw_log() {
local ts=$(_tfw_timestamp)
cat >&$_tfw_log_fd <<EOF cat >&$_tfw_log_fd <<EOF
$* ${ts##* } $*
EOF EOF
} }
@ -479,25 +480,25 @@ tfw_cat() {
for file; do for file; do
case $file in case $file in
--stdout) --stdout)
echo "#--- ${header:-stdout of $executed} ---" tfw_log "#--- ${header:-stdout of $executed} ---"
cat $show_nonprinting $_tfw_tmp/stdout cat $show_nonprinting $_tfw_tmp/stdout
echo "#---" tfw_log "#---"
header= header=
show_nonprinting= show_nonprinting=
;; ;;
--stderr) --stderr)
echo "#--- ${header:-stderr of $executed} ---" tfw_log "#--- ${header:-stderr of $executed} ---"
cat $show_nonprinting $_tfw_tmp/stderr cat $show_nonprinting $_tfw_tmp/stderr
echo "#---" tfw_log "#---"
header= header=
show_nonprinting= show_nonprinting=
;; ;;
--header=*) header="${1#*=}";; --header=*) header="${1#*=}";;
-v|--show-nonprinting) show_nonprinting=-v;; -v|--show-nonprinting) show_nonprinting=-v;;
*) *)
echo "#--- ${header:-$file} ---" tfw_log "#--- ${header:-$file} ---"
cat $show_nonprinting "$file" cat $show_nonprinting "$file"
echo "#---" tfw_log "#---"
header= header=
show_nonprinting= show_nonprinting=
;; ;;
@ -510,7 +511,7 @@ assertExitStatus() {
shift $_tfw_getopts_shift shift $_tfw_getopts_shift
[ -z "$_tfw_message" ] && _tfw_message="exit status of $executed ($_tfw_exitStatus) $*" [ -z "$_tfw_message" ] && _tfw_message="exit status of $executed ($_tfw_exitStatus) $*"
_tfw_assertExpr "$_tfw_exitStatus" "$@" || _tfw_failexit _tfw_assertExpr "$_tfw_exitStatus" "$@" || _tfw_failexit
echo "# assert $_tfw_message" tfw_log "# assert $_tfw_message"
return 0 return 0
} }
@ -519,7 +520,7 @@ assertRealTime() {
shift $_tfw_getopts_shift shift $_tfw_getopts_shift
[ -z "$_tfw_message" ] && _tfw_message="real execution time of $executed ($realtime) $*" [ -z "$_tfw_message" ] && _tfw_message="real execution time of $executed ($realtime) $*"
_tfw_assertExpr "$realtime" "$@" || _tfw_failexit _tfw_assertExpr "$realtime" "$@" || _tfw_failexit
echo "# assert $_tfw_message" tfw_log "# assert $_tfw_message"
return 0 return 0
} }
@ -624,7 +625,8 @@ _tfw_abspath() {
} }
_tfw_timestamp() { _tfw_timestamp() {
date '+%Y-%m-%d %H:%M:%S.%N' local ts=$(date '+%Y-%m-%d %H:%M:%S.%N')
echo "${ts%[0-9][0-9][0-9][0-9][0-9][0-9]}"
} }
_tfw_setup() { _tfw_setup() {
@ -645,42 +647,42 @@ _tfw_setup() {
export TFWTMP=$_tfw_tmp/tmp export TFWTMP=$_tfw_tmp/tmp
mkdir $TFWTMP mkdir $TFWTMP
cd $TFWTMP cd $TFWTMP
echo '# SETUP' tfw_log '# SETUP'
case `type -t setup_$_tfw_test_name` in case `type -t setup_$_tfw_test_name` in
function) function)
echo "# call setup_$_tfw_test_name()" tfw_log "# call setup_$_tfw_test_name()"
$_tfw_trace && set -x $_tfw_trace && set -x
setup_$_tfw_test_name $_tfw_test_name setup_$_tfw_test_name $_tfw_test_name
set +x set +x
;; ;;
*) *)
echo "# call setup($_tfw_test_name)" tfw_log "# call setup($_tfw_test_name)"
$_tfw_trace && set -x $_tfw_trace && set -x
setup $_tfw_test_name setup $_tfw_test_name
set +x set +x
;; ;;
esac esac
echo '# END SETUP' tfw_log '# END SETUP'
} }
_tfw_teardown() { _tfw_teardown() {
_tfw_phase=teardown _tfw_phase=teardown
echo '# TEARDOWN' tfw_log '# TEARDOWN'
case `type -t teardown_$_tfw_test_name` in case `type -t teardown_$_tfw_test_name` in
function) function)
echo "# call teardown_$_tfw_test_name()" tfw_log "# call teardown_$_tfw_test_name()"
$_tfw_trace && set -x $_tfw_trace && set -x
teardown_$_tfw_test_name teardown_$_tfw_test_name
set +x set +x
;; ;;
*) *)
echo "# call teardown($_tfw_test_name)" tfw_log "# call teardown($_tfw_test_name)"
$_tfw_trace && set -x $_tfw_trace && set -x
teardown $_tfw_test_name teardown $_tfw_test_name
set +x set +x
;; ;;
esac esac
echo '# END TEARDOWN' tfw_log '# END TEARDOWN'
} }
# Executes $_tfw_executable with the given arguments. # Executes $_tfw_executable with the given arguments.

View File

@ -65,7 +65,7 @@ setup_servald_instances() {
# Now make sure that they can see each other # Now make sure that they can see each other
secs=1 secs=1
sleep $secs # Should be plenty of time sleep $secs # Should be plenty of time
echo "Dummynet file after $secs seconds: "`ls -l $DUMMYNET` tfw_log "# Dummynet file after $secs seconds: "`ls -l $DUMMYNET`
set_instance +A set_instance +A
executeOk_servald id peers executeOk_servald id peers
assertStdoutLineCount '==' 1 assertStdoutLineCount '==' 1