]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blob - tests/lib.sh
tests: Split run.sh in multiple test files.
[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
44 die() {
45     echo "$@" >&2
46     exit 1
47 }
48
49 get_library_path() {
50     # Get name of the real library file from libtool's .la file.
51     dlname=`$EGREP "^dlname='" "$builddir/../src/libcoloredstderr.la"` \
52         || die 'dlname not found'
53     dlname=`echo "$dlname" | sed "s/^dlname='//; s/'$//"`
54
55     library="$builddir/../src/.libs/$dlname"
56     test -e "$library" || die "'$library' not found"
57
58     echo "`pwd`/$library"
59 }
60
61 library=`get_library_path`
62 force_write=1
63
64
65 run_test() {
66     printf '%s' "Running test '$*' .. "
67
68     testcase="$1"
69     expected="$2"
70     shift
71     shift
72
73     output="output-$$"
74
75     (
76         # Standard setup.
77         LD_PRELOAD="$library"
78         COLORED_STDERR_FDS=2,
79         export LD_PRELOAD
80         export COLORED_STDERR_FDS
81
82         # Change pre/post strings for simpler testing.
83         COLORED_STDERR_PRE='>STDERR>'
84         COLORED_STDERR_POST='<STDERR<'
85         export COLORED_STDERR_PRE
86         export COLORED_STDERR_POST
87         # And force writes to a file (unless we are testing the force).
88         if test "x$force_write" != x; then
89             COLORED_STDERR_FORCE_WRITE=1
90             export COLORED_STDERR_FORCE_WRITE
91         fi
92
93         $valgrind_cmd "$@" "$testcase" > "$output" 2>&1
94     )
95
96     diff -u "$expected" "$output" \
97         || die 'failed!'
98     rm "$output"
99     echo 'passed.'
100 }
101
102 test_script() {
103     testcase="$1"
104     shift
105     run_test "$srcdir/$testcase" "$srcdir/$testcase.expected" "$@"
106 }
107 test_script_subshell() {
108     test_script "$1" bash -c 'bash $1' ''
109 }
110 test_program() {
111     testcase="$1"
112     shift
113     run_test "$builddir/$testcase" "$srcdir/$testcase.expected" "$@"
114 }
115 test_program_subshell() {
116     test_program "$1" sh -c '$1' ''
117 }