utils.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env bash
  2. #
  3. #
  4. # This file contains test utility functions which are used by the test scripts
  5. # for each binary.
  6. #
  7. #
  8. COLOR_OFF='\e[0m' # Text Reset
  9. RED='\e[0;31m' # Red
  10. YELLOW='\e[0;33m' # Yellow
  11. GREEN='\e[0;32m' # Green
  12. RUNTIME="/tmp"
  13. STORE="${RUNTIME}/store"
  14. out() {
  15. [[ -z "$DEBUG_OUTPUT_OFF" ]] && echo -e "${YELLOW}:: $*${COLOR_OFF}"
  16. }
  17. success() {
  18. [[ -z "$DEBUG_OUTPUT_OFF" ]] && echo -e "${GREEN}>> $*${COLOR_OFF}"
  19. }
  20. err() {
  21. [[ -z "$DEBUG_OUTPUT_OFF" ]] && echo -e "${RED}!! $*${COLOR_OFF}"
  22. }
  23. silent() {
  24. DEBUG_OUTPUT_OFF=1 $*
  25. }
  26. imag-call-binary() {
  27. local searchdir=$1; shift
  28. local binary=$1; shift
  29. [[ -d $searchdir ]] || { err "FATAL: No directory $searchdir"; exit 1; }
  30. local bin=$(find $searchdir -iname $binary -type f -executable)
  31. local flags="--config ./imagrc.toml --override-config store.implicit-create=true --debug --rtp $RUNTIME"
  32. out "Calling '$bin $flags $*'"
  33. $bin $flags $*
  34. }
  35. cat_entry() {
  36. cat ${STORE}/$1
  37. }
  38. reset_store() {
  39. rm -r "${STORE}"
  40. }
  41. call_test() {
  42. out "-- TESTING: '$1' --"
  43. $1
  44. result=$?
  45. if [[ -z "$DONT_RESET_STORE" ]]; then
  46. out "Reseting store"
  47. reset_store
  48. out "Store reset done"
  49. fi
  50. [[ $result -eq 0 ]] || { err "-- FAILED: '$1'. Exiting."; exit 1; }
  51. success "-- SUCCESS: '$1' --"
  52. }
  53. invoke_tests() {
  54. out "Invoking tests."
  55. if [[ ! -z "$INVOKE_TEST" ]]; then
  56. out "Invoking only $INVOKE_TEST"
  57. call_test "$INVOKE_TEST"
  58. else
  59. out "Invoking $*"
  60. for t in $*; do
  61. call_test "$t"
  62. done
  63. fi
  64. }