X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=tests%2Flib.sh;h=3427b5608fc36cdf7dca0824fbca4b4f97979f73;hb=1068648718dad86471451266fcdf4248bb79f3fe;hp=3e29a55023816cc539065c20c1b8ce898b00b7db;hpb=5c8b5233fbadc8963cef28d851599bf4c34d271a;p=coloredstderr%2Fcoloredstderr.git diff --git a/tests/lib.sh b/tests/lib.sh index 3e29a55..3427b56 100644 --- a/tests/lib.sh +++ b/tests/lib.sh @@ -15,17 +15,34 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +set -e + # Allow running the script directly without running `make check`. test "x$builddir" = x && builddir=. test "x$EGREP" = x && EGREP='grep -E' -# In case we are called with LD_PRELOAD already set. -unset LD_PRELOAD +# The tests fail if running under coloredstderr because the tests redirect +# stderr to stdout which is detected by coloredstderr :D (and not colored as a +# result). Therefore remove LD_PRELOAD and re-exec the test. +if test -n "$LD_PRELOAD"; then + unset LD_PRELOAD + exec "$0" +fi + +# Use valgrind to run the tests if it's available. +valgrind_cmd= +if type valgrind >/dev/null 2>&1; then + valgrind_cmd='valgrind --quiet --error-exitcode=1' +fi + # Clean locale for reproducible tests. LC_ALL=C unset LANGUAGE +# Set default COLORED_STDERR_FDS value. +fds=2, + die() { echo "$@" >&2 @@ -56,10 +73,12 @@ run_test() { shift shift + output="output-$$" + ( # Standard setup. LD_PRELOAD="$library" - COLORED_STDERR_FDS=2, + COLORED_STDERR_FDS="$fds" export LD_PRELOAD export COLORED_STDERR_FDS @@ -69,33 +88,46 @@ run_test() { export COLORED_STDERR_PRE export COLORED_STDERR_POST # And force writes to a file (unless we are testing the force). - if test "x$force_write" != x; then + if test -n "$force_write"; then COLORED_STDERR_FORCE_WRITE=1 export COLORED_STDERR_FORCE_WRITE fi - $valgrind_cmd "$@" "$testcase" > output 2>&1 + $valgrind_cmd "$@" "$testcase" > "$output" 2>&1 ) - diff -u "$expected" output \ + diff -u "$expected" "$output" \ || die 'failed!' - rm output + rm "$output" echo 'passed.' } test_script() { testcase="$1" + expected="$2" + # shift || true is not enough for dash. + test $# -ge 2 && shift shift - run_test "$srcdir/$testcase" "$srcdir/$testcase.expected" "$@" + + if test -z "$expected"; then + expected="$testcase" + fi + run_test "$srcdir/$testcase" "$srcdir/$expected.expected" "$@" } test_script_subshell() { - test_script "$1" bash -c 'bash $1' '' + test_script "$1" "$2" bash -c 'bash $1' '' } test_program() { testcase="$1" + expected="$2" + test $# -ge 2 && shift shift - run_test "$builddir/$testcase" "$srcdir/$testcase.expected" "$@" + + if test -z "$expected"; then + expected="$testcase" + fi + run_test "$builddir/$testcase" "$srcdir/$expected.expected" "$@" } test_program_subshell() { - test_program "$1" sh -c '$1' '' + test_program "$1" "$2" sh -c '$1' '' }