0fd3afb9ed3c3f4ae94148c279f3a82c92f6315e
[mmh] / test / common.sh
1 # Common helper routines for test shell scripts
2 # -- intended to be sourced by them
3
4 test_skip()
5 {
6         WHY="$1"
7         echo "Test $0 SKIP ($WHY)"
8         exit 120
9 }
10
11 # portable implementation of 'which' utility
12 findprog()
13 {
14         FOUND=
15         PROG="$1"
16         IFS_SAVE="$IFS"
17         IFS=:
18         for D in $PATH; do
19                 if [ -z "$D" ]; then
20                         D=.
21                 fi
22                 if [ -f "$D/$PROG" ] && [ -x "$D/$PROG" ]; then
23                         printf '%s\n' "$D/$PROG"
24                         break
25                 fi
26         done
27         IFS="$IFS_SAVE"
28 }
29
30 require_prog()
31 {
32         if [ -z "$(findprog $1)" ]; then
33                 test_skip "missing $1"
34         fi
35 }
36
37 # Some stuff for doing silly progress indicators
38 progress_update()
39 {
40         test -t 1 || return 0   # supress progress meter if non-interactive
41         THIS="$1"
42         FIRST="$2"
43         LAST="$3"
44         RANGE="$(expr $LAST - $FIRST ||:)"
45         PROG="$(expr $THIS - $FIRST ||:)"
46         # this automatically rounds to nearest integer
47         PERC="$(expr 100 \* $PROG / $RANGE ||:)"
48         # note \r so next update will overwrite
49         printf "%3d%%\r" $PERC
50 }
51
52 progress_done()
53 {
54         test -t 1 || return 0   # supress progress meter if non-interactive
55         printf "100%%\n"
56 }