]> git.draconx.ca Git - liblbx.git/blob - tests/util/test-init.sh
52625c0d199f86f66f14957a42cb4ffdbfc68e3a
[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 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 set -e
29
30 test ${test_lib_sourced-no} = yes && return 0
31 test_lib_sourced=yes
32
33 # CDPATH is evil if used in non-interactive scripts (and even more
34 # evil if exported in the environment).
35 CDPATH=; unset CDPATH
36
37 # Be more Bourne compatible.
38 # (Snippet inspired to configure's initialization in Autoconf 2.64)
39 DUALCASE=1; export DUALCASE # for MKS sh
40 if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
41         emulate sh
42         NULLCMD=:
43         setopt NO_GLOB_SUBST
44         # If Zsh is not started directly in POSIX-compatibility mode, it has
45         # some incompatibilities in the handling of $0 that conflict with our
46         # usage; i.e., $0 inside a file sourced with the '.' builtin is
47         # temporarily set to the name of the sourced file.  Work around that.
48         # Note that a bug in some versions of Zsh prevents us from resetting $0
49         # in a sourced script, so the use of $argv0.  For more info see:
50         #   <http://www.zsh.org/mla/workers/2009/msg01140.html>
51         # The apparently useless 'eval' here is needed by at least dash 0.5.2,
52         # to prevent it from bailing out with an error like:
53         #   "Syntax error: Bad substitution".
54         eval 'argv0=${functrace[-1]%:*}' && test -f "$argv0" || {
55                 echo "Cannot determine the path of running test script." >&2
56                 echo "Your Zsh (version $ZSH_VERSION) is probably too old." >&2
57                 exit 99
58         }
59 else
60         argv0=$0
61         # Ignore command substitution failure, for it might cause problems
62         # with "set -e" on some shells.
63         am_shell_opts=$(set -o) || :
64         case $am_shell_opts in *posix*) set -o posix;; esac
65         unset am_shell_opts
66 fi
67
68 # A single whitespace character.
69 sp=' '
70 # A tabulation character.
71 tab='   '
72 # A newline character.
73 nl='
74 '
75
76 # As autoconf-generated configure scripts do, ensure that IFS
77 # is defined initially, so that saving and restoring $IFS works.
78 IFS=$sp$tab$nl
79
80 . "$srcdir/tests/util/tap-functions.sh"
81 . "$srcdir/tests/util/test-defs.sh"
82
83 testdata=$srcdir/tests/testdata
84
85 # We need a scratch directory for most tests, so set that up now.
86 dx_testname=`expr x"$argv0" : x'.*/\(.*\)\.'`
87 dx_testdir=tests/$dx_testname.dir
88
89 dx_cleanup_testdir()
90 {
91         exitstatus=$1
92
93         cd "$builddir"
94         if test "$exitstatus" -eq 0 &&
95            test $tap_pass_count_ -eq $tap_count_
96         then
97                 rm -rf "$dx_testdir"
98         fi
99 }
100
101 dx_create_testdir()
102 {
103         $MKDIR_P "$dx_testdir"
104         trap 'dx_cleanup_testdir $?' 0
105         cd "$dx_testdir"
106 }
107
108 dx_pam_header()
109 {
110         awk 'BEGIN { ret=1 }
111                 NR==1 { if (!/^P7$/) exit 1 }
112                 /^P7$/,/^ENDHDR$/ { ret=0; if ($2) print $1"="$2 }
113                 /^ENDHDR$/ { print $1"="NR+1; exit }
114                 END { exit ret }' ${1+"$@"}
115 }
116
117 dx_check_pam_md5_() {
118         dx_pam_header "$1" > "$1.sh" || return
119         . "./$1.sh" || return
120
121         tail -n +"$ENDHDR" "$1" > "$1.px" || return
122         echo "$2  $1.px" > "$1.md5" || return
123         $MD5SUM -c "$1.md5"
124 }
125
126 dx_check_pam_md5() {
127         if $HAVE_MD5SUM; then
128                 command_ok_ "$1" dx_check_pam_md5_ "$1" "$2"
129         else
130                 skip_ "$1"
131         fi
132 }
133
134 # Variables to run the LBX tools.  Default to the build tree, but can also be
135 # set explicitly to test installed tools.
136 : "${LBXTOOL=$builddir/lbxtool$EXEEXT}"
137 : "${LBXIMG=$builddir/lbximg$EXEEXT}"
138
139 # Set MALLOC_CHECK_ in the environment of the test programs which will enable
140 # malloc instrumentation on glibc-based systems to hopefully catch some forms
141 # of memory corruption.
142 dx_test_wrapper="$builddir/libtool --mode=execute env MALLOC_CHECK_=3"
143 LBXTOOL="$dx_test_wrapper $LBXTOOL"
144 LBXIMG="$dx_test_wrapper $LBXIMG"