logo

utils-std

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

tap.sh (3771B)


  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 <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. name="$1"; shift
  124. file="$1"; shift
  125. command -v mktemp >/dev/null 2>/dev/null || \
  126. skip $1 "t_file requires mktemp(1)"
  127. count=$((count+1))
  128. out="$(mktemp)"
  129. "${target?}" "$@" 2>&1 >"$out"
  130. ret="$?"
  131. if [ "$ret" != "$exp_ret" ]; then
  132. printf 'not ok %d - %s\n' "$count" "$name"
  133. printf '# Expected exit code %d, got %d\n' "$exp_ret" "$ret"
  134. printf "$out" | sed -e 's;^;# ;'
  135. err=1
  136. elif ! cmp -s "${out?}" "$file"; then
  137. printf 'not ok %d - %s\n' "$count" "$name"
  138. diff -u "$out" "$file" | sed -e 's;^;# ;'
  139. err=1
  140. else
  141. printf 'ok %d - %s\n' "$count" "$name"
  142. rm "$out"
  143. fi
  144. }
  145. t_end ()
  146. {
  147. if [ $count -ne $plans ]
  148. then
  149. printf 'Error: Ran %d instead of the planned %d tests\n' "$count" "$plans" >&2
  150. err=1
  151. fi
  152. exit $err
  153. }
  154. # $1 -> name
  155. # $2 -> reason for skipping
  156. skip ()
  157. {
  158. count=$((count+1))
  159. name="$1"
  160. shift
  161. printf 'ok %s # skip %s\n' "$count $name" "$*"
  162. }
  163. printf '1..%d\n' "$plans"
  164. trap t_end EXIT