mhl and mhbuild ignore to long lines
[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 #fake sleeps 60 secounds and then reads all input
12 mmh_test_fakepager()
13 {
14         sleep 60
15
16         while read a
17         do
18                 sleep 0
19         done
20         exit 0
21 }
22
23 test_skip()
24 {
25         echo "$0: $1"
26         failed=120
27         exit
28 }
29
30 # portable implementation of 'which' utility
31 findprog()
32 {
33         FOUND=
34         PROG="$1"
35         IFS_SAVE="$IFS"
36         IFS=:
37         for D in $PATH; do
38                 if [ -z "$D" ]; then
39                         D=.
40                 fi
41                 if [ -f "$D/$PROG" ] && [ -x "$D/$PROG" ]; then
42                         printf '%s\n' "$D/$PROG"
43                         break
44                 fi
45         done
46         IFS="$IFS_SAVE"
47 }
48
49 require_prog()
50 {
51         if [ -z "$(findprog $1)" ]; then
52                 test_skip "missing $1"
53         fi
54 }
55
56 require_locale()
57 {
58         for locale in "$@"; do
59                 if locale -a | grep -i "$locale" >/dev/null; then
60                         return
61                 fi
62         done
63         test_skip "no suitable locale available"
64 }
65
66 # Do a best guess at FQDN
67 mh_hostname()
68 {
69         hostname -f 2>/dev/null || uname -n
70 }
71
72 # Some stuff for doing silly progress indicators
73 progress_update()
74 {
75         test -t 1 || return 0   # suppress progress meter if non-interactive
76         this="$1"
77         first="$2"
78         last="$3"
79         range="$(expr $last - $first ||:)"
80         prog="$(expr $this - $first ||:)"
81         # this automatically rounds to nearest integer
82         perc="$(expr 100 \* $prog / $range ||:)"
83         # note \r so next update will overwrite
84         printf "%3d%%\r" $perc
85 }
86
87 progress_done()
88 {
89         test -t 1 || return 0   # suppress progress meter if non-interactive
90         printf "100%%\n"
91 }
92
93
94 #### Replace generated Content-ID headers with static value
95 replace_contentid()
96 {
97         sed "/^Content-ID/s/:.*/: <TESTID>/" "$@"
98 }
99
100
101 #### Filter that squeezes blank lines, partially emulating GNU cat -s,
102 #### but sufficient for our purpose.
103 #### From http://www-rohan.sdsu.edu/doc/sed.html, compiled by Eric Pement.
104 squeeze_lines()
105 {
106         sed '/^$/N;/\n$/D'
107 }
108
109 #### Filter that converts non-breakable space U+00A0 to an ASCII space.
110 prepare_space()
111 {
112         sed 's/'"`printf '\\302\\240'`"'/ /g'
113 }
114
115
116 # first argument:       command line to run
117 # second argument:      "normspace" to normalize the whitespace
118 # stdin:                expected output
119 runandcheck()
120 {
121         if [ "$2" = "normspace" ]; then
122                 eval "$1" 2>&1 | prepare_space >"$tmpfile"
123                 diff="`prepare_space | diff -ub - "$tmpfile" || :`"
124         else
125                 eval "$1" >"$tmpfile" 2>&1
126                 diff="`diff -u - "$tmpfile"`"
127         fi
128
129         if [ "$diff" ]; then
130                 echo "$0: $1 failed"
131                 echo "$diff"
132                 failed=`expr "${failed:-0}" + 1`
133         fi
134 }