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")
20 if [[ "$o_nocolor" != "true" && -n "$1" ]]; then
21 export TERM=$ORIGINAL_TERM
23 error) tput $C_BOLD; tput $C_SETAF 1;;
24 skip) tput $C_SETAF 5;;
26 (($o_verbose == 0)) && return
28 pass) tput $C_BOLD; tput $C_SETAF 2;;
29 info) tput $C_SETAF 3;;
31 (($o_verbose == 0)) && return
37 if [[ "$o_nocolor" != "true" && -n "$1" ]]; then
46 [[ "$exit_ok" == "true" ]] && exit $code
47 say_color error "FATAL: Unexpected exit with code $code"
53 say_color error "error: $*"
65 local rv="$1" expectation="$2"
67 if [[ "$expectation" == "success" ]]; then
68 (($rv == 0)) && return 0 || return 1
70 if (($rv > 129 && $rv <= 192)); then
71 echo >&2 "died by signal"
74 if (($rv == 127)); then
75 echo >&2 "command not found"
79 echo >&2 "command was supposed to fail but succeeded"
87 local f expectation="$3" ret
92 if retval_ok "$ret" "$expectation"; then
94 say_color ok "ok $test_count - $1"
98 say_color error "not ok - $test_count $1"
99 f="$o_results_dir/${0##*/}-$$.out"
100 if [[ -s "$f" ]]; then
101 sed -e 's/^/# /' < "$f"
103 sed -e 's/^/# /' <<< "$2"
105 [[ "$o_immediate" != "true" ]] && return
112 (($# != 2)) && error "bug: not 2 parameters to test_skip()"
115 say_color skip >&3 "skipping test $this_test.$test_count ($1): $2"
116 say_color skip "ok $test_count - $1 # skipped ($2)"
119 test_require_objects()
124 # if no objects were given, we assume this test is run manually
125 # rather than via "make test". We won't check anything in this case
126 [[ -z "$o_objects" ]] && return
130 for o2 in $o_objects; do
131 [[ "$o1" != "$o2" ]] && continue
135 [[ "$found" == "true" ]] && continue
136 [[ -n "$result" ]] && result+=" "
142 test_require_executables()
148 [[ -n "$(builtin type -t "$i")" ]] && continue
149 [[ -n "$result" ]] && result+=" "
157 local t=$(exec 2>&1 1>/dev/null; time -p "$@")
158 result=$(awk '{print $2 * 1000; exit 0}' <<< "$t")
161 test_expect_success()
163 (($# != 2)) && error "bug: not 2 parameters to test_expect_success()"
164 echo >&3 "expecting success: $2"
165 _test_run "$1" "$2" "success"
169 test_expect_failure()
171 (($# != 2)) && error "bug: not 2 parameters to test_expect_failure()"
172 echo >&3 "expecting failure: $2"
173 _test_run "$1" "$2" "failure"
179 test_results_path="$o_results_dir/${0##*/}-$$.counts"
181 echo "total $test_count"
182 echo "success $test_success"
183 echo "failed $test_failure"
184 echo "skipped $test_skipped"
186 } > $test_results_path
189 msg="$test_count test(s) ($test_skipped test(s) skipped)"
190 if (($test_failure == 0)); then
191 say_color pass "# ${0##*/}: passed all $msg"
194 say_color error "# ${0##*/}: failed $test_failure among $msg"
199 sanitize_environment()
218 [[ "$TERM" == "dumb" ]] && return
221 tput $C_BOLD &>/dev/null || {
223 tput $C_BOLD &>/dev/null
226 tput $C_SETAF 1 &>/dev/null || {
228 tput $C_SETAF 1 &>/dev/null
231 tput $C_SGR0 >/dev/null 2>&1 || {
233 tput $C_SGR0 &>/dev/null
242 -i|--immediate) o_immediate="true"; shift;;
243 -l|--long) export o_long="true"; shift;;
244 -h|--help) o_help="true"; shift;;
245 -v=0|--verbose=0) o_verbose="0"; shift;;
246 -v=1|--verbose=1) o_verbose="1"; shift;;
247 -v|--verbose|-v=2|--verbose=2) o_verbose="2"; shift;;
248 --no-color) o_nocolor="true"; shift;;
249 --man-dir) export o_man_dir="$2"; shift; shift;;
250 --results-dir) o_results_dir="$2"; shift; shift;;
251 --trash-dir) o_trash_dir="$2"; shift; shift;;
252 --executables-dir) export o_executables_dir="$2"; shift; shift;;
253 --executables) export o_executables="$2"; shift; shift;;
254 --objects) export o_objects="$2"; shift; shift;;
255 *) echo "error: unknown test option '$1'" >&2; exit 1;;
258 [[ -z "$o_verbose" ]] && o_verbose=1
261 create_trash_dir_and_cd()
263 local trash="$o_trash_dir/trash-dir.${0##*/}"
265 rm -rf "$trash" || error "could not remove trash dir"
266 mkdir -p "$trash" || error "could not make trash dir"
267 cd "$trash" || error "could not change to trash dir"
274 test_dir="$(realpath $wd/${0%/*})"
275 test_audio_file_dir="$test_dir/audio_files"
277 [[ -z "$o_results_dir" ]] && o_results_dir="$test_dir/test-results"
278 [[ -z "$o_executables_dir" ]] && o_executables_dir="$test_dir/.."
279 [[ -z "$o_trash_dir" ]] && o_trash_dir="$test_dir/trashes"
280 [[ -z "$o_man_dir" ]] && o_man_dir="$test_dir/../build/man/man1"
282 # we want alsolute paths because relative paths become invalid
283 # after changing to the trash dir
284 [[ -n "${o_results_dir##/*}" ]] && o_results_dir="$wd/$o_results_dir"
285 [[ -n "${o_executables_dir##/*}" ]] && o_executables_dir="$wd/$o_results_dir"
286 [[ -n "${o_man_dir##/*}" ]] && o_man_dir="$wd/$o_man_dir"
287 [[ -n "${o_trash_dir##/*}" ]] && o_trash_dir="$wd/$o_trash_dir"
289 mkdir -p "$o_results_dir"
293 if [[ "$o_nocolor" != "true" ]]; then
295 [[ "$result" != "true" ]] && o_nocolor="true"
298 # Each test must set test_description
299 [[ -z "${test_description}" ]] && error "${0##*/} did not set test_description"
300 if [[ "$o_help" == "true" ]]; then
302 sed -e '1!d' <<< "$test_description"
303 if (($o_verbose >= 2)); then
305 sed -e '1,2d' -e 's/^/ /g' <<<"$test_description"
312 [[ -z "$o_executables" ]] && o_executables="para_afh para_audioc para_audiod
313 para_client para_mixer para_filter para_gui para_recv para_server
315 for exe in $o_executables; do
316 export $(tr 'a-z' 'A-Z' <<< $exe)="$o_executables_dir/$exe"
326 create_trash_dir_and_cd
328 if (($o_verbose >= 2)); then
331 exec 4>$o_results_dir/${0##*/}-$$.out 3>&4
337 say_color run "# running ${0##*/}"