X-Git-Url: https://ruderich.org/simon/gitweb/?p=coloredstderr%2Fcoloredstderr.git;a=blobdiff_plain;f=tests%2Flib.sh;h=957ff55ef49a3c1e678f1c65659466a7a41b587f;hp=94807699c8f305c2bd207031986257beeffa7c5d;hb=8a65b4486febf00e3fad5bafc3773a811e675a4c;hpb=fb9548453fbc0dedc8f145e8468fc601de784c32 diff --git a/tests/lib.sh b/tests/lib.sh index 9480769..957ff55 100644 --- a/tests/lib.sh +++ b/tests/lib.sh @@ -1,6 +1,6 @@ # Library for the test suite. -# Copyright (C) 2013 Simon Ruderich +# Copyright (C) 2013-2015 Simon Ruderich # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -15,17 +15,38 @@ # 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$abs_builddir" = x && abs_builddir="`pwd`" 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 +# Clear user defined variables. +unset COLORED_STDERR_FDS +unset COLORED_STDERR_FORCE_WRITE +# Set default COLORED_STDERR_PRIVATE_FDS value. +fds=2, + die() { echo "$@" >&2 @@ -56,36 +77,70 @@ run_test() { shift shift + output="output-$$" + ( # Standard setup. LD_PRELOAD="$library" - COLORED_STDERR_FDS=2, + COLORED_STDERR_PRIVATE_FDS="$fds" export LD_PRELOAD - export COLORED_STDERR_FDS + export COLORED_STDERR_PRIVATE_FDS # Change pre/post strings for simpler testing. - COLORED_STDERR_PRE='>stderr>' - COLORED_STDERR_POST=' output 2>&1 + $valgrind_cmd "$@" "$testcase" > "$output" 2>&1 ) - diff -u "$expected" output \ + # Some sed implementations (e.g. on FreeBSD 9.1) always append a trailing + # newline. Add "EOF" to detect if the real output had one. + echo EOF >> "$output" + + # Merge continuous regions of colored output. The exact calls don't matter + # as long as the output is colored. + sed 's/STDERR>//g' < "$output" > "$output.tmp" + mv "$output.tmp" "$output" + + diff -u "$expected" "$output" \ || die 'failed!' - rm output + rm "$output" echo 'passed.' } test_script() { - run_test "$srcdir/$1" "$srcdir/$1.expected" + testcase="$1" + expected="$2" + # shift || true is not enough for dash. + test $# -ge 2 && shift + shift + + if test -z "$expected"; then + expected="$testcase" + fi + run_test "$srcdir/$testcase" "$srcdir/$expected.expected" "$@" +} +test_script_subshell() { + test_script "$1" "$2" sh -c 'sh $1' '' } test_program() { - run_test "$builddir/$1" "$srcdir/$1.expected" + testcase="$1" + expected="$2" + test $# -ge 2 && shift + shift + + if test -z "$expected"; then + expected="$testcase" + fi + run_test "$builddir/$testcase" "$srcdir/$expected.expected" "$@" +} +test_program_subshell() { + test_program "$1" "$2" sh -c '$1' '' }