utils.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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="--no-color --config ./imagrc.toml --override-config store.implicit-create=true --rtp $RUNTIME"
  32. out "Calling '$bin $flags $*'"
  33. $bin $flags $*
  34. }
  35. cat_entry() {
  36. cat ${STORE}/$1
  37. }
  38. reset_store() {
  39. rm -rf "${STORE}"/.git
  40. rm -r "${STORE}"
  41. }
  42. call_test() {
  43. prepare_store_directory || {
  44. err "Preparing store directory failed"
  45. exit 1
  46. }
  47. out "-- TESTING: '$1' --"
  48. $1
  49. result=$?
  50. if [[ -z "$DONT_RESET_STORE" ]]; then
  51. out "Reseting store"
  52. reset_store
  53. out "Store reset done"
  54. fi
  55. [[ $result -eq 0 ]] || { err "-- FAILED: '$1'. Exiting."; exit 1; }
  56. success "-- SUCCESS: '$1' --"
  57. }
  58. __git() {
  59. out "Calling git: $*"
  60. git --work-tree=/tmp/store/ --git-dir=/tmp/store/.git $*
  61. }
  62. __git_commit() {
  63. out "Calling git-commit: $*"
  64. git --work-tree=/tmp/store/ --git-dir=/tmp/store/.git commit -m "$*"
  65. }
  66. prepare_store_directory() {
  67. out "Preparing /tmp/store"
  68. mkdir -p /tmp/store/ &&\
  69. touch /tmp/store/.gitkeep &&\
  70. __git init &&\
  71. __git config --local user.email "imag@imag-pim.org" &&\
  72. __git config --local user.name "Imag CI" &&\
  73. __git add .gitkeep &&\
  74. __git_commit 'Initial commit: .gitkeep'
  75. }
  76. invoke_tests() {
  77. out "Invoking tests."
  78. if [[ ! -z "$INVOKE_TEST" ]]; then
  79. out "Invoking only $INVOKE_TEST"
  80. call_test "$INVOKE_TEST"
  81. else
  82. out "Invoking $*"
  83. for t in $*; do
  84. call_test "$t"
  85. done
  86. fi
  87. }