9acd0c4e25b1a627af8543d73c7022a1da4d104f
[open-adventure.git] / tests / tapview
1 #! /bin/sh
2 # tapview - a TAP (Test Anything Protocol) viewer in pure POSIX shell
3 #
4 # Copyright by Eric S. Raymond
5 #
6 # This code is intended to be embedded in your project. The author
7 # grants permission for it to be distributed under the prevailing
8 # license of your project if you choose, provided that license is
9 # OSD-compliant; otherwise the following SPDX tag applies.
10 #
11 # SPDX-License-Identifier: BSD-2-Clause
12 #
13 # This is version 1.0
14 # A newer version may be available at https://gitlab.com/esr/tapview
15 #
16 # POSIX allows but does not mandate that -n suppresses emission of a
17 # trailing newline in echo. Thus, some shell builtin echos don't do
18 # that.  Cope gracefully.
19 # shellcheck disable=SC2039
20 if [ "$(echo -n "a"; echo "b")" != "ab" ]
21 then
22     ECHO="echo"
23 elif [ "$(/bin/echo -n "a"; /bin/echo "b")" = "ab" ]
24 then
25     ECHO="/bin/echo"
26 else
27     echo "tapview: bailing out, your echo lacks -n support."
28     exit 3
29 fi
30
31 OK="."
32 FAIL="F"
33 SKIP="s"
34 TODO_NOT_OK="x"
35 TODO_OK="u"
36
37 ship_char() {
38     # shellcheck disable=SC2039
39     "${ECHO}" -n "$1"
40 }
41
42 ship_line() {
43     report="${report}${1}\n"
44 }
45
46 testcount=0
47 failcount=0
48 skipcount=0
49 todocount=0
50 test_before_plan=no
51 test_after_plan=no
52 expect=""
53 status=0
54
55 report=""
56 IFS=""
57 state=start
58 while read -r line
59 do
60     if expr "$line" : "Bail out!" >/dev/null
61     then
62         ship_line "$line"
63         status=2
64         break
65     fi
66     if expr "$line" : '1\.\.[0-9][0-9]*' >/dev/null >/dev/null
67     then
68         if [ "$expect" != "" ]
69         then
70             if [ "${testcount}" -gt 0 ]
71             then
72                 echo ""
73             fi
74             ship_line "Cannot have more than one plan line."
75             echo "${report}"
76             exit 1
77         fi
78         if expr "$line" : ".* *SKIP" >/dev/null || expr "$line" : ".* *skip" >/dev/null
79         then
80             ship_line "$line"
81             echo "${report}"
82             exit 1      # Not specified in the standard whether this should exit 1 or 0
83         fi
84         expect=$(expr "$line" : '1\.\.\([0-9][0-9]*\)')
85         continue
86     fi
87     if expr "$line" : "ok" >/dev/null
88     then
89         testcount=$((testcount + 1))
90         if [ "$expect" = "" ]
91         then
92             test_before_plan=yes
93         else
94             test_after_plan=yes
95         fi
96         if expr "$line" : ".*# *TODO" >/dev/null || expr "$line" : ".*# *todo" >/dev/null
97         then
98             ship_char ${TODO_OK}
99             ship_line "$line"
100             todocount=$((todocount + 1))
101         elif expr "$line" : ".*# *SKIP" >/dev/null || expr "$line" : ".*# *skip" >/dev/null
102         then
103             ship_char ${SKIP}
104             ship_line "$line"
105             skipcount=$((skipcount + 1))
106         else
107             ship_char ${OK}
108         fi
109         state=ok
110         continue
111     fi
112     if expr "$line" : "not ok" >/dev/null
113     then
114         testcount=$((testcount + 1))
115         if [ "$expect" = "" ]
116         then
117             test_before_plan=yes
118         else
119             test_after_plan=yes
120         fi
121         if expr "$line" : ".*# *SKIP" >/dev/null || expr "$line" : ".*# *skip" >/dev/null
122         then
123             ship_char "${SKIP}"
124             state=ok
125             skipcount=$((skipcount + 1))
126             continue
127         fi
128         if expr "$line" : ".*# *TODO" >/dev/null || expr "$line" : ".*# *todo" >/dev/null
129         then
130             ship_char ${TODO_NOT_OK}
131             state=ok
132             todocount=$((todocount + 1))
133             continue
134         fi
135         ship_char "${FAIL}"
136         ship_line "$line"
137         state=not_ok
138         failcount=$((failcount + 1))
139         status=1
140         continue
141     fi
142     # shellcheck disable=SC2166
143     if [ "${state}" = "yaml" ]
144     then
145         ship_line "$line"
146         if expr "$line" : '[    ]*\.\.\.' >/dev/null
147         then
148             state=ok
149         fi
150     elif expr "$line" : "[      ]*---" >/dev/null
151     then
152         ship_line "$line"
153         state=yaml
154     fi
155 done
156
157 /bin/echo ""
158
159 if [ -z "$expect" ]
160 then
161     ship_line "Missing a plan."
162     status=1
163 elif [ "$test_before_plan" = "yes" ] && [ "$test_after_plan" = "yes" ] 
164 then
165     ship_line "A plan line may only be placed before or after all tests."
166     status=1
167 elif [ "${expect}" -gt "${testcount}" ]
168 then
169     ship_line "Expected ${expect} tests but only ${testcount} ran."
170     status=1
171 elif [ "${expect}" -lt "${testcount}" ]
172 then
173     ship_line "Expected ${expect} tests but ${testcount} ran."
174     status=1
175 fi
176
177 report="${report}${testcount} tests, ${failcount} failures"
178 if [ "$todocount" != 0 ]
179 then
180     report="${report}, ${todocount} TODOs"
181 fi
182 if [ "$skipcount" != 0 ]
183 then
184     report="${report}, ${skipcount} SKIPs"
185 fi
186
187 echo "${report}."
188
189 exit "${status}"
190
191 # end