Skip to content

Commit

Permalink
Merge pull request todotxt#73 from inkarkat/testlib-enhancements
Browse files Browse the repository at this point in the history
Testlib enhancements

As the vast majority of tests uses the todo.sh output to verify its correctness, output differences should always be shown, not just in verbose mode. 

This change removes the output redirection and check for exit code from the command-under-test, so as another benefit, these "housekeeping commands" are now kept off the test log.

Also, by using a separate function for exit code assertion, this can now report both differences in output and exit code (and not suppress the former any more.)

(Note: To see any of this, you need to temporarily introduce some test failures.)

Finally, I've added color highlighting for the aggregate test report (as in test-lib), because it didn't stood out against the colored test results, so that I often missed the crucial summary.
  • Loading branch information
ginatrapani committed Feb 15, 2012
2 parents 9e38fa1 + c31716a commit 7485836
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 13 deletions.
65 changes: 59 additions & 6 deletions tests/aggregate-results.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,57 @@
#!/bin/sh
#!/bin/bash

[ "x$TERM" != "xdumb" ] && (
export TERM &&
[ -t 1 ] &&
tput bold >/dev/null 2>&1 &&
tput setaf 1 >/dev/null 2>&1 &&
tput sgr0 >/dev/null 2>&1
) &&
color=t

case "$1" in
--no-color)
color=; shift ;;
esac

if test -n "$color"; then
say_color () {
(
export TERM
case "$1" in
error) tput bold; tput setaf 1;; # bold red
skip) tput bold; tput setaf 2;; # bold green
pass) tput setaf 2;; # green
info) tput setaf 3;; # brown
*) test -n "$quiet" && return;;
esac
shift
printf "* %s" "$*"
tput sgr0
echo
)
}
else
say_color() {
test -z "$1" && test -n "$quiet" && return
shift
echo "* $*"
}
fi

get_color()
{
# Only use the supplied color if there are actually instances of that
# type, so that a clean test run does not distract the user by the
# appearance of the error highlighting.
if [ ${1:?} -eq 0 ]
then
echo 'info'
else
echo "${2:-info}"
fi
}


fixed=0
success=0
Expand Down Expand Up @@ -27,8 +80,8 @@ do
done <"$file"
done

printf "%-8s%d\n" fixed $fixed
printf "%-8s%d\n" success $success
printf "%-8s%d\n" failed $failed
printf "%-8s%d\n" broken $broken
printf "%-8s%d\n" total $total
say_color 'info' "$(printf "%-8s%d\n" fixed $fixed)"
say_color "$(get_color "$success" 'pass')" "$(printf "%-8s%d\n" success $success)"
say_color "$(get_color "$failed" 'error')" "$(printf "%-8s%d\n" failed $failed)"
say_color "$(get_color "$broken" 'error')" "$(printf "%-8s%d\n" broken $broken)"
say_color 'info' "$(printf "%-8s%d\n" total $total)"
67 changes: 60 additions & 7 deletions tests/test-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ test_failure_ () {
test_failure=$(($test_failure + 1))
say_color error "FAIL $test_count: $1"
shift
echo "$@" | sed -e 's/^/ /'
echo "$@"
test "$immediate" = "" || { trap - EXIT; exit 1; }
}

Expand All @@ -199,8 +199,9 @@ test_debug () {
}

test_run_ () {
eval >&3 2>&4 "$1"
eval > output 2>&1 "$1"
eval_ret="$?"
cat >&3 output
return 0
}

Expand Down Expand Up @@ -260,6 +261,57 @@ test_expect_success () {
echo >&3 ""
}

test_expect_output () {
test "$#" = 2 ||
error "bug in the test script: not 2 parameters to test-expect-output"
if ! test_skip "$@"
then
say >&3 "expecting success and output: $2"
test_run_ "$2"
if [ "$?" = 0 -a "$eval_ret" = 0 ]
then
cmp_output=$(test_cmp expect output)
if [ "$?" = 0 ]
then
test_ok_ "$1"
else
test_failure_ "$@" "
$cmp_output"
fi
else
test_failure_ "$@"
fi
fi
echo >&3 ""
}

test_expect_code_and_output () {
test "$#" = 3 ||
error "bug in the test script: not 3 parameters to test-expect-code-and-output"
if ! test_skip "$@"
then
say >&3 "expecting exit code $1 and output: $3"
test_run_ "$3"
if [ "$?" = 0 -a "$eval_ret" = "$1" ]
then
cmp_output=$(test_cmp expect output)
if [ "$?" = 0 ]
then
test_ok_ "$2"
else
test_failure_ "$2" "$3" "
$cmp_output"
fi
else
cmp_output=$(test_cmp expect output)
test_failure_ "$2" "$3" "
* expected exit code $1, actual ${eval_ret}${cmp_output:+
}${cmp_output}"
fi
fi
echo >&3 ""
}

test_expect_code () {
test "$#" = 3 ||
error "bug in the test script: not 3 parameters to test-expect-code"
Expand All @@ -271,7 +323,8 @@ test_expect_code () {
then
test_ok_ "$2"
else
test_failure_ "$@"
test_failure_ "$2" "$3" "
* expected exit code $1, actual ${eval_ret}"
fi
fi
echo >&3 ""
Expand Down Expand Up @@ -542,9 +595,9 @@ test_todo_session () {
"")
if [ ! -z "$cmd" ]; then
if [ $status = 0 ]; then
test_expect_success "$1 $subnum" "$cmd > output && test_cmp expect output"
test_expect_output "$1 $subnum" "$cmd"
else
test_expect_success "$1 $subnum" "$cmd > output ; test \$? = $status && test_cmp expect output"
test_expect_code_and_output "$status" "$1 $subnum" "$cmd"
fi

subnum=$(($subnum + 1))
Expand All @@ -560,9 +613,9 @@ test_todo_session () {
done
if [ ! -z "$cmd" ]; then
if [ $status = 0 ]; then
test_expect_success "$1 $subnum" "$cmd > output && test_cmp expect output"
test_expect_output "$1 $subnum" "$cmd"
else
test_expect_success "$1 $subnum" "$cmd > output ; test \$? = $status && test_cmp expect output"
test_expect_code_and_output "$status" "$1 $subnum" "$cmd"
fi
fi
}
Expand Down

0 comments on commit 7485836

Please sign in to comment.