]> git.tuebingen.mpg.de Git - paraslash.git/blob - t/test-lib.sh
e444e5780d2f152eb9755916375996c9340016cd
[paraslash.git] / t / test-lib.sh
1 #!/bin/bash
2
3 # paraslash test suite helper functions
4 # Licensed under the GPL v2. For licencing details see COPYING.
5 # uses ideas and code from git's test-lib.sh, Copyright (c) 2005 Junio C Hamano
6
7
8 get_audio_file_paths()
9 {
10         local suffix="$1"
11
12         if (($# == 0)); then
13                 result=$(find "$test_audio_file_dir" -type f)
14         else
15                 result=$(find "$test_audio_file_dir" -type f -name "*.$suffix")
16         fi
17 }
18
19 say_color()
20 {
21         if [[ "$o_nocolor" != "true" && -n "$1" ]]; then
22                 export TERM=$ORIGINAL_TERM
23                 case "$1" in
24                         error) tput bold; tput setaf 1;;
25                         skip)  tput setaf 5;;
26                         ok)
27                                 (($o_verbose == 0)) && return
28                                 tput setaf 2;;
29                         pass)  tput bold; tput setaf 2;;
30                         info)  tput setaf 3;;
31                         run)
32                                 (($o_verbose == 0)) && return
33                                 tput setaf 6;;
34                 esac
35         fi
36         shift
37         printf "%s\n" "$*"
38         if [[ "$o_nocolor" != "true" && -n "$1" ]]; then
39                 tput sgr0
40                 export TERM=dumb
41         fi
42 }
43
44 die()
45 {
46         local code=$?
47         [[ "$exit_ok" == "true" ]] && exit $code
48         say_color error "FATAL: Unexpected exit with code $code"
49         exit 1
50 }
51
52 error()
53 {
54         say_color error "error: $*"
55         exit_ok="true"
56         exit 1
57 }
58
59 say()
60 {
61         say_color info "$*"
62 }
63
64 _test_run()
65 {
66         local f
67
68         let test_count++
69         eval >&3 2>&4 "$2"
70         if (($? == 0)); then
71                 let test_success++
72                 say_color ok "ok $test_count - $1"
73                 return
74         fi
75         let test_failure++
76         say_color error "not ok - $test_count $1"
77         f="$o_results_dir/${0##*/}-$$.out"
78         if [[ -s "$f" ]]; then
79                 sed -e 's/^/#   /' < "$f"
80         else
81                 sed -e 's/^/#   /' <<< "$2"
82         fi
83         [[ "$o_immediate" != "true" ]] && return
84         exit_ok="true"
85         exit 1
86 }
87
88 test_skip()
89 {
90         (($# != 2)) && error "bug: not 2 parameters to test_skip()"
91         let test_count++
92         let test_skipped++
93         say_color skip >&3 "skipping test $this_test.$test_count ($1): $2"
94         say_color skip "ok $test_count - $1 # skipped ($2)"
95 }
96
97 test_require_objects()
98 {
99         local o1 o2 found
100
101         result=
102         # if no objects were given, we assume this test is run manually
103         # rather than via "make test". We won't check anything in this case
104         [[ -z "$o_objects" ]] && return
105
106         for o1 in $1; do
107                 found=
108                 for o2 in $o_objects; do
109                         [[ "$o1" != "$o2" ]] && continue
110                         found="true"
111                         break
112                 done
113                 [[ "$found" == "true" ]] && continue
114                 [[ -n "$result" ]] && result+=" "
115                 result+="$o1"
116         done
117         [[ -z "$result" ]]
118 }
119
120 test_require_executables()
121 {
122         local i
123
124         result=
125         for i in "$@"; do
126                 [[ -n "$(builtin type -t "$i")" ]] && continue
127                 [[ -n "$result" ]] && result+=" "
128                 result+="$i"
129         done
130         [[ -z "$result" ]]
131 }
132
133 test_duration()
134 {
135         local t=$(exec 2>&1 1>/dev/null; time -p "$@")
136         result=$(awk '{print $2 * 1000}' <<< $t)
137 }
138
139 test_expect_success()
140 {
141         (($# != 2)) && error "bug: not 2 parameters to test_expect_success()"
142         say >&3 "expecting success: $2"
143         _test_run "$1" "$2"
144         echo >&3 ""
145 }
146
147 test_done()
148 {
149         test_results_path="$o_results_dir/${0##*/}-$$.counts"
150         {
151                 echo "total $test_count"
152                 echo "success $test_success"
153                 echo "failed $test_failure"
154                 echo "skipped $test_skipped"
155                 echo
156         } > $test_results_path
157
158         exit_ok="true"
159         msg="$test_count test(s) ($test_skipped test(s) skipped)"
160         if (($test_failure == 0)); then
161                 say_color pass "# ${0##*/}: passed all $msg"
162                 exit 0
163         else
164                 say_color error "# ${0##*/}: failed $test_failure among $msg"
165                 exit 1
166         fi
167 }
168
169 sanitize_environment()
170 {
171         export LANG=C
172         export LC_ALL=C
173         export PAGER=cat
174         export TZ=UTC
175         export TERM=dumb
176         export EDITOR=:
177         export HOME=$(pwd)
178
179         unset VISUAL
180         unset EMAIL
181         unset CDPATH
182         unset GREP_OPTIONS
183 }
184
185 can_use_colors()
186 {
187         result="false"
188         [[ "$TERM" == "dumb" ]] && return
189         [[ -t 1 ]] || return
190         tput bold >/dev/null 2>&1 || return
191         tput setaf 1 >/dev/null 2>&1 || return
192         tput sgr0 >/dev/null 2>&1 || return
193         result="true"
194 }
195
196 parse_options()
197 {
198         while (($# > 0)); do
199                 case "$1" in
200                 -i|--immediate) o_immediate="true"; shift;;
201                 -l|--long) export o_long="true"; shift;;
202                 -h|--help) o_help="true"; shift;;
203                 -v=0|--verbose=0) o_verbose="0"; shift;;
204                 -v=1|--verbose=1) o_verbose="1"; shift;;
205                 -v|--verbose|-v=2|--verbose=2) o_verbose="2"; shift;;
206                 --no-color) o_nocolor="true"; shift;;
207                 --results-dir) o_results_dir="$2"; shift; shift;;
208                 --trash-dir) o_trash_dir="$2"; shift; shift;;
209                 --executables-dir) export o_executables_dir="$2"; shift; shift;;
210                 --executables) export o_executables="$2"; shift; shift;;
211                 --objects) export o_objects="$2"; shift; shift;;
212                 *) echo "error: unknown test option '$1'" >&2; exit 1;;
213                 esac
214         done
215         [[ -z "$o_verbose" ]] && o_verbose=1
216 }
217
218 create_trash_dir_and_cd()
219 {
220         local trash="$o_trash_dir/trash-dir.${0##*/}"
221
222         rm -rf "$trash" || error "could not remove trash dir"
223         mkdir -p "$trash" || error "could not make trash dir"
224         cd "$trash" || error "could not change to trash dir"
225 }
226
227 fixup_dirs()
228 {
229         local wd=$(pwd)
230
231         test_dir="$wd/${0%/*}"
232         test_audio_file_dir="$test_dir/audio_files"
233
234         [[ -z "$o_results_dir" ]] && o_results_dir="$test_dir/test-results"
235         [[ -z "$o_executables_dir" ]] && o_executables_dir="$test_dir/.."
236         [[ -z "$o_trash_dir" ]] && o_trash_dir="$test_dir/trashes"
237
238         # we want alsolute paths because relative paths become invalid
239         # after changing to the trash dir
240         [[ -n "${o_results_dir##/*}" ]] && o_results_dir="$wd/$o_results_dir"
241         [[ -n "${o_executables_dir##/*}" ]] && o_executables_dir="$wd/$o_results_dir"
242         [[ -n "${o_trash_dir##/*}" ]] && o_trash_dir="$wd/$o_trash_dir"
243
244         mkdir -p "$o_results_dir"
245 }
246
247 parse_options "$@"
248 if [[ "$o_nocolor" != "true" ]]; then
249         can_use_colors
250         [[ "$result" != "true" ]] && o_nocolor="true"
251 fi
252
253 # Each test must set test_description
254 [[ -z "${test_description}" ]] && error "${0##*/} did not set test_description"
255 if [[ "$o_help" == "true" ]]; then
256         printf "${0##*/}: "
257         sed -e '1!d' <<< "$test_description"
258         if (($o_verbose >= 2)); then
259                 echo
260                 sed -e '1,2d' -e 's/^/  /g' <<<"$test_description"
261                 echo
262         fi
263         exit 0
264 fi
265 fixup_dirs
266
267 [[ -z "$o_executables" ]] && o_executables="para_afh para_audioc para_audiod
268         para_client para_fade para_filter para_gui para_recv para_server
269         para_write"
270 for exe in $o_executables; do
271         export $(tr 'a-z' 'A-Z' <<< $exe)="$o_executables_dir/$exe"
272 done
273
274 test_failure=0
275 test_count=0
276 test_success=0
277 test_skipped=0
278
279 ORIGINAL_TERM=$TERM
280 sanitize_environment
281 create_trash_dir_and_cd
282
283 if (($o_verbose >= 2)); then
284         exec 4>&2 3>&1
285 else
286         exec 4>$o_results_dir/${0##*/}-$$.out 3>&4
287 fi
288
289 exit_ok=
290 trap 'die' EXIT
291
292 say_color run "# running ${0##*/}"