]> git.draconx.ca Git - liblbx.git/blob - tests/util/test-init.sh
tests: Allow custom test command wrappers
[liblbx.git] / tests / util / test-init.sh
1 # 2ooM: The Master of Orion II Reverse Engineering Project
2 # Common test suite initialization functions.
3 # Copyright © 2013-2014 Nick Bowler
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 file incorporates work covered by the following copyright and
11 # permission notice:
12 #
13 #   Copyright (C) 1996-2013 Free Software Foundation, Inc.
14 #
15 #   This program is free software; you can redistribute it and/or modify
16 #   it under the terms of the GNU General Public License as published by
17 #   the Free Software Foundation; either version 2, or (at your option)
18 #   any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
28 test ${test_lib_sourced-no} = yes && return 0
29 test_lib_sourced=yes
30
31 # CDPATH is evil if used in non-interactive scripts (and even more
32 # evil if exported in the environment).
33 CDPATH=; unset CDPATH
34
35 # Be more Bourne compatible.
36 # (Snippet inspired to configure's initialization in Autoconf 2.64)
37 DUALCASE=1; export DUALCASE # for MKS sh
38 if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
39         emulate sh
40         NULLCMD=:
41         setopt NO_GLOB_SUBST
42         # If Zsh is not started directly in POSIX-compatibility mode, it has
43         # some incompatibilities in the handling of $0 that conflict with our
44         # usage; i.e., $0 inside a file sourced with the '.' builtin is
45         # temporarily set to the name of the sourced file.  Work around that.
46         # Note that a bug in some versions of Zsh prevents us from resetting $0
47         # in a sourced script, so the use of $argv0.  For more info see:
48         #   <http://www.zsh.org/mla/workers/2009/msg01140.html>
49         # The apparently useless 'eval' here is needed by at least dash 0.5.2,
50         # to prevent it from bailing out with an error like:
51         #   "Syntax error: Bad substitution".
52         eval 'argv0=${functrace[-1]%:*}' && test -f "$argv0" || {
53                 echo "Cannot determine the path of running test script." >&2
54                 echo "Your Zsh (version $ZSH_VERSION) is probably too old." >&2
55                 exit 99
56         }
57 else
58         argv0=$0
59         # Ignore command substitution failure, for it might cause problems
60         # with "set -e" on some shells.
61         am_shell_opts=$(set -o) || :
62         case $am_shell_opts in *posix*) set -o posix;; esac
63         unset am_shell_opts
64 fi
65
66 # A single whitespace character.
67 sp=' '
68 # A tabulation character.
69 tab='   '
70 # A newline character.
71 nl='
72 '
73
74 # As autoconf-generated configure scripts do, ensure that IFS
75 # is defined initially, so that saving and restoring $IFS works.
76 IFS=$sp$tab$nl
77
78 . "$srcdir/tests/util/tap-functions.sh"
79 . "$srcdir/tests/util/test-defs.sh"
80
81 testdata=$srcdir/tests/testdata
82
83 # We need a scratch directory for most tests, so set that up now.
84 dx_testname=`expr x"$argv0" : x'.*/\(.*\)\.'`
85 dx_testdir=tests/$dx_testname.dir
86
87 dx_cleanup_testdir() {
88         exitstatus=$1
89
90         cd "$builddir" || return
91         if test "$exitstatus" -eq 0 &&
92            test $tap_pass_count_ -eq $tap_count_
93         then
94                 rm -rf "$dx_testdir"
95         fi
96 }
97
98 dx_create_testdir_() {
99         $MKDIR_P "$dx_testdir" || return
100         trap 'dx_cleanup_testdir $?' 0
101         cd "$dx_testdir"
102 }
103
104 dx_create_testdir() {
105         dx_create_testdir_ || bailout_ "failed to create test directory"
106 }
107
108 dx_pam_header() {
109         awk 'BEGIN { ret=1 }
110                 NR==1 { if (!/^P7$/) exit 1 }
111                 /^P7$/,/^ENDHDR$/ { ret=0; if ($2) print $1"="$2 }
112                 /^ENDHDR$/ { print $1"="NR+1; exit }
113                 END { exit ret }' ${1+"$@"}
114 }
115
116 # Set all transparent pixels in a PAM image to black.  This assumes (fine for
117 # LBX images) that all pixels are either fully-transparent or fully-opaque.
118 dx_pam_normalize() {
119         dx_pam_header "$1" > "$1.sh" || return
120         ( . "./$1.sh" || exit
121                 case $TUPLTYPE in
122                 GRAYSCALE_ALPHA) $PAMCHANNEL -infile "$1" 1 > "$1.alpha" ;;
123                 RGB_ALPHA) $PAMCHANNEL -infile "$1" 3 > "$1.alpha" ;;
124                 *) rm -f "$1.alpha" ;;
125                 esac
126         ) || return
127
128         if test -e "$1.alpha"; then
129                 $PAMARITH -and "$1" "$1.alpha"
130         else
131                 cat "$1"
132         fi
133 }
134
135 dx_check_pam_md5_() {
136         ( dx_pam_header "$1" > "$1.sh" || exit
137                 . "./$1.sh" || exit
138                 tail -n +"$ENDHDR" "$1" > "$1.px"
139         ) || return 55
140
141         $MD5SUM -c <<EOF
142 $2  $1.px
143 EOF
144 }
145
146 dx_check_pam_md5() {
147         if $HAVE_MD5SUM; then
148                 if dx_pam_normalize "$1" > "$1.tmp"; then
149                         mv -f "$1.tmp" "$1"
150                         dx_md5_reliable=true
151                 else
152                         dx_md5_reliable=false
153                 fi
154
155                 dx_check_pam_md5_ "$1" "$2"
156                 case $? in
157                 0)  ok_ "$1" ;;
158                 55) not_ok_ "$1" ;;
159                 *) if $dx_md5_reliable; then
160                         not_ok_ "$1"
161                 else
162                         skip_ -r "netpbm unavailable or broken" "$1"
163                 fi ;;
164                 esac
165         else
166                 skip_ -r "md5sum unavailable or broken" "$1"
167         fi
168 }
169
170 # Variables to run the LBX tools.  Default to the build tree, but can also be
171 # set explicitly to test installed tools.
172 : "${LBXTOOL=$builddir/lbxtool$EXEEXT}"
173 : "${LBXIMG=$builddir/lbximg$EXEEXT}"
174
175 dx_test_wrapper="$builddir/libtool --mode=execute${TESTWRAPPER:+ }$TESTWRAPPER"
176 LBXTOOL="$dx_test_wrapper $LBXTOOL"
177 LBXIMG="$dx_test_wrapper $LBXIMG"