]> pd.if.org Git - zpackage/blob - t/tap.sh
have tap.sh require use tryrun
[zpackage] / t / tap.sh
1 #!/bin/sh
2
3 tn=0
4 planned=0
5
6 tryrun() {
7         program=$1
8         shift
9         $program "$@" > test.out 2>&1
10         tap_return_value=$?
11         if [ $tap_return_value -ne 0 ]; then
12                 printf 'not ';
13         fi
14         tn=$((tn + 1))
15         printf 'ok %d - %s %s\n' $tn $program "$*"
16         if [ $tap_return_value -ne 0 ]; then
17                 cat test.out | diagstdin
18                 rm test.out
19         fi
20         return $tap_return_value
21 }
22
23 require() {
24         tryrun "$@"
25         rv=$?
26         if [ $rv -ne 0 ]; then
27                 bailout "exit status $rv";
28         fi
29 }
30
31 bailout() {
32         diag "$@"
33         printf "bail out!\n";
34         exit 255;
35 }
36
37 shownote() {
38         if [ "$note" != "" ]; then
39                 printf '# %s\n' "$note"
40         fi
41 }
42
43 diag() {
44         printf '# %s\n' "$@"
45 }
46
47 diagfile() {
48         sed -e 's/^/# /' $1
49 }
50
51 diagstdin() {
52         sed -e 's/^/# /'
53 }
54
55 okexit() {
56         exitwith 0 "$*"
57 }
58
59 failsok() {
60         rv=$?
61         note=
62         if [ $rv -eq 0 ]; then
63                 printf 'not ';
64                 note=$(printf 'got "%d" expected "%d"' "$rv" "0")
65         fi
66         tn=$((tn + 1))
67         printf 'ok %d - %s\n' $tn "$*"
68         shownote
69 }
70
71 exitwith() {
72         rv=$?
73         note=
74         if [ $rv -ne $1 ]; then
75                 printf 'not ';
76                 note=$(printf 'got "%d" expected "%d"' "$rv" "$1")
77         fi
78         shift
79         tn=$((tn + 1))
80         printf 'ok %d - %s\n' $tn "$*"
81         shownote
82         return $rv
83 }
84
85 okstreq() {
86         note=
87         if [ "$1" != "$2" ]; then
88                 printf 'not ';
89                 note=$(printf 'got "%s" expected "%s"' "$1" "$2")
90         fi
91
92         shift;shift;
93         tn=$((tn + 1))
94         printf 'ok %d - %s\n' $tn "$*"
95         shownote
96 }
97
98 plan() {
99         planned=$1
100         printf '1..%d\n' $planned
101 }
102
103 finish() {
104         if [ $planned -eq 0 ]; then
105                 printf '1..%d\n' $tn
106         fi
107 }
108
109 skip() {
110         tap_skipping=$1
111         shift
112         while [ $tap_skipping -gt 0 ]; do
113                 tn=$((tn + 1))
114                 tap_skipping=$((tap_skipping - 1))
115                 printf 'ok %d - # skip %s\n' $tn "$*"
116         done
117 }