Fixed the test framework by adjusting it to mmh.
[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         THIS="$1"
41         FIRST="$2"
42         LAST="$3"
43         RANGE="$(expr $LAST - $FIRST ||:)"
44         PROG="$(expr $THIS - $FIRST ||:)"
45         # this automatically rounds to nearest integer
46         PERC="$(expr 100 \* $PROG / $RANGE ||:)"
47         # note \r so next update will overwrite
48         printf "%3d%%\r" $PERC
49 }
50
51 progress_done()
52 {
53         printf "100%%\n"
54 }