1# Common helper routines for test shell scripts
2# -- intended to be sourced by them
3
4tmpfile="`mktemp`"
5trap '
6	rm -f "$tmpfile"
7	exit "$failed"
8' 0 1 2 15
9failed=0
10
11
12test_skip()
13{
14	echo "$0: $1"
15	failed=120
16	exit
17}
18
19# portable implementation of 'which' utility
20findprog()
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
38require_prog()
39{
40	if [ -z "$(findprog $1)" ]; then
41		test_skip "missing $1"
42	fi
43}
44
45require_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
56mh_hostname()
57{
58	hostname -f 2>/dev/null || uname -n
59}
60
61# Some stuff for doing silly progress indicators
62progress_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
76progress_done()
77{
78	test -t 1 || return 0   # suppress progress meter if non-interactive
79	printf "100%%\n"
80}
81
82
83#### Replace generated Content-ID headers with static value
84replace_contentid()
85{
86	sed "/^Content-ID/s/:.*/: <TESTID>/" "$@"
87}
88
89
90#### Filter that squeezes blank lines, partially emulating GNU cat -s,
91#### but sufficient for our purpose.
92#### From http://www-rohan.sdsu.edu/doc/sed.html, compiled by Eric Pement.
93squeeze_lines()
94{
95	sed '/^$/N;/\n$/D'
96}
97
98#### Filter that converts non-breakable space U+00A0 to an ASCII space.
99prepare_space()
100{
101	sed 's/'"`printf '\\302\\240'`"'/ /g'
102}
103
104
105# first argument:	command line to run
106# second argument:	"normspace" to normalize the whitespace
107# stdin:		expected output
108runandcheck()
109{
110	if [ "$2" = "normspace" ]; then
111		eval "$1" 2>&1 | prepare_space >"$tmpfile"
112		diff="`prepare_space | diff -ub - "$tmpfile" || :`"
113	else
114		eval "$1" >"$tmpfile" 2>&1
115		diff="`diff -u - "$tmpfile"`"
116	fi
117
118	if [ "$diff" ]; then
119		echo "$0: $1 failed"
120		echo "$diff"
121		failed=`expr "${failed:-0}" + 1`
122	fi
123}
124