Use the same mh_hostname() function from test/common.h in mhsign(1)
[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 # Do a best guess at FQDN
56 mh_hostname()
57 {
58         hostname -f 2>/dev/null || uname -n
59 }
60
61 # Some stuff for doing silly progress indicators
62 progress_update()
63 {
64         test -t 1 || return 0   # suppress progress meter if non-interactive
65         this="$1"
66         first="$2"
67         last="$3"
68         range="$(expr $last - $first ||:)"
69         prog="$(expr $this - $first ||:)"
70         # this automatically rounds to nearest integer
71         perc="$(expr 100 \* $prog / $range ||:)"
72         # note \r so next update will overwrite
73         printf "%3d%%\r" $perc
74 }
75
76 progress_done()
77 {
78         test -t 1 || return 0   # suppress progress meter if non-interactive
79         printf "100%%\n"
80 }
81
82
83
84 #### Filter that squeezes blank lines, partially emulating GNU cat -s,
85 #### but sufficient for our purpose.
86 #### From http://www-rohan.sdsu.edu/doc/sed.html, compiled by Eric Pement.
87 squeeze_lines()
88 {
89         sed '/^$/N;/\n$/D'
90 }
91
92 #### Filter that converts non-breakable space U+00A0 to an ASCII space.
93 prepare_space()
94 {
95         sed 's/'"`printf '\\302\\240'`"'/ /g'
96 }
97
98
99 # first argument:       command line to run
100 # second argument:      "normspace" to normalize the whitespace
101 # stdin:                expected output
102 runandcheck()
103 {
104         if [ "$2" = "normspace" ]; then
105                 eval "$1" 2>&1 | prepare_space >"$tmpfile"
106                 diff="`prepare_space | diff -ub - "$tmpfile" || :`"
107         else
108                 eval "$1" >"$tmpfile" 2>&1
109                 diff="`diff -u - "$tmpfile"`"
110         fi
111
112         if [ "$diff" ]; then
113                 echo "$0: $1 failed"
114                 echo "$diff"
115                 failed=`expr "${failed:-0}" + 1`
116         fi
117 }