]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blob - tests/lib.sh
tests: Handle `sed` which append a trailing newline.
[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 # Clear user defined variables.
44 unset COLORED_STDERR_FDS
45 unset COLORED_STDERR_FORCE_WRITE
46 # Set default COLORED_STDERR_PRIVATE_FDS value.
47 fds=2,
48
49
50 die() {
51     echo "$@" >&2
52     exit 1
53 }
54
55 get_library_path() {
56     # Get name of the real library file from libtool's .la file.
57     dlname=`$EGREP "^dlname='" "$builddir/../src/libcoloredstderr.la"` \
58         || die 'dlname not found'
59     dlname=`echo "$dlname" | sed "s/^dlname='//; s/'$//"`
60
61     library="$builddir/../src/.libs/$dlname"
62     test -e "$library" || die "'$library' not found"
63
64     echo "`pwd`/$library"
65 }
66
67 library=`get_library_path`
68 force_write=1
69
70
71 run_test() {
72     printf '%s' "Running test '$*' .. "
73
74     testcase="$1"
75     expected="$2"
76     shift
77     shift
78
79     output="output-$$"
80
81     (
82         # Standard setup.
83         LD_PRELOAD="$library"
84         COLORED_STDERR_PRIVATE_FDS="$fds"
85         export LD_PRELOAD
86         export COLORED_STDERR_PRIVATE_FDS
87
88         # Change pre/post strings for simpler testing.
89         COLORED_STDERR_PRE='>STDERR>'
90         COLORED_STDERR_POST='<STDERR<'
91         export COLORED_STDERR_PRE
92         export COLORED_STDERR_POST
93         # And force writes to a file (unless we are testing the force).
94         if test -n "$force_write"; then
95             COLORED_STDERR_FORCE_WRITE=1
96             export COLORED_STDERR_FORCE_WRITE
97         fi
98
99         $valgrind_cmd "$@" "$testcase" > "$output" 2>&1
100     )
101
102     # Some sed implementations (e.g. on FreeBSD 9.1) always append a trailing
103     # newline. Add "EOF" to detect if the real output had one.
104     echo EOF >> "$output"
105
106     # Merge continuous regions of colored output. The exact calls don't matter
107     # as long as the output is colored.
108     sed 's/<STDERR<>STDERR>//g' < "$output" > "$output.tmp"
109     mv "$output.tmp" "$output"
110
111     diff -u "$expected" "$output" \
112         || die 'failed!'
113     rm "$output"
114     echo 'passed.'
115 }
116
117 test_script() {
118     testcase="$1"
119     expected="$2"
120     # shift || true is not enough for dash.
121     test $# -ge 2 && shift
122     shift
123
124     if test -z "$expected"; then
125         expected="$testcase"
126     fi
127     run_test "$srcdir/$testcase" "$srcdir/$expected.expected" "$@"
128 }
129 test_script_subshell() {
130     test_script "$1" "$2" sh -c 'sh $1' ''
131 }
132 test_program() {
133     testcase="$1"
134     expected="$2"
135     test $# -ge 2 && shift
136     shift
137
138     if test -z "$expected"; then
139         expected="$testcase"
140     fi
141     run_test "$builddir/$testcase" "$srcdir/$expected.expected" "$@"
142 }
143 test_program_subshell() {
144     test_program "$1" "$2" sh -c '$1' ''
145 }