]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blob - tests/lib.sh
957ff55ef49a3c1e678f1c65659466a7a41b587f
[coloredstderr/coloredstderr.git] / tests / lib.sh
1 # Library for the test suite.
2
3 # Copyright (C) 2013-2015  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$abs_builddir" = x && abs_builddir="`pwd`"
24 test "x$EGREP" = x && EGREP='grep -E'
25
26 # The tests fail if running under coloredstderr because the tests redirect
27 # stderr to stdout which is detected by coloredstderr :D (and not colored as a
28 # result). Therefore remove LD_PRELOAD and re-exec the test.
29 if test -n "$LD_PRELOAD"; then
30     unset LD_PRELOAD
31     exec "$0"
32 fi
33
34 # Use valgrind to run the tests if it's available.
35 valgrind_cmd=
36 if type valgrind >/dev/null 2>&1; then
37     valgrind_cmd='valgrind --quiet --error-exitcode=1'
38 fi
39
40 # Clean locale for reproducible tests.
41 LC_ALL=C
42 unset LANGUAGE
43
44 # Clear user defined variables.
45 unset COLORED_STDERR_FDS
46 unset COLORED_STDERR_FORCE_WRITE
47 # Set default COLORED_STDERR_PRIVATE_FDS value.
48 fds=2,
49
50
51 die() {
52     echo "$@" >&2
53     exit 1
54 }
55
56 get_library_path() {
57     # Get name of the real library file from libtool's .la file.
58     dlname=`$EGREP "^dlname='" "$builddir/../src/libcoloredstderr.la"` \
59         || die 'dlname not found'
60     dlname=`echo "$dlname" | sed "s/^dlname='//; s/'$//"`
61
62     library="$builddir/../src/.libs/$dlname"
63     test -e "$library" || die "'$library' not found"
64
65     echo "`pwd`/$library"
66 }
67
68 library=`get_library_path`
69 force_write=1
70
71
72 run_test() {
73     printf '%s' "Running test '$*' .. "
74
75     testcase="$1"
76     expected="$2"
77     shift
78     shift
79
80     output="output-$$"
81
82     (
83         # Standard setup.
84         LD_PRELOAD="$library"
85         COLORED_STDERR_PRIVATE_FDS="$fds"
86         export LD_PRELOAD
87         export COLORED_STDERR_PRIVATE_FDS
88
89         # Change pre/post strings for simpler testing.
90         COLORED_STDERR_PRE='>STDERR>'
91         COLORED_STDERR_POST='<STDERR<'
92         export COLORED_STDERR_PRE
93         export COLORED_STDERR_POST
94         # And force writes to a file (unless we are testing the force).
95         if test -n "$force_write"; then
96             COLORED_STDERR_FORCE_WRITE=1
97             export COLORED_STDERR_FORCE_WRITE
98         fi
99
100         $valgrind_cmd "$@" "$testcase" > "$output" 2>&1
101     )
102
103     # Some sed implementations (e.g. on FreeBSD 9.1) always append a trailing
104     # newline. Add "EOF" to detect if the real output had one.
105     echo EOF >> "$output"
106
107     # Merge continuous regions of colored output. The exact calls don't matter
108     # as long as the output is colored.
109     sed 's/<STDERR<>STDERR>//g' < "$output" > "$output.tmp"
110     mv "$output.tmp" "$output"
111
112     diff -u "$expected" "$output" \
113         || die 'failed!'
114     rm "$output"
115     echo 'passed.'
116 }
117
118 test_script() {
119     testcase="$1"
120     expected="$2"
121     # shift || true is not enough for dash.
122     test $# -ge 2 && shift
123     shift
124
125     if test -z "$expected"; then
126         expected="$testcase"
127     fi
128     run_test "$srcdir/$testcase" "$srcdir/$expected.expected" "$@"
129 }
130 test_script_subshell() {
131     test_script "$1" "$2" sh -c 'sh $1' ''
132 }
133 test_program() {
134     testcase="$1"
135     expected="$2"
136     test $# -ge 2 && shift
137     shift
138
139     if test -z "$expected"; then
140         expected="$testcase"
141     fi
142     run_test "$builddir/$testcase" "$srcdir/$expected.expected" "$@"
143 }
144 test_program_subshell() {
145     test_program "$1" "$2" sh -c '$1' ''
146 }