Fix out-of-bounds error when incorporating email from stdin
[mmh] / test / common.sh
1 # Common helper routines for test shell scripts
2 # -- intended to be sourced by them
3
4 tmpfile="`mktemp`"
5 trap '
6         rm -f "$tmpfile"
7         exit "$failed"
8 ' 0 1 2 15
9 failed=0
10
11
12 test_skip()
13 {
14         echo "$0: $1"
15         failed=120
16         exit
17 }
18
19 # portable implementation of 'which' utility
20 findprog()
21 {
22         FOUND=
23         PROG="$1"
24         IFS_SAVE="$IFS"
25         IFS=:
26         for D in $PATH; do
27                 if [ -z "$D" ]; then
28                         D=.
29                 fi
30                 if [ -f "$D/$PROG" ] && [ -x "$D/$PROG" ]; then
31                         printf '%s\n' "$D/$PROG"
32                         break
33                 fi
34         done
35         IFS="$IFS_SAVE"
36 }
37
38 require_prog()
39 {
40         if [ -z "$(findprog $1)" ]; then
41                 test_skip "missing $1"
42         fi
43 }
44
45 require_locale()
46 {
47         for locale in "$@"; do
48                 if locale -a | grep -i "$locale" >/dev/null; then
49                         return
50                 fi
51         done
52         test_skip "no suitable locale available"
53 }
54
55 # Some stuff for doing silly progress indicators
56 progress_update()
57 {
58         test -t 1 || return 0   # supress progress meter if non-interactive
59         this="$1"
60         first="$2"
61         last="$3"
62         range="$(expr $last - $first ||:)"
63         prog="$(expr $this - $first ||:)"
64         # this automatically rounds to nearest integer
65         perc="$(expr 100 \* $prog / $range ||:)"
66         # note \r so next update will overwrite
67         printf "%3d%%\r" $perc
68 }
69
70 progress_done()
71 {
72         test -t 1 || return 0   # supress progress meter if non-interactive
73         printf "100%%\n"
74 }
75
76
77
78 #### Filter that squeezes blank lines, partially emulating GNU cat -s,
79 #### but sufficient for our purpose.
80 #### From http://www-rohan.sdsu.edu/doc/sed.html, compiled by Eric Pement.
81 squeeze_lines()
82 {
83         sed '/^$/N;/\n$/D'
84 }
85
86 #### Filter that converts non-breakable space U+00A0 to an ASCII space.
87 prepare_space()
88 {
89         sed 's/'"`printf '\\302\\240'`"'/ /g'
90 }
91
92
93 # first argument:       command line to run
94 # second argument:      "normspace" to normalize the whitespace
95 # stdin:                expected output
96 runandcheck()
97 {
98         if [ "$2" = "normspace" ]; then
99                 eval "$1" 2>&1 | prepare_space >"$tmpfile"
100                 diff="`prepare_space | diff -ub - "$tmpfile" || :`"
101         else
102                 eval "$1" >"$tmpfile" 2>&1
103                 diff="`diff -u - "$tmpfile"`"
104         fi
105
106         if [ "$diff" ]; then
107                 echo "$0: $1 failed"
108                 echo "$diff"
109                 failed=`expr "${failed:-0}" + 1`
110         fi
111 }