]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blob - tests/lib.sh
m4/ax_c___attribute__.m4: Add.
[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
19 # Allow running the script directly without running `make check`.
20 test "x$builddir" = x && builddir=.
21 test "x$EGREP" = x && EGREP='grep -E'
22
23 # In case we are called with LD_PRELOAD already set.
24 unset LD_PRELOAD
25 # Clean locale for reproducible tests.
26 LC_ALL=C
27 unset LANGUAGE
28
29
30 die() {
31     echo "$@" >&2
32     exit 1
33 }
34
35 get_library_path() {
36     # Get name of the real library file from libtool's .la file.
37     dlname=`$EGREP "^dlname='" "$builddir/../src/libcoloredstderr.la"` \
38         || die 'dlname not found'
39     dlname=`echo "$dlname" | sed "s/^dlname='//; s/'$//"`
40
41     library="$builddir/../src/.libs/$dlname"
42     test -e "$library" || die "'$library' not found"
43
44     echo "`pwd`/$library"
45 }
46
47 library=`get_library_path`
48 force_write=1
49
50
51 run_test() {
52     printf '%s' "Running test '$*' .. "
53
54     testcase="$1"
55     expected="$2"
56     shift
57     shift
58
59     (
60         # Standard setup.
61         LD_PRELOAD="$library"
62         COLORED_STDERR_FDS=2,
63         export LD_PRELOAD
64         export COLORED_STDERR_FDS
65
66         # Change pre/post strings for simpler testing.
67         COLORED_STDERR_PRE='>STDERR>'
68         COLORED_STDERR_POST='<STDERR<'
69         export COLORED_STDERR_PRE
70         export COLORED_STDERR_POST
71         # And force writes to a file (unless we are testing the force).
72         if test "x$force_write" != x; then
73             COLORED_STDERR_FORCE_WRITE=1
74             export COLORED_STDERR_FORCE_WRITE
75         fi
76
77         $valgrind_cmd "$@" "$testcase" > output 2>&1
78     )
79
80     diff -u "$expected" output \
81         || die 'failed!'
82     rm output
83     echo 'passed.'
84 }
85
86 test_script() {
87     testcase="$1"
88     shift
89     run_test "$srcdir/$testcase" "$srcdir/$testcase.expected" "$@"
90 }
91 test_script_subshell() {
92     test_script "$1" bash -c 'bash $1' ''
93 }
94 test_program() {
95     testcase="$1"
96     shift
97     run_test "$builddir/$testcase" "$srcdir/$testcase.expected" "$@"
98 }
99 test_program_subshell() {
100     test_program "$1" sh -c '$1' ''
101 }