logo

utils-std

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

tap.sh (5334B)


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