2 # tapview - a TAP (Test Anything Protocol) viewer in pure POSIX shell
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 a
8 # license by reference.
10 # SPDX-FileCopyrightText: Eric S. Raymond <esr@thyrsus.com>
11 # SPDX-License-Identifier: BSD-2-Clause
14 # A newer version may be available at https://gitlab.com/esr/tapview
23 # shellcheck disable=SC2039
24 printf '%s' "$1" # https://www.etalabs.net/sh_tricks.html
28 report="${report}${1}\n"
32 # Terminate dot display and bail out with error
33 if [ "${testcount}" -gt 0 ]
37 report="${report}${1}\n"
52 # shellcheck disable=SC2086
53 context_get () { printenv "ctx_${1}${depth}"; }
54 context_set () { export "ctx_${1}${depth}=${2}"; }
59 context_set test_before_plan no
60 context_set test_after_plan no
67 if [ "$(context_get count)" -gt 0 ] && [ -z "$(context_get plan)" ]
69 ship_line "Missing a plan at line ${ln}."
71 elif [ "$(context_get test_before_plan)" = "yes" ] && [ "$(context_get test_after_plan)" = "yes" ]
73 ship_line "A plan line may only be placed before or after all tests."
75 elif [ "$(context_get plan)" != "" ] && [ "$(context_get expect)" -gt "$(context_get count)" ]
77 ship_line "Expected $(context_get expect) tests but only ${testcount} ran."
89 if expr "$line" : "Bail out!" >/dev/null
95 # Use the current indent to choose a scope level
96 indent=$(expr "$line" : '[ ]*')
97 if [ "${indent}" -lt "${depth}" ]
101 elif [ "${indent}" -gt "${depth}" ]
106 # Process a plan line
107 if expr "$line" : '[ ]*1\.\.[0-9][0-9]*' >/dev/null
109 if [ "$(context_get plan)" != "" ]
111 ship_error "tapview: cannot have more than one plan line."
113 if expr "$line" : ".* *SKIP" >/dev/null || expr "$line" : ".* *skip" >/dev/null
117 exit 1 # Not specified in the standard whether this should exit 1 or 0
119 context_set plan "${line}"
120 context_set expect "$(expr "$line" : '[ ]*1\.\.\([0-9][0-9]*\)')"
122 elif expr "$line" : '[ ]*[0-9][0-9]*\.\.[0-9][0-9]*' >/dev/null
124 echo "Ill-formed plan line at ${ln}"
127 # Check for out-of-order test point numbers with the sequence (TAP 14)
128 testpoint=$(expr "$line" : '.*ok *\([0-9][0-9]*\)')
129 if [ "${testpoint}" != "" ] && [ "$(context_get expect)" != "" ] && [ "${testpoint}" -gt "$(context_get expect)" ]
131 ship_error "tapview: testpoint number ${testpoint} is out of range for plan $(context_get plan)."
134 if expr "$line" : "[ ]*ok" >/dev/null
136 context_set count $(($(context_get count) + 1))
137 testcount=$((testcount + 1))
138 if [ "$(context_get plan)" = "" ]
140 context_set test_before_plan yes
142 context_set test_after_plan yes
144 if expr "$line" : "[^#]* # *TODO" >/dev/null || expr "$line" : "[^#]* # *todo" >/dev/null
148 todocount=$((todocount + 1))
149 if expr "$line" : "[^#]*#[^ ]" >/dev/null
151 ship_line "Suspicious comment leader at ${ln}"
153 elif expr "$line" : "[^#]* # *SKIP" >/dev/null || expr "$line" : "[^#]* # *skip" >/dev/null
157 skipcount=$((skipcount + 1))
158 if expr "$line" : "[^#]*#[^ ]" >/dev/null
160 ship_line "Suspicious comment leader at ${ln}"
168 # Process a not-ok line
169 if expr "$line" : "[ ]*not ok" >/dev/null
171 context_set count $(($(context_get count) + 1))
172 testcount=$((testcount + 1))
173 if [ "$(context_get plan)" = "" ]
175 context_set test_before_plan yes
177 context_set test_after_plan yes
179 if expr "$line" : "[^#]* # *SKIP" >/dev/null || expr "$line" : "[^#]* # *skip" >/dev/null
183 skipcount=$((skipcount + 1))
184 if expr "$line" : "[^#]* #[^ ]" >/dev/null
186 ship_line "Suspicious comment leader at lime ${ln}"
190 if expr "$line" : "[^#]* # *TODO" >/dev/null || expr "$line" : "[^#]* # *todo" >/dev/null
192 ship_char ${TODO_NOT_OK}
194 todocount=$((todocount + 1))
195 if expr "$line" : "[^#]* #[^ ]" >/dev/null
197 ship_line "Suspicious comment leader at line ${ln}"
204 failcount=$((failcount + 1))
206 if [ "$(context_get bail)" = yes ]
208 ship_line "Bailing out on line ${ln} due to +bail pragma."
213 # Process a TAP 14 pragma
214 if expr "$line" : "pragma" >/dev/null
217 # shellcheck disable=SC2086
220 +bail) context_set bail yes;;
221 -bail) context_set bail yes;;
222 +strict) context_set strict yes;;
223 -strict) context_set strict yes;;
224 *) ship_line "Pragma '$line' ignored";;
229 # shellcheck disable=SC2166
230 if [ "${state}" = "yaml" ]
233 if expr "$line" : '[ ]*\.\.\.' >/dev/null
239 elif expr "$line" : "[ ]*---" >/dev/null
245 # Ignore blank lines and comments
246 if [ -z "$line" ] || expr "$line" : '[ ]+$' >/dev/null || expr "$line" : "#" >/dev/null
250 # Any line that is not a valid plan, test result, pragma,
251 # or comment lands here.
252 if [ "$(context_get strict)" = yes ]
254 ship_line "Bailing out on line ${ln} due to +strict pragma"
265 report="${report}${testcount} tests, ${failcount} failures"
266 if [ "$todocount" != 0 ]
268 report="${report}, ${todocount} TODOs"
270 if [ "$skipcount" != 0 ]
272 report="${report}, ${skipcount} SKIPs"