logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

tap.sh (5542B)


  1. #!/bin/false
  2. # utils-std: Collection of commonly available Unix tools
  3. # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  4. # SPDX-License-Identifier: MPL-2.0
  5. count=0
  6. err=0
  7. # t [--exit=n] [--input=str] <test_name> <arguments> <expected_output>
  8. t ()
  9. {
  10. exp_ret=0
  11. for i; do
  12. case "$i" in
  13. --exit=*)
  14. exp_ret="${i#*=}"
  15. shift
  16. ;;
  17. --input=*)
  18. input="${i#*=}"
  19. shift
  20. ;;
  21. --)
  22. shift
  23. break
  24. ;;
  25. # Note: * is still a wildcard, even with a range before
  26. -[a-zA-Z0-9]|--[a-zA-Z0-9]*=*)
  27. printf 'Unknown option: %s\n' "$i"
  28. exit 2
  29. ;;
  30. *)
  31. break
  32. ;;
  33. esac
  34. done
  35. if [ "${input+set}" = "set" ]; then
  36. # Append a final slash so sh(1) doesn't trims final newlines
  37. out="$(printf "${input?}" | "${target?}" $2 2>&1;r=$?;printf %s /;exit $r)"
  38. ret="$?"
  39. else
  40. # Append a final slash so sh(1) doesn't trims final newlines
  41. out="$("${target?}" $2 2>&1;r=$?;printf %s /;exit $r)"
  42. ret="$?"
  43. fi
  44. out="${out%/}"
  45. count=$((count+1))
  46. if [ "$ret" != "$exp_ret" ]; then
  47. printf 'not ok %d - %s\n' "$count" "$1"
  48. printf '# Expected exit code %d, got %d\n' "$exp_ret" "$ret"
  49. printf '# == Got ==\n'
  50. printf '# %s\n' "$out" | sed -e 's;^[^#];# ;'
  51. err=1
  52. elif [ "$out" != "$3" ]; then
  53. printf 'not ok %d - %s\n' "$count" "$1"
  54. printf '# == Expected ==\n'
  55. printf '# %s\n' "$3" | sed -e 's;^[^#];# ;'
  56. printf '# == Got ==\n'
  57. printf '# %s\n' "$out" | sed -e 's;^[^#];# ;'
  58. err=1
  59. else
  60. printf 'ok %d - %s\n' "$count" "$1"
  61. fi
  62. }
  63. # t_args [--exit=n] [--input=str] <test_name> <expected_output> <arguments ...>
  64. t_args() {
  65. exp_ret=0
  66. for i; do
  67. case "$i" in
  68. --exit=*)
  69. exp_ret="${i#*=}"
  70. shift
  71. ;;
  72. --input=*)
  73. input="${i#*=}"
  74. shift
  75. ;;
  76. --)
  77. shift
  78. break
  79. ;;
  80. # Note: * is still a wildcard, even with a range before
  81. -[a-zA-Z0-9]|--[a-zA-Z0-9]*=*)
  82. printf 'Unknown option: %s\n' "$i"
  83. exit 2
  84. ;;
  85. *)
  86. break
  87. ;;
  88. esac
  89. done
  90. name="$1"; shift
  91. exp_out="$1"; shift
  92. if [ "${input+set}" = "set" ]; then
  93. # Append a final slash so sh(1) doesn't trims final newlines
  94. out="$(printf "${input?}" | "${target?}" "$@" 2>&1;r=$?;printf %s /;exit $r)"
  95. ret="$?"
  96. else
  97. # Append a final slash so sh(1) doesn't trims final newlines
  98. out="$("${target?}" "$@" 2>&1;r=$?;printf %s /;exit $r)"
  99. ret="$?"
  100. fi
  101. out="${out%/}"
  102. count=$((count+1))
  103. if [ "$ret" != "$exp_ret" ]; then
  104. printf 'not ok %d - %s\n' "$count" "$name"
  105. printf '# Expected exit code %d, got %d\n' "$exp_ret" "$ret"
  106. printf '# == Got ==\n'
  107. printf '# %s\n' "$out" | sed -e 's;^[^#];# ;'
  108. err=1
  109. elif [ "$out" != "$exp_out" ]; then
  110. printf 'not ok %d - %s\n' "$count" "$name"
  111. printf '# == Expected ==\n'
  112. printf '# %s\n' "$exp_out" | sed -e 's;^[^#];# ;'
  113. printf '# == Got ==\n'
  114. printf '# %s\n' "$out" | sed -e 's;^[^#];# ;'
  115. err=1
  116. else
  117. printf 'ok %d - %s\n' "$count" "$name"
  118. fi
  119. }
  120. # t_file [--exit=n] [--infile=file] <test_name> <file_expected> [arguments ...]
  121. # file_expected is an existing file to compare output against
  122. t_file()
  123. {
  124. exp_ret=0
  125. for i; do
  126. case "$i" in
  127. --exit=*)
  128. exp_ret="${i#*=}"
  129. shift
  130. ;;
  131. --infile=*)
  132. infile="${i#*=}"
  133. shift
  134. ;;
  135. --)
  136. shift
  137. break
  138. ;;
  139. # Note: * is still a wildcard, even with a range before
  140. -[a-zA-Z0-9]|--[a-zA-Z0-9]*=*)
  141. printf 'Unknown option: %s\n' "$i"
  142. exit 2
  143. ;;
  144. *)
  145. break
  146. ;;
  147. esac
  148. done
  149. name="$1"; shift
  150. file="$1"; shift
  151. command -v mktemp >/dev/null 2>/dev/null || \
  152. skip $1 "t_file requires mktemp(1)"
  153. count=$((count+1))
  154. out="$(mktemp)"
  155. if [ "${infile+set}" = "set" ]; then
  156. <"$infile" "${target?}" "$@" 2>&1 >"$out"
  157. else
  158. "${target?}" "$@" 2>&1 >"$out"
  159. fi
  160. ret="$?"
  161. if [ "$ret" != "$exp_ret" ]; then
  162. printf 'not ok %d - %s\n' "$count" "$name"
  163. printf '# Expected exit code %d, got %d\n' "$exp_ret" "$ret"
  164. printf '# %s\n' "$out" | sed -e 's;^[^#];# ;'
  165. err=1
  166. elif ! cmp -s "${out?}" "$file"; then
  167. printf 'not ok %d - %s\n' "$count" "$name"
  168. diff -u "$out" "$file" | sed -e 's;^;# ;'
  169. err=1
  170. else
  171. printf 'ok %d - %s\n' "$count" "$name"
  172. rm "$out"
  173. fi
  174. }
  175. # t_cmd [--exit=n] <test_name> <expected_output> <command> [arguments]
  176. t_cmd() {
  177. exp_ret=0
  178. for i; do
  179. case "$i" in
  180. --exit=*)
  181. exp_ret="${i#*=}"
  182. shift
  183. ;;
  184. --)
  185. shift
  186. break
  187. ;;
  188. # Note: * is still a wildcard, even with a range before
  189. -[a-zA-Z0-9]|--[a-zA-Z0-9]*=*)
  190. printf 'Unknown option: %s\n' "$i"
  191. exit 2
  192. ;;
  193. *)
  194. break
  195. ;;
  196. esac
  197. done
  198. name="$1"; shift
  199. exp_out="$1"; shift
  200. # Append a final slash so sh(1) doesn't trims final newlines
  201. out="$("$@" 2>&1;r=$?;printf %s /;exit $r)"
  202. ret="$?"
  203. out="${out%/}"
  204. count=$((count+1))
  205. if [ "$ret" != "$exp_ret" ]; then
  206. printf 'not ok %d - %s\n' "$count" "$name"
  207. printf '# Expected exit code %d, got %d\n' "$exp_ret" "$ret"
  208. printf '# %s\n' "$out" | sed -e 's;^[^#];# ;'
  209. err=1
  210. elif [ "$out" != "$exp_out" ]; then
  211. printf 'not ok %d - %s\n' "$count" "$name"
  212. printf '# == Expected ==\n'
  213. printf '# %s\n' "$exp_out" | sed -e 's;^[^#];# ;'
  214. printf '# == Got ==\n'
  215. printf '# %s\n' "$out" | sed -e 's;^[^#];# ;'
  216. err=1
  217. else
  218. printf 'ok %d - %s\n' "$count" "$name"
  219. fi
  220. }
  221. t_end ()
  222. {
  223. if [ $count -ne $plans ]
  224. then
  225. printf 'error: Ran %d instead of the planned %d tests\n' "$count" "$plans" >&2
  226. err=1
  227. fi
  228. exit $err
  229. }
  230. # $1 -> name
  231. # $2 -> reason for skipping
  232. skip ()
  233. {
  234. count=$((count+1))
  235. name="$1"
  236. shift
  237. printf 'ok %s # skip %s\n' "$count $name" "$*"
  238. }
  239. if ! test -f "${target?}"; then
  240. printf '1..0 # SKIP: missing executable: %s\n' "${target?}"
  241. exit 0
  242. fi
  243. printf '1..%d\n' "$plans"
  244. trap t_end EXIT