Simple Makefile target helper https://pagure.io/mkit

tools.sh 3.1KB

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