JAT - Just A Testing library https://pagure.io/shellfu-bash-jat

tools.sh 3.2KB

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