]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blob - tests/lib.sh
tests/lib.sh: Fix tests for dash.
[coloredstderr/coloredstderr.git] / tests / lib.sh
1 # Library for the test suite.
2
3 # Copyright (C) 2013  Simon Ruderich
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 set -e
19
20
21 # Allow running the script directly without running `make check`.
22 test "x$builddir" = x && builddir=.
23 test "x$EGREP" = x && EGREP='grep -E'
24
25 # The tests fail if running under coloredstderr because the tests redirect
26 # stderr to stdout which is detected by coloredstderr :D (and not colored as a
27 # result). Therefore remove LD_PRELOAD and re-exec the test.
28 if test -n "$LD_PRELOAD"; then
29     unset LD_PRELOAD
30     exec "$0"
31 fi
32
33 # Use valgrind to run the tests if it's available.
34 valgrind_cmd=
35 if type valgrind >/dev/null 2>&1; then
36     valgrind_cmd='valgrind --quiet --error-exitcode=1'
37 fi
38
39 # Clean locale for reproducible tests.
40 LC_ALL=C
41 unset LANGUAGE
42
43 # Set default COLORED_STDERR_FDS value.
44 fds=2,
45
46
47 die() {
48     echo "$@" >&2
49     exit 1
50 }
51
52 get_library_path() {
53     # Get name of the real library file from libtool's .la file.
54     dlname=`$EGREP "^dlname='" "$builddir/../src/libcoloredstderr.la"` \
55         || die 'dlname not found'
56     dlname=`echo "$dlname" | sed "s/^dlname='//; s/'$//"`
57
58     library="$builddir/../src/.libs/$dlname"
59     test -e "$library" || die "'$library' not found"
60
61     echo "`pwd`/$library"
62 }
63
64 library=`get_library_path`
65 force_write=1
66
67
68 run_test() {
69     printf '%s' "Running test '$*' .. "
70
71     testcase="$1"
72     expected="$2"
73     shift
74     shift
75
76     output="output-$$"
77
78     (
79         # Standard setup.
80         LD_PRELOAD="$library"
81         COLORED_STDERR_FDS="$fds"
82         export LD_PRELOAD
83         export COLORED_STDERR_FDS
84
85         # Change pre/post strings for simpler testing.
86         COLORED_STDERR_PRE='>STDERR>'
87         COLORED_STDERR_POST='<STDERR<'
88         export COLORED_STDERR_PRE
89         export COLORED_STDERR_POST
90         # And force writes to a file (unless we are testing the force).
91         if test -n "$force_write"; then
92             COLORED_STDERR_FORCE_WRITE=1
93             export COLORED_STDERR_FORCE_WRITE
94         fi
95
96         $valgrind_cmd "$@" "$testcase" > "$output" 2>&1
97     )
98
99     diff -u "$expected" "$output" \
100         || die 'failed!'
101     rm "$output"
102     echo 'passed.'
103 }
104
105 test_script() {
106     testcase="$1"
107     expected="$2"
108     # shift || true is not enough for dash.
109     test $# -ge 2 && shift
110     shift
111
112     if test -z "$expected"; then
113         expected="$testcase"
114     fi
115     run_test "$srcdir/$testcase" "$srcdir/$expected.expected" "$@"
116 }
117 test_script_subshell() {
118     test_script "$1" "$2" bash -c 'bash $1' ''
119 }
120 test_program() {
121     testcase="$1"
122     expected="$2"
123     test $# -ge 2 && shift
124     shift
125
126     if test -z "$expected"; then
127         expected="$testcase"
128     fi
129     run_test "$builddir/$testcase" "$srcdir/$expected.expected" "$@"
130 }
131 test_program_subshell() {
132     test_program "$1" "$2" sh -c '$1' ''
133 }