logo

utils-cidr

utilities to manipulate CIDR ip-ranges git clone https://anongit.hacktivis.me/git/utils-cidr.git

tap.sh (3086B)


  1. #!/bin/false
  2. # extracted from https://hacktivis.me/git/utils-std
  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> <expected_output> <arguments ...>
  8. t() {
  9. exp_ret=0
  10. for i; do
  11. case "$i" in
  12. --exit=*)
  13. exp_ret="${i#*=}"
  14. shift
  15. ;;
  16. --input=*)
  17. input="${i#*=}"
  18. shift
  19. ;;
  20. --)
  21. shift
  22. break
  23. ;;
  24. # Note: * is still a wildcard, even with a range before
  25. -[a-zA-Z0-9]|--[a-zA-Z0-9]*=*)
  26. printf 'Unknown option: %s\n' "$i"
  27. exit 2
  28. ;;
  29. *)
  30. break
  31. ;;
  32. esac
  33. done
  34. name="$1"; shift
  35. exp_out="$1"; shift
  36. if [ "${input+set}" = "set" ]; then
  37. # Append a final slash so sh(1) doesn't trims final newlines
  38. out="$(printf "${input?}" | "${target?}" "$@" 2>&1;r=$?;printf %s /;exit $r)"
  39. ret="$?"
  40. else
  41. # Append a final slash so sh(1) doesn't trims final newlines
  42. out="$("${target?}" "$@" 2>&1;r=$?;printf %s /;exit $r)"
  43. ret="$?"
  44. fi
  45. out="${out%/}"
  46. count=$((count+1))
  47. if [ "$ret" != "$exp_ret" ]; then
  48. printf 'not ok %d - %s\n' "$count" "$name"
  49. printf '# Expected exit code %d, got %d\n' "$exp_ret" "$ret"
  50. printf '# == Got ==\n'
  51. printf '# %s\n' "$out" | sed -e 's;^[^#];# ;'
  52. err=1
  53. elif [ "$out" != "$exp_out" ]; then
  54. printf 'not ok %d - %s\n' "$count" "$name"
  55. printf '# == Expected ==\n'
  56. printf '# %s\n' "$exp_out" | sed -e 's;^[^#];# ;'
  57. printf '# == Got ==\n'
  58. printf '# %s\n' "$out" | sed -e 's;^[^#];# ;'
  59. err=1
  60. else
  61. printf 'ok %d - %s\n' "$count" "$name"
  62. fi
  63. }
  64. # t_cmd [--exit=n] <test_name> <expected_output> <command> [arguments]
  65. t_cmd() {
  66. exp_ret=0
  67. for i; do
  68. case "$i" in
  69. --exit=*)
  70. exp_ret="${i#*=}"
  71. shift
  72. ;;
  73. --)
  74. shift
  75. break
  76. ;;
  77. # Note: * is still a wildcard, even with a range before
  78. -[a-zA-Z0-9]|--[a-zA-Z0-9]*=*)
  79. printf 'Unknown option: %s\n' "$i"
  80. exit 2
  81. ;;
  82. *)
  83. break
  84. ;;
  85. esac
  86. done
  87. name="$1"; shift
  88. exp_out="$1"; shift
  89. [ -z "${name}" ] && name="$*"
  90. # Append a final slash so sh(1) doesn't trims final newlines
  91. out="$("$@" 2>&1;r=$?;printf %s /;exit $r)"
  92. ret="$?"
  93. out="${out%/}"
  94. count=$((count+1))
  95. if [ "$ret" != "$exp_ret" ]; then
  96. printf 'not ok %d - %s\n' "$count" "$name"
  97. printf '# Expected exit code %d, got %d\n' "$exp_ret" "$ret"
  98. printf '# %s\n' "$out" | sed -e 's;^[^#];# ;'
  99. err=1
  100. elif [ "$out" != "$exp_out" ]; then
  101. printf 'not ok %d - %s\n' "$count" "$name"
  102. printf '# == Expected ==\n'
  103. printf '# %s\n' "$exp_out" | sed -e 's;^[^#];# ;'
  104. printf '# == Got ==\n'
  105. printf '# %s\n' "$out" | sed -e 's;^[^#];# ;'
  106. err=1
  107. else
  108. printf 'ok %d - %s\n' "$count" "$name"
  109. fi
  110. }
  111. t_end()
  112. {
  113. if [ $count -ne $plans ]
  114. then
  115. printf 'error: Ran %d instead of the planned %d tests\n' "$count" "$plans" >&2
  116. err=1
  117. fi
  118. exit $err
  119. }
  120. # $1 -> name
  121. # $2 -> reason for skipping
  122. skip()
  123. {
  124. count=$((count+1))
  125. name="$1"
  126. shift
  127. printf 'ok %s # skip %s\n' "$count $name" "$*"
  128. }
  129. if ! test -f "${target?}"; then
  130. printf '1..0 # SKIP: missing executable: %s\n' "${target?}"
  131. exit 0
  132. fi
  133. printf '1..%d\n' "$plans"
  134. trap t_end EXIT