Debian Codegraph @

tests/tapview

raw
1 #! /bin/sh
2 # tapview - a TAP (Test Anything Protocol) viewer in pure POSIX shell
3 #
4 # This code is intended to be embedded in your project. The author
5 # grants permission for it to be distributed under the prevailing
6 # license of your project if you choose, provided that license is
7 # OSD-compliant; otherwise the following SPDX tag incorporates the
8 # MIT No Attribution license by reference.
9 #
10 # SPDX-FileCopyrightText: (C) Eric S. Raymond <[email protected]>
11 # SPDX-License-Identifier: MIT-0
12 #
13 # This version shipped on 2024-02-05.
14 #
15 # A newer version may be available at https://gitlab.com/esr/tapview;
16 # check the ship date oagainst the commit list there to see if it
17 # might be a good idea to update.
18
19 OK="."
20 FAIL="F"
21 SKIP="s"
22 TODO_NOT_OK="x"
23 TODO_OK="u"
24 LF='
25 '
26
27 ship_char() {
28 # shellcheck disable=SC2039
29 printf '%s' "$1" # https://www.etalabs.net/sh_tricks.html
30 }
31
32 ship_line() {
33 report="${report}${1}$LF"
34 }
35
36 ship_error() {
37 # Terminate dot display and bail out with error
38 if [ "${testcount}" -gt 0 ]
39 then
40 echo ""
41 fi
42 report="${report}${1}$LF"
43 echo "${report}"
44 exit 1
45 }
46
47 testcount=0
48 failcount=0
49 skipcount=0
50 todocount=0
51 status=0
52 report=""
53 ln=0
54 state=plaintext
55
56 # shellcheck disable=SC2086
57 context_get () { printenv "ctx_${1}${depth}"; }
58 context_set () { export "ctx_${1}${depth}=${2}"; }
59
60 context_push () {
61 context_set plan ""
62 context_set count 0
63 context_set test_before_plan no
64 context_set test_after_plan no
65 context_set expect ""
66 context_set bail no
67 context_set strict no
68 }
69
70 context_pop () {
71 if [ "$(context_get count)" -gt 0 ] && [ -z "$(context_get plan)" ]
72 then
73 ship_line "Missing a plan at line ${ln}."
74 status=1
75 elif [ "$(context_get test_before_plan)" = "yes" ] && [ "$(context_get test_after_plan)" = "yes" ]
76 then
77 ship_line "A plan line may only be placed before or after all tests."
78 status=1
79 elif [ "$(context_get plan)" != "" ] && [ "$(context_get expect)" -gt "$(context_get count)" ]
80 then
81 ship_line "Expected $(context_get expect) tests but only ${testcount} ran."
82 status=1
83 elif [ "$(context_get plan)" != "" ] && [ "$(context_get expect)" -lt "$(context_get count)" ]
84 then
85 ship_line "${testcount} ran but $(context_get expect) expected."
86 status=1
87 fi
88 }
89
90 directive () {
91 case "$1" in
92 *[[:space:]]#[[:space:]]*$2*) return 0;;
93 *) return 1;;
94 esac
95 }
96
97 depth=0
98 context_push
99 while read -r line
100 do
101 ln=$((ln + 1))
102 IFS=" "
103 # shellcheck disable=SC2086
104 set -- $line
105 tok1="$1"
106 tok2="$2"
107 tok3="$3"
108 IFS=""
109 # Ignore blank lines and comments
110 if [ -z "$tok1" ] || [ "$tok1" = '#' ]
111 then
112 continue
113 fi
114 # Process bailout
115 if [ "$tok1" = "Bail" ] && [ "$tok2" = "out!" ]
116 then
117 ship_line "$line"
118 status=2
119 break
120 fi
121 # Use the current indent to choose a scope level
122 leading_spaces="${line%%[! ]*}"
123 indent=${#leading_spaces}
124 if [ "${indent}" -lt "${depth}" ]
125 then
126 context_pop
127 depth="${indent}"
128 elif [ "${indent}" -gt "${depth}" ]
129 then
130 depth="${indent}"
131 context_push
132 fi
133 # Process a plan line
134 case "$tok1" in
135 [0123456789]*)
136 case "$tok1" in
137 1[.][.]*)
138 if [ "$(context_get plan)" != "" ]
139 then
140 ship_error "tapview: cannot have more than one plan line."
141 fi
142 if directive "$line" [Ss][Kk][Ii][Pp]
143 then
144 ship_line "$line"
145 echo "${report}"
146 exit 1 # Not specified in the standard whether this should exit 1 or 0
147 fi
148 context_set plan "${line}"
149 context_set expect "${tok1##[1][.][.]}"
150 continue
151 ;;
152 *)
153 echo "Ill-formed plan line at ${ln}"
154 exit 1
155 ;;
156 esac
157 ;; esac
158 # Check for test point numbers out-of-order with the sequence (TAP 14)
159 testpoint=""
160 case "$tok1" in
161 ok) testpoint="$tok2";;
162 not) testpoint="$tok3";;
163 esac
164 case "$testpoint" in
165 *[0123456789]*)
166 if [ "${testpoint}" != "" ] && [ "$(context_get expect)" != "" ] && [ "${testpoint}" -gt "$(context_get expect)" ]
167 then
168 ship_error "tapview: testpoint number ${testpoint} is out of range for plan $(context_get plan)."
169 fi
170 ;; esac
171 # Process an ok line
172 if [ "$tok1" = "ok" ]
173 then
174 context_set count $(($(context_get count) + 1))
175 testcount=$((testcount + 1))
176 if [ "$(context_get plan)" = "" ]
177 then
178 context_set test_before_plan yes
179 else
180 context_set test_after_plan yes
181 fi
182 if directive "$line" [Tt][Oo][Dd][Oo]
183 then
184 ship_char ${TODO_OK}
185 ship_line "$line"
186 todocount=$((todocount + 1))
187 elif directive "$line" [Ss][Kk][Ii][Pp]
188 then
189 ship_char ${SKIP}
190 ship_line "$line"
191 skipcount=$((skipcount + 1))
192 else
193 ship_char ${OK}
194 fi
195 state=plaintext
196 continue
197 fi
198 # Process a not-ok line
199 if [ "$tok1" = "not" ] && [ "$tok2" = "ok" ]
200 then
201 context_set count $(($(context_get count) + 1))
202 testcount=$((testcount + 1))
203 if [ "$(context_get plan)" = "" ]
204 then
205 context_set test_before_plan yes
206 else
207 context_set test_after_plan yes
208 fi
209 if directive "$line" [Ss][Kk][Ii][Pp]
210 then
211 ship_char "${SKIP}"
212 state=plaintext
213 skipcount=$((skipcount + 1))
214 continue
215 fi
216 if directive "$line" [Tt][Oo][Dd][Oo]
217 then
218 ship_char ${TODO_NOT_OK}
219 state=plaintext
220 todocount=$((todocount + 1))
221 continue
222 fi
223 ship_char "${FAIL}"
224 ship_line "$line"
225 state=plaintext
226 failcount=$((failcount + 1))
227 status=1
228 if [ "$(context_get bail)" = yes ]
229 then
230 ship_line "Bailing out on line ${ln} due to +bail pragma."
231 break
232 fi
233 continue
234 fi
235 # Process a TAP 14 pragma
236 case "$line" in
237 pragma*)
238 case "$line" in
239 *+bail*) context_set bail yes;;
240 *-bail*) context_set bail yes;;
241 *+strict*) context_set strict yes;;
242 *-strict*) context_set strict yes;;
243 *) ship_line "Pragma '$line' ignored";;
244 esac
245 continue
246 ;;
247 esac
248 # shellcheck disable=SC2166
249 if [ "${state}" = "yaml" ]
250 then
251 ship_line "$line"
252 if [ "$tok1" = "..." ]
253 then
254 state=plaintext
255 else
256 continue
257 fi
258 elif [ "$tok1" = "---" ]
259 then
260 ship_line "$line"
261 state=yaml
262 continue
263 fi
264 # Any line that is not a valid plan, test result, pragma,
265 # or comment lands here.
266 if [ "$(context_get strict)" = yes ]
267 then
268 ship_line "Bailing out on line ${ln} due to +strict pragma"
269 status=1
270 break
271 fi
272 done
273
274 /bin/echo ""
275 depth=0
276 context_pop
277 report="${report}${testcount} tests, ${failcount} failures"
278 if [ "$todocount" != 0 ]
279 then
280 report="${report}, ${todocount} TODOs"
281 fi
282 if [ "$skipcount" != 0 ]
283 then
284 report="${report}, ${skipcount} SKIPs"
285 fi
286
287 echo "${report}."
288 exit "${status}"
289
290 # end