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