tools.sh 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 ffoo.stdout] [-E ffoo.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. # get args
  27. #
  28. while true; do case "$1" in
  29. -i) t_in="$2"; shift 2 ;;
  30. -n) t_name="$2"; shift 2 ;;
  31. -O) o_out="$2"; shift 2 ;;
  32. -E) o_err="$2"; shift 2 ;;
  33. -S) o_es="$2"; shift 2 ;;
  34. --) shift; break ;;
  35. "") break ;;
  36. -*) tf_warn "wrong testcli arg: $1"; return $TF_ES_BAILOUT ;;
  37. *) break ;;
  38. esac done
  39. tf_debug "t_in='$t_in'"
  40. tf_debug "t_name='$t_name'"
  41. tf_debug "o_out='$o_out'"
  42. tf_debug "o_err='$o_err'"
  43. tf_debug "o_es='$o_es'"
  44. tf_debug "test command: $*"
  45. test "$t_in" = "-" && t_in=/dev/stdin # works better for check below
  46. test -z "$t_name" && { tf_warn "missing test name" ; return $TF_ES_BAILOUT; }
  47. test -z "$1" && { tf_warn "missing test command" ; return $TF_ES_BAILOUT; }
  48. test -r "$t_in" || { tf_warn "missing input file: $t_in" ; return $TF_ES_BAILOUT; }
  49. test -e "$o_out" || { tf_warn "missing oracle stdout: $o_out" ; return $TF_ES_BAILOUT; }
  50. test -e "$o_err" || { tf_warn "missing oracle stderr: $o_err" ; return $TF_ES_BAILOUT; }
  51. # prepare
  52. #
  53. mkdir -p result
  54. r_out="result/$t_name.stdout"
  55. r_err="result/$t_name.stderr"
  56. tf_debug "r_out='$r_out'"
  57. tf_debug "r_err='$r_err'"
  58. touch $r_out || { tf_warn "cannot create tmp file: $r_out" ; return $TF_ES_BAILOUT; }
  59. touch $r_err || { tf_warn "cannot create tmp file: $r_err" ; return $TF_ES_PANIC; }
  60. # run
  61. #
  62. ( <$t_in eval "$@" >$r_out 2>$r_err ); r_es=$?
  63. tf_debug "r_es='$r_es'"
  64. # eval/report/exit
  65. #
  66. test $r_es = $o_es || { tf_warn "bad exit status: $r_es (need $o_es)" ; t_es=$TF_ES_FAIL; }
  67. diff -u $o_err $r_err || t_es=$TF_ES_FAIL
  68. diff -u $o_out $r_out || t_es=$TF_ES_FAIL
  69. return $t_es
  70. }