shell dot on steroids https://pagure.io/shellfu

tools.sh 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/bash
  2. #shellcheck disable=SC1090
  3. . "$TF_DIR/include/common.sh"
  4. # 1. exec: [test] -> [result]
  5. # 2. eval: [result] == [oracle]
  6. tf_testflt() {
  7. #
  8. # Run a simple test for a unix filter
  9. #
  10. # tf_testflt -n foo [-i foo.stdin] \
  11. # [-O foo.out] [-E foo.err] [-S 3] \
  12. # cmd arg...
  13. #
  14. # Will drop *result/NAME.out* and *result/NAME.err* (intentionally
  15. # not cleaning up).
  16. #
  17. # defaults
  18. #
  19. local t_in="/dev/null" # test: stdin
  20. local t_name="" # test: name
  21. # command is "$@" after arg parsing
  22. local t_es="0" # final test exit status
  23. local o_out="/dev/null" # oracle: stdout
  24. local o_err="/dev/null" # oracle: stderr
  25. local o_es="0" # oralce: exit status
  26. local r_out r_err r_es # result: ^ ^ ^ those 3
  27. local diff=diff
  28. type colordiff >/dev/null 2>/dev/null && diff=colordiff
  29. # get args
  30. #
  31. local orig_args="$0 $*"
  32. tf_debug "orig_args=$orig_args"
  33. local arg_err=false
  34. while true; do case "$1" in
  35. -i) t_in="$2"; shift 2 || { arg_err=true; break; } ;;
  36. -n) t_name="$2"; shift 2 || { arg_err=true; break; } ;;
  37. -O) o_out="$2"; shift 2 || { arg_err=true; break; } ;;
  38. -E) o_err="$2"; shift 2 || { arg_err=true; break; } ;;
  39. -S) o_es="$2"; shift 2 || { arg_err=true; break; } ;;
  40. --) shift; break ;;
  41. "") break ;;
  42. -*) tf_warn "wrong testcli arg: $1"; return "$TF_ES_BAILOUT" ;;
  43. *) break ;;
  44. esac done
  45. $arg_err && { tf_warn "error parsing arguments: $orig_args"; return "$TF_ES_BAILOUT"; }
  46. tf_debug "t_in='$t_in'"
  47. tf_debug "t_name='$t_name'"
  48. tf_debug "o_out='$o_out'"
  49. tf_debug "o_err='$o_err'"
  50. tf_debug "o_es='$o_es'"
  51. tf_debug "test command: $*"
  52. test "$t_in" = "-" && t_in=/dev/stdin # works better for check below
  53. test -z "$t_name" && { tf_warn "missing test name" ; return "$TF_ES_BAILOUT"; }
  54. test -z "$1" && { tf_warn "missing test command" ; return "$TF_ES_BAILOUT"; }
  55. test -r "$t_in" || { tf_warn "missing input file: $t_in" ; return "$TF_ES_BAILOUT"; }
  56. test -e "$o_out" || { tf_warn "missing oracle stdout: $o_out" ; return "$TF_ES_BAILOUT"; }
  57. test -e "$o_err" || { tf_warn "missing oracle stderr: $o_err" ; return "$TF_ES_BAILOUT"; }
  58. test "$o_es" -ge 0 || { tf_warn "invalid oracle status: $o_es" ; return "$TF_ES_BAILOUT"; }
  59. # prepare
  60. #
  61. mkdir -p result
  62. r_out="result/$t_name.out"
  63. r_err="result/$t_name.err"
  64. tf_debug "r_out='$r_out'"
  65. tf_debug "r_err='$r_err'"
  66. touch "$r_out" || { tf_warn "cannot create tmp file: $r_out" ; return "$TF_ES_BAILOUT"; }
  67. touch "$r_err" || { tf_warn "cannot create tmp file: $r_err" ; return "$TF_ES_PANIC"; }
  68. # run
  69. #
  70. ( <"$t_in" eval "$@" >"$r_out" 2>"$r_err" ); r_es=$?
  71. tf_debug "r_es='$r_es'"
  72. # eval/report/exit
  73. #
  74. test "$r_es" = "$o_es" || { tf_warn "bad exit status: $r_es (need $o_es)" ; t_es=$TF_ES_FAIL; }
  75. $diff -u "$r_err" "$o_err" || t_es=$TF_ES_FAIL
  76. $diff -u "$r_out" "$o_out" || t_es=$TF_ES_FAIL
  77. return "$t_es"
  78. }