3 # Test suite helper functions, uses ideas and code from git's test-lib.sh,
4 # Copyright (c) 2005 Junio C Hamano. Licensed under the GPL v2, see file
12 result=$(find "$test_audio_file_dir" -type f)
14 result=$(find "$test_audio_file_dir" -type f -name "*.$suffix")
23 if [[ "$o_nocolor" != "true" && -n "$1" ]]; then
24 export TERM=$ORIGINAL_TERM
26 error) tput $C_BOLD; tput $C_SETAF 1;;
27 skip) tput $C_SETAF 5;;
29 (($o_verbose == 0)) && return
31 pass) tput $C_BOLD; tput $C_SETAF 2;;
32 info) tput $C_SETAF 3;;
34 (($o_verbose == 0)) && return
38 if [[ "$severity" == 'error' ]]; then
39 printf "%s\n" "$*" 1>&2
43 if [[ "$o_nocolor" != "true" && -n "$1" ]]; then
52 [[ "$exit_ok" == "true" ]] && exit $code
53 say_color error "FATAL: Unexpected exit with code $code"
59 say_color error "error: $*"
71 local rv="$1" expectation="$2"
73 if [[ "$expectation" == "success" ]]; then
74 (($rv == 0)) && return 0 || return 1
76 if (($rv > 129 && $rv <= 192)); then
77 echo >&2 "died by signal"
80 if (($rv == 127)); then
81 echo >&2 "command not found"
85 echo >&2 "command was supposed to fail but succeeded"
93 local f expectation="$3" ret
98 if retval_ok "$ret" "$expectation"; then
100 say_color ok "ok $test_count - $1"
104 say_color error "not ok - $test_count $1"
105 f="$o_results_dir/${0##*/}-$$.out"
106 if [[ -s "$f" ]]; then
107 sed -e 's/^/# /' < "$f"
109 sed -e 's/^/# /' <<< "$2"
111 [[ "$o_immediate" != "true" ]] && return
118 (($# != 2)) && error "bug: not 2 parameters to test_skip()"
121 say_color skip >&3 "skipping test $this_test.$test_count ($1): $2"
122 say_color skip "ok $test_count - $1 # skipped ($2)"
125 test_require_objects()
130 # if no objects were given, we assume this test is run manually
131 # rather than via "make test". We won't check anything in this case
132 [[ -z "$o_objects" ]] && return
136 for o2 in $o_objects; do
137 [[ "$o1" != "$o2" ]] && continue
141 [[ "$found" == "true" ]] && continue
142 [[ -n "$result" ]] && result+=" "
148 test_require_executables()
154 [[ -n "$(builtin type -t "$i")" ]] && continue
155 [[ -n "$result" ]] && result+=" "
163 local t=$(exec 2>&1 1>/dev/null; time -p "$@")
164 result=$(awk '{print $2 * 1000; exit 0}' <<< "$t")
167 test_expect_success()
169 (($# != 2)) && error "bug: not 2 parameters to test_expect_success()"
170 echo >&3 "expecting success: $2"
171 _test_run "$1" "$2" "success"
175 test_expect_failure()
177 (($# != 2)) && error "bug: not 2 parameters to test_expect_failure()"
178 echo >&3 "expecting failure: $2"
179 _test_run "$1" "$2" "failure"
185 test_results_path="$o_results_dir/${0##*/}-$$.counts"
187 echo "total $test_count"
188 echo "success $test_success"
189 echo "failed $test_failure"
190 echo "skipped $test_skipped"
192 } > $test_results_path
195 msg="$test_count test(s) ($test_skipped test(s) skipped)"
196 if (($test_failure == 0)); then
197 say_color pass "# ${0##*/}: passed all $msg"
200 say_color error "# ${0##*/}: failed $test_failure among $msg"
205 sanitize_environment()
224 [[ "$TERM" == "dumb" ]] && return
227 tput $C_BOLD &>/dev/null || {
229 tput $C_BOLD &>/dev/null
232 tput $C_SETAF 1 &>/dev/null || {
234 tput $C_SETAF 1 &>/dev/null
237 tput $C_SGR0 >/dev/null 2>&1 || {
239 tput $C_SGR0 &>/dev/null
248 -i|--immediate) o_immediate="true"; shift;;
249 -l|--long) export o_long="true"; shift;;
250 -h|--help) o_help="true"; shift;;
251 -v=0|--verbose=0) o_verbose="0"; shift;;
252 -v=1|--verbose=1) o_verbose="1"; shift;;
253 -v|--verbose|-v=2|--verbose=2) o_verbose="2"; shift;;
254 --no-color) o_nocolor="true"; shift;;
255 --man-dir) export o_man_dir="$2"; shift; shift;;
256 --results-dir) o_results_dir="$2"; shift; shift;;
257 --trash-dir) o_trash_dir="$2"; shift; shift;;
258 --executables-dir) export o_executables_dir="$2"; shift; shift;;
259 --executables) export o_executables="$2"; shift; shift;;
260 --objects) export o_objects="$2"; shift; shift;;
261 *) echo "error: unknown test option '$1'" >&2; exit 1;;
264 [[ -z "$o_verbose" ]] && o_verbose=1
267 create_trash_dir_and_cd()
269 local trash="$o_trash_dir/trash-dir.${0##*/}"
271 rm -rf "$trash" || error "could not remove trash dir"
272 mkdir -p "$trash" || error "could not make trash dir"
273 cd "$trash" || error "could not change to trash dir"
280 test_dir="$(realpath $wd/${0%/*})"
281 test_audio_file_dir="$test_dir/audio_files"
283 [[ -z "$o_results_dir" ]] && o_results_dir="$test_dir/test-results"
284 [[ -z "$o_executables_dir" ]] && o_executables_dir="$test_dir/.."
285 [[ -z "$o_trash_dir" ]] && o_trash_dir="$test_dir/trashes"
286 [[ -z "$o_man_dir" ]] && o_man_dir="$test_dir/../build/man/man1"
288 # we want absolute paths because relative paths become invalid
289 # after changing to the trash dir
290 [[ -n "${o_results_dir##/*}" ]] && o_results_dir="$wd/$o_results_dir"
291 [[ -n "${o_executables_dir##/*}" ]] && o_executables_dir="$wd/$o_results_dir"
292 [[ -n "${o_man_dir##/*}" ]] && o_man_dir="$wd/$o_man_dir"
293 [[ -n "${o_trash_dir##/*}" ]] && o_trash_dir="$wd/$o_trash_dir"
295 mkdir -p "$o_results_dir"
299 if [[ "$o_nocolor" != "true" ]]; then
301 [[ "$result" != "true" ]] && o_nocolor="true"
304 # Each test must set test_description
305 [[ -z "${test_description}" ]] && error "${0##*/} did not set test_description"
306 if [[ "$o_help" == "true" ]]; then
308 sed -e '1!d' <<< "$test_description"
309 if (($o_verbose >= 2)); then
311 sed -e '1,2d' -e 's/^/ /g' <<<"$test_description"
318 [[ -z "$o_executables" ]] && o_executables="para_afh para_audioc para_audiod
319 para_client para_mixer para_filter para_gui para_recv para_server
321 for exe in $o_executables; do
322 export $(tr 'a-z' 'A-Z' <<< $exe)="$o_executables_dir/$exe"
332 create_trash_dir_and_cd
334 if (($o_verbose >= 2)); then
337 exec 4>$o_results_dir/${0##*/}-$$.out 3>&4
343 say_color run "# running ${0##*/}"